Migrate LocalTimeline to SwiftData
This commit is contained in:
parent
cb1b5b69df
commit
527d982dce
|
@ -124,6 +124,7 @@ extension View {
|
|||
func withModelContainer() -> some View {
|
||||
modelContainer(for: [
|
||||
Draft.self,
|
||||
LocalTimeline.self,
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,10 +8,12 @@ import Network
|
|||
import Nuke
|
||||
import SwiftUI
|
||||
import Timeline
|
||||
import SwiftData
|
||||
|
||||
@MainActor
|
||||
struct SettingsTabs: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(\.modelContext) private var context
|
||||
|
||||
@Environment(PushNotificationsService.self) private var pushNotifications
|
||||
@Environment(UserPreferences.self) private var preferences
|
||||
|
@ -27,6 +29,8 @@ struct SettingsTabs: View {
|
|||
@State private var timelineCache = TimelineCache()
|
||||
|
||||
@Binding var popToRootTab: Tab
|
||||
|
||||
@Query(sort: \LocalTimeline.creationDate, order: .reverse) var localTimelines: [LocalTimeline]
|
||||
|
||||
var body: some View {
|
||||
NavigationStack(path: $routerPath.path) {
|
||||
|
@ -303,16 +307,13 @@ struct SettingsTabs: View {
|
|||
|
||||
private var remoteLocalTimelinesView: some View {
|
||||
Form {
|
||||
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
|
||||
Text(server)
|
||||
ForEach(localTimelines) { timeline in
|
||||
Text(timeline.instance)
|
||||
}.onDelete { indexes in
|
||||
if let index = indexes.first {
|
||||
_ = preferences.remoteLocalTimelines.remove(at: index)
|
||||
context.delete(localTimelines[index])
|
||||
}
|
||||
}
|
||||
.onMove(perform: { indices, newOffset in
|
||||
moveTimelineItems(from: indices, to: newOffset)
|
||||
})
|
||||
.listRowBackground(theme.primaryBackgroundColor)
|
||||
Button {
|
||||
routerPath.presentedSheet = .addRemoteLocalTimeline
|
||||
|
@ -329,10 +330,6 @@ struct SettingsTabs: View {
|
|||
}
|
||||
}
|
||||
|
||||
private func moveTimelineItems(from source: IndexSet, to destination: Int) {
|
||||
preferences.remoteLocalTimelines.move(fromOffsets: source, toOffset: destination)
|
||||
}
|
||||
|
||||
private var cacheSection: some View {
|
||||
Section("settings.section.cache") {
|
||||
if cachedRemoved {
|
||||
|
|
|
@ -10,6 +10,7 @@ import SwiftUI
|
|||
@MainActor
|
||||
struct AddRemoteTimelineView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(\.modelContext) private var context
|
||||
|
||||
@Environment(UserPreferences.self) private var preferences
|
||||
@Environment(Theme.self) private var theme
|
||||
|
@ -39,7 +40,7 @@ struct AddRemoteTimelineView: View {
|
|||
}
|
||||
Button {
|
||||
guard instance != nil else { return }
|
||||
preferences.remoteLocalTimelines.append(instanceName)
|
||||
context.insert(LocalTimeline(instance: instanceName))
|
||||
dismiss()
|
||||
} label: {
|
||||
Text("timeline.add.action.add")
|
||||
|
|
|
@ -6,9 +6,12 @@ import Models
|
|||
import Network
|
||||
import SwiftUI
|
||||
import Timeline
|
||||
import SwiftData
|
||||
|
||||
@MainActor
|
||||
struct TimelineTab: View {
|
||||
@Environment(\.modelContext) private var context
|
||||
|
||||
@Environment(AppAccountsManager.self) private var appAccount
|
||||
@Environment(Theme.self) private var theme
|
||||
@Environment(CurrentAccount.self) private var currentAccount
|
||||
|
@ -18,17 +21,20 @@ struct TimelineTab: View {
|
|||
@Binding var popToRootTab: Tab
|
||||
|
||||
@State private var didAppear: Bool = false
|
||||
@State private var timeline: TimelineFilter
|
||||
@State private var timeline: TimelineFilter = .home
|
||||
@State private var scrollToTopSignal: Int = 0
|
||||
|
||||
@Query(sort: \LocalTimeline.creationDate, order: .reverse) var localTimelines: [LocalTimeline]
|
||||
|
||||
@AppStorage("last_timeline_filter") public var lastTimelineFilter: TimelineFilter = .home
|
||||
@AppStorage("remote_local_timeline") var legacyLocalTimelines: [String] = []
|
||||
@AppStorage("last_timeline_filter") var lastTimelineFilter: TimelineFilter = .home
|
||||
|
||||
private let canFilterTimeline: Bool
|
||||
|
||||
init(popToRootTab: Binding<Tab>, timeline: TimelineFilter? = nil) {
|
||||
canFilterTimeline = timeline == nil
|
||||
self.timeline = timeline ?? .home
|
||||
_popToRootTab = popToRootTab
|
||||
self.timeline = timeline ?? .home
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
|
@ -43,6 +49,7 @@ struct TimelineTab: View {
|
|||
.id(client.id)
|
||||
}
|
||||
.onAppear {
|
||||
migrateUserPreferencesTimeline()
|
||||
routerPath.client = client
|
||||
if !didAppear, canFilterTimeline {
|
||||
didAppear = true
|
||||
|
@ -129,12 +136,12 @@ struct TimelineTab: View {
|
|||
}
|
||||
|
||||
Menu("timeline.filter.local") {
|
||||
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
|
||||
ForEach(localTimelines) { remoteLocal in
|
||||
Button {
|
||||
timeline = .remoteLocal(server: server, filter: .local)
|
||||
timeline = .remoteLocal(server: remoteLocal.instance, filter: .local)
|
||||
} label: {
|
||||
VStack {
|
||||
Label(server, systemImage: "dot.radiowaves.right")
|
||||
Label(remoteLocal.instance, systemImage: "dot.radiowaves.right")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -235,4 +242,11 @@ struct TimelineTab: View {
|
|||
timeline = .federated
|
||||
}
|
||||
}
|
||||
|
||||
func migrateUserPreferencesTimeline() {
|
||||
for instance in legacyLocalTimelines {
|
||||
context.insert(LocalTimeline(instance: instance))
|
||||
}
|
||||
legacyLocalTimelines = []
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import SwiftUI
|
|||
@MainActor
|
||||
@Observable public class UserPreferences {
|
||||
class Storage {
|
||||
@AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = []
|
||||
@AppStorage("tag_groups") public var tagGroups: [TagGroup] = []
|
||||
@AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari
|
||||
@AppStorage("show_translate_button_inline") public var showTranslateButton: Bool = true
|
||||
|
@ -62,12 +61,7 @@ import SwiftUI
|
|||
private let storage = Storage()
|
||||
|
||||
private var client: Client?
|
||||
|
||||
public var remoteLocalTimelines: [String] {
|
||||
didSet {
|
||||
storage.remoteLocalTimelines = remoteLocalTimelines
|
||||
}
|
||||
}
|
||||
|
||||
public var tagGroups: [TagGroup] {
|
||||
didSet {
|
||||
storage.tagGroups = tagGroups
|
||||
|
@ -371,7 +365,6 @@ import SwiftUI
|
|||
}
|
||||
|
||||
private init() {
|
||||
remoteLocalTimelines = storage.remoteLocalTimelines
|
||||
tagGroups = storage.tagGroups
|
||||
preferredBrowser = storage.preferredBrowser
|
||||
showTranslateButton = storage.showTranslateButton
|
||||
|
|
|
@ -2,7 +2,7 @@ import SwiftData
|
|||
import SwiftUI
|
||||
import Foundation
|
||||
|
||||
@Model public class Draft: Identifiable {
|
||||
@Model public class Draft {
|
||||
@Attribute(.unique) public var id: UUID
|
||||
public var content: String
|
||||
public var creationDate: Date
|
|
@ -0,0 +1,13 @@
|
|||
import SwiftData
|
||||
import SwiftUI
|
||||
import Foundation
|
||||
|
||||
@Model public class LocalTimeline {
|
||||
public var instance: String
|
||||
public var creationDate: Date
|
||||
|
||||
public init(instance: String) {
|
||||
self.instance = instance
|
||||
self.creationDate = Date()
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import SwiftUI
|
||||
import SwiftData
|
||||
import DesignSystem
|
||||
import Models
|
||||
|
||||
struct DraftsListView: View {
|
||||
@AppStorage("draft_posts") public var legacyDraftPosts: [String] = []
|
||||
|
|
Loading…
Reference in New Issue