forked from shadowfacts/Tusker
parent
40ff8d0a2a
commit
4be2258882
|
@ -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,8 +33,12 @@ class EditedFilter: ObservableObject {
|
|||
self.title = mo.title
|
||||
self.contexts = mo.contexts
|
||||
if let expiresAt = mo.expiresAt {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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<Bool> {
|
||||
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
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue