From 4be225888213503ec8be2cdb84b5e15042fa2449 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 5 Dec 2022 19:01:32 -0500 Subject: [PATCH] Fix saving expired filters not reenabling them Closes #289 --- Tusker/Models/EditedFilter.swift | 8 ++++++- Tusker/Screens/Filters/EditFilterView.swift | 26 +++++++++++---------- Tusker/Screens/Filters/FiltersView.swift | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Tusker/Models/EditedFilter.swift b/Tusker/Models/EditedFilter.swift index 2501c7e985..b9a5e38b55 100644 --- a/Tusker/Models/EditedFilter.swift +++ b/Tusker/Models/EditedFilter.swift @@ -10,6 +10,8 @@ import Foundation import Pachyderm class EditedFilter: ObservableObject { + static let defaultExpiresInForExpired: TimeInterval = 24 * 60 * 60 + let id: String? @Published var title: String? @Published var contexts: [FilterV1.Context] @@ -31,7 +33,11 @@ class EditedFilter: ObservableObject { self.title = mo.title self.contexts = mo.contexts if let expiresAt = mo.expiresAt { - expiresIn = expiresAt.timeIntervalSinceNow + if expiresAt <= Date() { + expiresIn = EditedFilter.defaultExpiresInForExpired + } else { + expiresIn = expiresAt.timeIntervalSinceNow + } } self.keywords = mo.keywordMOs.map { Keyword(id: $0.id, keyword: $0.keyword, wholeWord: $0.wholeWord) diff --git a/Tusker/Screens/Filters/EditFilterView.swift b/Tusker/Screens/Filters/EditFilterView.swift index 6833580b78..a41d242364 100644 --- a/Tusker/Screens/Filters/EditFilterView.swift +++ b/Tusker/Screens/Filters/EditFilterView.swift @@ -27,18 +27,21 @@ struct EditFilterView: View { return durations.map { .init(value: $0, title: f.string(from: $0)!) } }() - @ObservedObject var filter: EditedFilter - let create: Bool + @ObservedObject private var filter: EditedFilter + private let create: Bool + private let originallyExpired: Bool @EnvironmentObject private var mastodonController: MastodonController @Environment(\.dismiss) private var dismiss + @State private var expiresIn: TimeInterval @State private var originalFilter: EditedFilter @State private var edited = false @State private var isSaving = false @State private var saveError: (any Error)? - init(filter: EditedFilter, create: Bool) { + init(filter: EditedFilter, create: Bool, originallyExpired: Bool) { self.filter = filter self.create = create + self.originallyExpired = originallyExpired self._originalFilter = State(wrappedValue: EditedFilter(copying: filter)) if let expiresIn = filter.expiresIn { self._expiresIn = State(wrappedValue: Self.expiresInOptions.min(by: { a, b in @@ -47,17 +50,10 @@ struct EditFilterView: View { return aDist < bDist })!.value) } else { - self._expiresIn = State(wrappedValue: 24 * 60 * 60) + self._expiresIn = State(wrappedValue: EditedFilter.defaultExpiresInForExpired) } } - @State private var expiresIn: TimeInterval { - didSet { - if expires.wrappedValue { - filter.expiresIn = expiresIn - } - } - } private var expires: Binding { Binding { filter.expiresIn != nil @@ -158,7 +154,7 @@ struct EditFilterView: View { Button(create ? "Create" : "Save") { saveFilter() } - .disabled(!filter.isValid(for: mastodonController) || !edited) + .disabled(!filter.isValid(for: mastodonController) || (!edited && originallyExpired)) } } } @@ -167,6 +163,12 @@ struct EditFilterView: View { }, message: { error in Text(error.localizedDescription) }) + .onChange(of: expiresIn, perform: { newValue in + edited = true + if expires.wrappedValue { + filter.expiresIn = newValue + } + }) .onReceive(filter.objectWillChange, perform: { _ in edited = true }) diff --git a/Tusker/Screens/Filters/FiltersView.swift b/Tusker/Screens/Filters/FiltersView.swift index 498fa60d25..1552107b4f 100644 --- a/Tusker/Screens/Filters/FiltersView.swift +++ b/Tusker/Screens/Filters/FiltersView.swift @@ -51,7 +51,7 @@ struct FiltersList: View { List { Section { NavigationLink { - EditFilterView(filter: EditedFilter(), create: true) + EditFilterView(filter: EditedFilter(), create: true, originallyExpired: false) } label: { Label("Add Filter", systemImage: "plus") .foregroundColor(.accentColor) @@ -88,7 +88,7 @@ struct FiltersList: View { Section { ForEach(filters, id: \.id) { filter in NavigationLink { - EditFilterView(filter: EditedFilter(filter), create: false) + EditFilterView(filter: EditedFilter(filter), create: false, originallyExpired: filter.expiresAt != nil && filter.expiresAt! <= Date()) } label: { FilterRow(filter: filter) }