Skip to content

Commit

Permalink
[Refactor] 기록들 및 기록계산을 위한 struct: RecordTimes 구조체 제작
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed May 18, 2022
1 parent 53e3b17 commit 497c913
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Project_Timer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
8791C1F227DCD050000D6BA9 /* Notification+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8791C1F127DCD050000D6BA9 /* Notification+Extension.swift */; };
8791C1F427DCD120000D6BA9 /* UserDefaultsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8791C1F327DCD120000D6BA9 /* UserDefaultsManager.swift */; };
8791C1F627DCE703000D6BA9 /* UIView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8791C1F527DCE703000D6BA9 /* UIView+Extension.swift */; };
87A4704826493D4D007B89D6 /* Time.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A4704726493D4D007B89D6 /* Time.swift */; };
87A4704826493D4D007B89D6 /* RecordTimes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A4704726493D4D007B89D6 /* RecordTimes.swift */; };
87A4704A26493D6E007B89D6 /* StaticCircularProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A4704926493D6E007B89D6 /* StaticCircularProgressView.swift */; };
87A48BC727DEF0C200F46D0F /* Date+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A48BC627DEF0C200F46D0F /* Date+Extension.swift */; };
87A48BC927DF026800F46D0F /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A48BC827DF026800F46D0F /* Int+Extension.swift */; };
Expand Down Expand Up @@ -84,7 +84,7 @@
8791C1F127DCD050000D6BA9 /* Notification+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Extension.swift"; sourceTree = "<group>"; };
8791C1F327DCD120000D6BA9 /* UserDefaultsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsManager.swift; sourceTree = "<group>"; };
8791C1F527DCE703000D6BA9 /* UIView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Extension.swift"; sourceTree = "<group>"; };
87A4704726493D4D007B89D6 /* Time.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Time.swift; sourceTree = "<group>"; };
87A4704726493D4D007B89D6 /* RecordTimes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordTimes.swift; sourceTree = "<group>"; };
87A4704926493D6E007B89D6 /* StaticCircularProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StaticCircularProgressView.swift; sourceTree = "<group>"; };
87A48BC627DEF0C200F46D0F /* Date+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extension.swift"; sourceTree = "<group>"; };
87A48BC827DF026800F46D0F /* Int+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extension.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -134,7 +134,7 @@
children = (
87459705263FF0D700172C0F /* Daily.swift */,
87459707263FF0F000172C0F /* Storage.swift */,
87A4704726493D4D007B89D6 /* Time.swift */,
87A4704726493D4D007B89D6 /* RecordTimes.swift */,
877E448B267DF8DD00BF104A /* Dumy.swift */,
);
path = Model;
Expand Down Expand Up @@ -422,7 +422,7 @@
932EE05A2490A78400B00774 /* SetViewController.swift in Sources */,
87C4EB30266A4E8E00A6C3A9 /* todayContentView.swift in Sources */,
87A48BCB27DF112B00F46D0F /* LogVM.swift in Sources */,
87A4704826493D4D007B89D6 /* Time.swift in Sources */,
87A4704826493D4D007B89D6 /* RecordTimes.swift in Sources */,
93A892B124C14D4700F89180 /* CircularProgressView.swift in Sources */,
8791C1F427DCD120000D6BA9 /* UserDefaultsManager.swift in Sources */,
9316ECD5253F40A00082CAE8 /* SetTimerViewController.swift in Sources */,
Expand Down
2 changes: 0 additions & 2 deletions Project_Timer/Global/Model/Daily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct Daily: Codable, CustomStringConvertible {
var day: Date = Date()
var timeline = Array(repeating: 0, count: 24) //24시 : 0, 01시 : 1
var tasks: [String: Int] = [:] // 과목명 : 누적시간
var currentTask: String = "" // tasks 증가를 위한 key값
var currentTaskFromTime: Int? = 0 // tasks 증가를 위한 기준값
var maxTime: Int = 0 //최고연속시간
var totalTime: Int { // computed property
return self.tasks.values.reduce(0, +)
Expand Down
116 changes: 116 additions & 0 deletions Project_Timer/Global/Model/RecordTimes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Time.swift
// Project_Timer
//
// Created by Kang Minsang on 2021/02/28.
// Copyright © 2021 FDEE. All rights reserved.
//

import Foundation

struct RecordTimes: Codable, CustomStringConvertible {
var description: String {
return "\(self.currentTimes())"
}
static let fileName: String = "recordTimes.json"
typealias Times = (sum: Int, timer: Int, stopwatch: Int, goal: Int)

private(set) var recordTask: String = "none" // 측정중인 과목명
private(set) var recordTaskFromTime: Int = 0 // 측정중인 과목 기준시간값

private(set) var recordStartAt: Date = Date() // 기록측정 시작시각
private(set) var recording: Bool = false // 기록중인지 여부값, network 상 다른 기기에서도 표시 가능

private var recordingMode: Int = 1 // 기록모드값, 1: timer, 2: stopwatch
private var settedGoalTime: Int = 21600// 사용자가 설정한 목표시간값
private var settedTimerTime: Int = 2400 // 사용자가 설정한 타이머 시간값

private var savedSumTime: Int = 0 // sum 기준값 및 저장된 sum 값
private var savedTimerTime: Int = 2400 // timer 기준값 및 저장된 timer 값
private var savedStopwatchTime: Int = 0// stopwath 기준값 및 저장된 stopwatch 값
private var savedGoalTime: Int = 21600 // 저장된 goalTime 값

mutating func updateTask(to taskName: String, fromTime: Int) {
self.recordTask = taskName
self.recordTaskFromTime = fromTime
self.save()
}

mutating func updateMode(to mode: Int) {
self.recordingMode = mode
self.save()
}

mutating func recordStart() {
self.recordStartAt = Date()
self.recording = true
self.save()
}

mutating func recordStop(finishAt: Date) {
self.recording = false
self.savedSumTime += self.interval(to: finishAt)
self.savedGoalTime = self.settedGoalTime - self.savedSumTime
switch self.recordingMode {
case 1: self.savedTimerTime -= self.interval(to: finishAt)
case 2: self.savedStopwatchTime += self.interval(to: finishAt)
default: break
}
self.save()
}

mutating func updateTimerTime(to timerTime: Int) {
self.settedTimerTime = timerTime
self.save()
}

mutating func updateGoalTime(to goalTime: Int) {
self.settedGoalTime = goalTime
self.save()
}

mutating func load() {
guard let savedRecordTimes = Storage.retrive(RecordTimes.fileName, from: .documents, as: RecordTimes.self) else {
self = RecordTimes()
self.recordTask = UserDefaults.standard.value(forKey: "task") as? String ?? "none"
self.settedGoalTime = UserDefaults.standard.value(forKey: "allTime") as? Int ?? 21600
self.settedTimerTime = UserDefaults.standard.value(forKey: "second") as? Int ?? 2400
self.savedSumTime = UserDefaults.standard.value(forKey: "sum2") as? Int ?? 0
self.savedTimerTime = UserDefaults.standard.value(forKey: "second2") as? Int ?? 2400
self.savedStopwatchTime = UserDefaults.standard.value(forKey: "sumTime_temp") as? Int ?? 0
self.savedGoalTime = UserDefaults.standard.value(forKey: "allTime2") as? Int ?? 21600
return
}

self = savedRecordTimes
}

private func save() {
Storage.store(self, to: .documents, as: RecordTimes.fileName)
// MARK: network 상에 반영한다면?
}

func interval(to: Date) -> Int {
return Self.interval(from: self.recordStartAt, to: to)
}

func currentTimes() -> Times { // VC 에서 매초
guard self.recording else {
return Times(self.savedSumTime, self.savedTimerTime, self.savedStopwatchTime, self.savedGoalTime)
}

let currentAt = Date()
let currentSum = self.savedSumTime + self.interval(to: currentAt)
let currentGoal = self.settedGoalTime - currentSum
let currentTimer = self.savedTimerTime - self.interval(to: currentAt)
let currentStopwatch = self.savedStopwatchTime + self.interval(to: currentAt)
return Times(sum: currentSum, timer: currentTimer, stopwatch: currentStopwatch, goal: currentGoal)
}
}

extension RecordTimes {
static func interval(from: Date, to: Date) -> Int {
let timeComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: from, to: to)
return (timeComponents.hour ?? 0)*3600 + (timeComponents.minute ?? 0)*60 + (timeComponents.second ?? 0)
}
}
40 changes: 0 additions & 40 deletions Project_Timer/Global/Model/Time.swift

This file was deleted.

16 changes: 8 additions & 8 deletions Project_Timer/Stopwatch/StopwatchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class StopwatchViewController: UIViewController {
var totalTime: Int = 0
var beforePer2: Float = 0.0
var task: String = ""
var time: Time!
var time: RecordTimes!
//하루 그래프를 위한 구조
var daily = Daily()
var isLandscape: Bool = false
Expand Down Expand Up @@ -111,7 +111,7 @@ final class StopwatchViewController: UIViewController {
}

@objc func timerLogic() {
let seconds = Time.seconds(from: self.time.startDate, to: Date()) // 기록 시작점 ~ 현재까지 지난 초
let seconds = RecordTimes.seconds(from: self.time.startDate, to: Date()) // 기록 시작점 ~ 현재까지 지난 초
self.updateTimes(interval: seconds)
self.daily.updateCurrentTaskTime(interval: seconds)
self.daily.updateMaxTime(with: seconds)
Expand All @@ -124,9 +124,9 @@ final class StopwatchViewController: UIViewController {

private func updateTimes(interval: Int) {
// time 값을 기준으로 interval 만큼 지난 시간을 계산하여 표시
self.currentSumTime = self.time.fromSumTime + interval
self.currentStopwatchTime = self.time.fromStopwatchTime + interval
self.currentGoalTime = self.time.fromGoalTime - interval
self.currentSumTime = self.time.savedSumTime + interval
self.currentStopwatchTime = self.time.savedStopwatchTime + interval
self.currentGoalTime = self.time.goalTime - interval
}

@IBAction func taskBTAction(_ sender: Any) {
Expand Down Expand Up @@ -266,7 +266,7 @@ extension StopwatchViewController {

func refresh (hours: Int, mins: Int, secs: Int, start: Date) {
print("refresh")
let seconds = Time.seconds(from: self.time.startDate, to: Date())
let seconds = RecordTimes.seconds(from: self.time.startDate, to: Date())
self.updateTimes(interval: seconds)
self.daily.updateCurrentTaskTimeOfBackground(startAt: self.time.startDate, interval: seconds)
self.daily.updateMaxTime(with: seconds)
Expand Down Expand Up @@ -571,7 +571,7 @@ extension StopwatchViewController {
self.setColorsOfStart()
self.updateProgress()
// MARK: - init 은 정상적일 경우에만 하도록 개선 예정
self.time = Time(goal: self.currentGoalTime, sum: self.currentSumTime, stopwatch: self.currentStopwatchTime)
self.time = RecordTimes(goal: self.currentGoalTime, sum: self.currentSumTime, stopwatch: self.currentStopwatchTime)
self.startTimer()
self.setButtonsEnabledFalse()
self.finishTimeLabel.text = getFutureTime()
Expand All @@ -590,7 +590,7 @@ extension StopwatchViewController {

stopColor()
setButtonsEnabledTrue()
time.fromStopwatchTime = currentStopwatchTime //기준시간 저장
time.savedStopwatchTime = currentStopwatchTime //기준시간 저장
daily.save() //하루 그래프 데이터 계산
//dailys 저장
dailyViewModel.addDaily(daily)
Expand Down
14 changes: 7 additions & 7 deletions Project_Timer/Timer/TimerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TimerViewController: UIViewController {
var array_time = [String](repeating: "", count: 7)
var totalTime: Int = 0
var beforePer2: Float = 0.0
var time: Time!
var time: RecordTimes!
var task: String = ""
//하루 그래프를 위한 구조
var daily = Daily()
Expand Down Expand Up @@ -121,7 +121,7 @@ class TimerViewController: UIViewController {
self.outterProgress.progressColor = RED!
}

let seconds = Time.seconds(from: self.time.startDate, to: Date())
let seconds = RecordTimes.seconds(from: self.time.startDate, to: Date())
self.updateTimes(interval: seconds)
self.daily.updateCurrentTaskTime(interval: seconds)
self.daily.updateMaxTime(with: seconds)
Expand All @@ -141,9 +141,9 @@ class TimerViewController: UIViewController {

private func updateTimes(interval: Int) {
// time 값을 기준으로 interval 만큼 지난 시간을 계산하여 표시
self.currentSumTime = self.time.fromSumTime + interval
self.currentTimerTime = self.time.fromTimerTime - interval
self.currentGoalTime = self.time.fromGoalTime - interval
self.currentSumTime = self.time.savedSumTime + interval
self.currentTimerTime = self.time.savedTimerTime - interval
self.currentGoalTime = self.time.goalTime - interval
}

@IBAction func taskBTAction(_ sender: Any) {
Expand Down Expand Up @@ -289,7 +289,7 @@ extension TimerViewController {

func refresh (hours: Int, mins: Int, secs: Int, start: Date) {
print("refresh")
let seconds = Time.seconds(from: self.time.startDate, to: Date())
let seconds = RecordTimes.seconds(from: self.time.startDate, to: Date())
self.updateTimes(interval: seconds)
self.daily.updateCurrentTaskTimeOfBackground(startAt: self.time.startDate, interval: seconds)
self.daily.updateMaxTime(with: seconds)
Expand Down Expand Up @@ -637,7 +637,7 @@ extension TimerViewController {
timerStopped = false
checkReset()
// MARK: init 은 정상적일 경우에만 하도록 개선 예정
self.time = Time(goal: self.currentGoalTime, sum: self.currentSumTime, timer: self.currentTimerTime)
self.time = RecordTimes(goal: self.currentGoalTime, sum: self.currentSumTime, timer: self.currentTimerTime)
self.startTimer()
self.setButtonsEnabledFalse()
finishTimeLabel.text = getFutureTime()
Expand Down

0 comments on commit 497c913

Please sign in to comment.