Fix saving expired filters not reenabling them

Closes #289
This commit is contained in:
Shadowfacts 2022-12-05 19:01:32 -05:00
parent 40ff8d0a2a
commit 4be2258882
3 changed files with 23 additions and 15 deletions

View File

@ -10,6 +10,8 @@ import Foundation
import Pachyderm import Pachyderm
class EditedFilter: ObservableObject { class EditedFilter: ObservableObject {
static let defaultExpiresInForExpired: TimeInterval = 24 * 60 * 60
let id: String? let id: String?
@Published var title: String? @Published var title: String?
@Published var contexts: [FilterV1.Context] @Published var contexts: [FilterV1.Context]
@ -31,8 +33,12 @@ class EditedFilter: ObservableObject {
self.title = mo.title self.title = mo.title
self.contexts = mo.contexts self.contexts = mo.contexts
if let expiresAt = mo.expiresAt { if let expiresAt = mo.expiresAt {
if expiresAt <= Date() {
expiresIn = EditedFilter.defaultExpiresInForExpired
} else {
expiresIn = expiresAt.timeIntervalSinceNow expiresIn = expiresAt.timeIntervalSinceNow
} }
}
self.keywords = mo.keywordMOs.map { self.keywords = mo.keywordMOs.map {
Keyword(id: $0.id, keyword: $0.keyword, wholeWord: $0.wholeWord) Keyword(id: $0.id, keyword: $0.keyword, wholeWord: $0.wholeWord)
} }

View File

@ -27,18 +27,21 @@ struct EditFilterView: View {
return durations.map { .init(value: $0, title: f.string(from: $0)!) } return durations.map { .init(value: $0, title: f.string(from: $0)!) }
}() }()
@ObservedObject var filter: EditedFilter @ObservedObject private var filter: EditedFilter
let create: Bool private let create: Bool
private let originallyExpired: Bool
@EnvironmentObject private var mastodonController: MastodonController @EnvironmentObject private var mastodonController: MastodonController
@Environment(\.dismiss) private var dismiss @Environment(\.dismiss) private var dismiss
@State private var expiresIn: TimeInterval
@State private var originalFilter: EditedFilter @State private var originalFilter: EditedFilter
@State private var edited = false @State private var edited = false
@State private var isSaving = false @State private var isSaving = false
@State private var saveError: (any Error)? @State private var saveError: (any Error)?
init(filter: EditedFilter, create: Bool) { init(filter: EditedFilter, create: Bool, originallyExpired: Bool) {
self.filter = filter self.filter = filter
self.create = create self.create = create
self.originallyExpired = originallyExpired
self._originalFilter = State(wrappedValue: EditedFilter(copying: filter)) self._originalFilter = State(wrappedValue: EditedFilter(copying: filter))
if let expiresIn = filter.expiresIn { if let expiresIn = filter.expiresIn {
self._expiresIn = State(wrappedValue: Self.expiresInOptions.min(by: { a, b in self._expiresIn = State(wrappedValue: Self.expiresInOptions.min(by: { a, b in
@ -47,17 +50,10 @@ struct EditFilterView: View {
return aDist < bDist return aDist < bDist
})!.value) })!.value)
} else { } 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<Bool> { private var expires: Binding<Bool> {
Binding { Binding {
filter.expiresIn != nil filter.expiresIn != nil
@ -158,7 +154,7 @@ struct EditFilterView: View {
Button(create ? "Create" : "Save") { Button(create ? "Create" : "Save") {
saveFilter() saveFilter()
} }
.disabled(!filter.isValid(for: mastodonController) || !edited) .disabled(!filter.isValid(for: mastodonController) || (!edited && originallyExpired))
} }
} }
} }
@ -167,6 +163,12 @@ struct EditFilterView: View {
}, message: { error in }, message: { error in
Text(error.localizedDescription) Text(error.localizedDescription)
}) })
.onChange(of: expiresIn, perform: { newValue in
edited = true
if expires.wrappedValue {
filter.expiresIn = newValue
}
})
.onReceive(filter.objectWillChange, perform: { _ in .onReceive(filter.objectWillChange, perform: { _ in
edited = true edited = true
}) })

View File

@ -51,7 +51,7 @@ struct FiltersList: View {
List { List {
Section { Section {
NavigationLink { NavigationLink {
EditFilterView(filter: EditedFilter(), create: true) EditFilterView(filter: EditedFilter(), create: true, originallyExpired: false)
} label: { } label: {
Label("Add Filter", systemImage: "plus") Label("Add Filter", systemImage: "plus")
.foregroundColor(.accentColor) .foregroundColor(.accentColor)
@ -88,7 +88,7 @@ struct FiltersList: View {
Section { Section {
ForEach(filters, id: \.id) { filter in ForEach(filters, id: \.id) { filter in
NavigationLink { NavigationLink {
EditFilterView(filter: EditedFilter(filter), create: false) EditFilterView(filter: EditedFilter(filter), create: false, originallyExpired: filter.expiresAt != nil && filter.expiresAt! <= Date())
} label: { } label: {
FilterRow(filter: filter) FilterRow(filter: filter)
} }