Skip to content

Commit

Permalink
Add option to change a Track's start date
Browse files Browse the repository at this point in the history
This does not adjust any Tick offsets, so all Ticks will shift when you change it
  • Loading branch information
skjiisa committed Mar 19, 2021
1 parent 2949dd1 commit 146bf09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Tickmate/Tickmate/Model/TrackRepresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
//

import SwiftUI
import SwiftDate

struct TrackRepresentation: Equatable, Hashable {
var name: String
var color: Color
var multiple: Bool
var reversed: Bool
var systemImage: String?
var startDate: Date

init(name: String = "", color: Color = .black, multiple: Bool = false, reversed: Bool = false, systemImage: String? = nil) {
init(name: String = "", color: Color = .black, multiple: Bool = false, reversed: Bool = false, systemImage: String? = nil, startDate: Date = Date()) {
self.name = name
self.color = color
self.multiple = multiple
self.reversed = reversed
self.systemImage = systemImage
self.startDate = startDate
}

init(name: String = "", red: Int, green: Int, blue: Int, multiple: Bool = false, reversed: Bool = false, systemImage: String? = nil) {
Expand All @@ -28,6 +31,7 @@ struct TrackRepresentation: Equatable, Hashable {
self.multiple = multiple
self.reversed = reversed
self.systemImage = systemImage
self.startDate = Date()
}

var lightText: Bool {
Expand All @@ -45,6 +49,13 @@ struct TrackRepresentation: Equatable, Hashable {
name = track.name == "New Track" ? "" : track.name ?? ""
multiple = track.multiple
reversed = track.reversed
if let startDateString = track.startDate,
// let startDateInRegion = DateInRegion(startDateString, region: .current) {
let startDate = TrackController.iso8601.date(from: startDateString) {
// startDate = startDateInRegion.date
self.startDate = startDate
print(track.startDate, startDate)
}

if let trackImage = track.systemImage {
systemImage = trackImage
Expand All @@ -65,6 +76,7 @@ struct TrackRepresentation: Equatable, Hashable {
track.reversed = reversed
track.systemImage = systemImage
track.color = Int32(color.rgb)
track.startDate = TrackController.iso8601.string(from: startDate)
}

static func == (rep: TrackRepresentation, track: Track) -> Bool {
Expand All @@ -73,7 +85,8 @@ struct TrackRepresentation: Equatable, Hashable {
rep.multiple == track.multiple &&
rep.reversed == track.reversed &&
rep.systemImage == track.systemImage &&
Int32(rep.color.rgb) == track.color
Int32(rep.color.rgb) == track.color &&
TrackController.iso8601.string(from: rep.startDate) == track.startDate
}

static func == (track: Track, rep: TrackRepresentation) -> Bool {
Expand Down
14 changes: 13 additions & 1 deletion Tickmate/Tickmate/Views/TrackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ struct TrackView: View {
+ " Long press to decrease counter.")
}

Toggle(isOn: $draftTrack.reversed) {
Toggle(isOn: $draftTrack.reversed.animation()) {
TextWithCaption(
text: "Reversed",
caption: "Days will be ticked by default."
+ " Tapping a day will untick it."
+ " Good for tracking abstaining from bad habits.")
}

if draftTrack.reversed {
DatePicker(selection: $draftTrack.startDate, displayedComponents: [.date]) {
TextWithCaption(
text: "Start date",
caption: "Days after this will automatically be ticked unless you untick them.")
}
}

ColorPicker("Color", selection: $draftTrack.color, supportsOpacity: false)

NavigationLink(
Expand Down Expand Up @@ -128,7 +136,11 @@ struct TrackView: View {
}

private func save() {
let oldStartDate = track.startDate
draftTrack.save(to: track)
if track.startDate != oldStartDate {
trackController.loadTicks(for: track)
}
PersistenceController.save(context: moc)
}

Expand Down

0 comments on commit 146bf09

Please sign in to comment.