From a21e597339ad89b0f133d5ce2459cb31af43c8c3 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Mon, 8 Feb 2021 12:33:51 -0800 Subject: [PATCH] Refactoring --- .../DB/Content/ContentDatabase+Migration.swift | 6 ++++++ DB/Sources/DB/Content/ContentDatabase.swift | 11 +++++------ DB/Sources/DB/Content/LastReadIdRecord.swift | 4 ++-- .../ServiceLayer/Services/CollectionService.swift | 4 ++-- .../ServiceLayer/Services/IdentityService.swift | 15 +++++---------- .../ServiceLayer/Services/TimelineService.swift | 11 ++--------- .../ServiceLayer/Utilities/AppPreferences.swift | 10 +++++----- .../View Models/CollectionItemsViewModel.swift | 12 ++++++------ Views/SwiftUI/PreferencesView.swift | 2 +- 9 files changed, 34 insertions(+), 41 deletions(-) diff --git a/DB/Sources/DB/Content/ContentDatabase+Migration.swift b/DB/Sources/DB/Content/ContentDatabase+Migration.swift index a0e3f24..b5f46ce 100644 --- a/DB/Sources/DB/Content/ContentDatabase+Migration.swift +++ b/DB/Sources/DB/Content/ContentDatabase+Migration.swift @@ -284,6 +284,12 @@ extension ContentDatabase { try db.rename(table: "new_accountListJoin", to: "accountListJoin") } + migrator.registerMigration("1.0.0-lridr-column-rename") { db in + try db.alter(table: "lastReadIdRecord") { t in + t.rename(column: "markerTimeline", to: "timelineId") + } + } + return migrator } } diff --git a/DB/Sources/DB/Content/ContentDatabase.swift b/DB/Sources/DB/Content/ContentDatabase.swift index c8280f2..ba9cc3f 100644 --- a/DB/Sources/DB/Content/ContentDatabase.swift +++ b/DB/Sources/DB/Content/ContentDatabase.swift @@ -399,8 +399,8 @@ public extension ContentDatabase { .eraseToAnyPublisher() } - func setLastReadId(_ id: String, markerTimeline: Marker.Timeline) -> AnyPublisher { - databaseWriter.writePublisher(updates: LastReadIdRecord(markerTimeline: markerTimeline, id: id).save) + func setLastReadId(_ id: String, timelineId: Timeline.Id) -> AnyPublisher { + databaseWriter.writePublisher(updates: LastReadIdRecord(timelineId: timelineId, id: id).save) .ignoreOutput() .eraseToAnyPublisher() } @@ -685,11 +685,11 @@ public extension ContentDatabase { .eraseToAnyPublisher() } - func lastReadId(_ markerTimeline: Marker.Timeline) -> String? { + func lastReadId(timelineId: Timeline.Id) -> String? { try? databaseWriter.read { try String.fetchOne( $0, - LastReadIdRecord.filter(LastReadIdRecord.Columns.markerTimeline == markerTimeline.rawValue) + LastReadIdRecord.filter(LastReadIdRecord.Columns.timelineId == timelineId) .select(LastReadIdRecord.Columns.id)) } } @@ -704,7 +704,6 @@ private extension ContentDatabase { try FileManager.default.databaseDirectoryURL(name: id.uuidString, appGroup: appGroup) } - // swiftlint:disable:next function_body_length static func clean(_ databaseWriter: DatabaseWriter, useHomeTimelineLastReadId: Bool) throws { try databaseWriter.write { @@ -723,7 +722,7 @@ private extension ContentDatabase { if let lastReadId = try Status.Id.fetchOne( $0, - LastReadIdRecord.filter(LastReadIdRecord.Columns.markerTimeline == Marker.Timeline.home.rawValue) + LastReadIdRecord.filter(LastReadIdRecord.Columns.timelineId == Timeline.home.id) .select(LastReadIdRecord.Columns.id)) ?? statusIds.first, let index = statusIds.firstIndex(of: lastReadId) { diff --git a/DB/Sources/DB/Content/LastReadIdRecord.swift b/DB/Sources/DB/Content/LastReadIdRecord.swift index 0294a35..80529cc 100644 --- a/DB/Sources/DB/Content/LastReadIdRecord.swift +++ b/DB/Sources/DB/Content/LastReadIdRecord.swift @@ -5,13 +5,13 @@ import GRDB import Mastodon struct LastReadIdRecord: ContentDatabaseRecord, Hashable { - let markerTimeline: Marker.Timeline + let timelineId: Timeline.Id let id: String } extension LastReadIdRecord { enum Columns { - static let markerTimeline = Column(CodingKeys.markerTimeline) + static let timelineId = Column(CodingKeys.timelineId) static let id = Column(CodingKeys.id) } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift b/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift index e6bf355..5c5d88b 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/CollectionService.swift @@ -12,7 +12,7 @@ public protocol CollectionService { var title: AnyPublisher { get } var titleLocalizationComponents: AnyPublisher<[String], Never> { get } var navigationService: NavigationService { get } - var markerTimeline: Marker.Timeline? { get } + var positionTimeline: Timeline? { get } func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher } @@ -29,5 +29,5 @@ extension CollectionService { public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() } - public var markerTimeline: Marker.Timeline? { nil } + public var positionTimeline: Timeline? { nil } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift index cb398f6..c0daa6e 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift @@ -33,7 +33,7 @@ public struct IdentityService { contentDatabase = try ContentDatabase( id: id, - useHomeTimelineLastReadId: appPreferences.homeTimelineBehavior == .rememberPosition, + useHomeTimelineLastReadId: appPreferences.homeTimelineBehavior == .localRememberPosition, inMemory: environment.inMemoryContent, appGroup: AppEnvironment.appGroup, keychain: environment.keychain) @@ -125,17 +125,12 @@ public extension IdentityService { .eraseToAnyPublisher() } - func getLocalLastReadId(_ markerTimeline: Marker.Timeline) -> String? { - contentDatabase.lastReadId(markerTimeline) + func getLocalLastReadId(timeline: Timeline) -> String? { + contentDatabase.lastReadId(timelineId: timeline.id) } - func setLastReadId(_ id: String, forMarker markerTimeline: Marker.Timeline) -> AnyPublisher { - switch AppPreferences(environment: environment).positionBehavior(markerTimeline: markerTimeline) { - case .rememberPosition: - return contentDatabase.setLastReadId(id, markerTimeline: markerTimeline) - case .newest: - return Empty().eraseToAnyPublisher() - } + func setLocalLastReadId(_ id: String, timeline: Timeline) -> AnyPublisher { + contentDatabase.setLastReadId(id, timelineId: timeline.id) } func identityPublisher(immediate: Bool) -> AnyPublisher { diff --git a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift index a0f7a7d..983ae00 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift @@ -50,19 +50,12 @@ public struct TimelineService { } extension TimelineService: CollectionService { + public var positionTimeline: Timeline? { timeline } + public var preferLastPresentIdOverNextPageMaxId: Bool { !timeline.ordered } - public var markerTimeline: Marker.Timeline? { - switch timeline { - case .home: - return .home - default: - return nil - } - } - public func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher { mastodonAPIClient.pagedRequest(timeline.endpoint, maxId: maxId, minId: minId) .handleEvents(receiveOutput: { diff --git a/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift b/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift index ce59cd1..1e1f1f8 100644 --- a/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift +++ b/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift @@ -39,7 +39,7 @@ public extension AppPreferences { } enum PositionBehavior: String, CaseIterable, Identifiable { - case rememberPosition + case localRememberPosition case newest public var id: String { rawValue } @@ -110,7 +110,7 @@ public extension AppPreferences { return value } - return .rememberPosition + return .localRememberPosition } set { self[.homeTimelineBehavior] = newValue.rawValue } } @@ -140,11 +140,11 @@ public extension AppPreferences { systemReduceMotion() && useSystemReduceMotionForMedia } - func positionBehavior(markerTimeline: Marker.Timeline) -> PositionBehavior { - switch markerTimeline { + func positionBehavior(timeline: Timeline) -> PositionBehavior { + switch timeline { case .home: return homeTimelineBehavior - case .notifications: + default: return .newest } } diff --git a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift index b840bd5..5158773 100644 --- a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift @@ -49,15 +49,15 @@ public class CollectionItemsViewModel: ObservableObject { .sink { _ in } .store(in: &cancellables) - if let markerTimeline = collectionService.markerTimeline { - if identityContext.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition { - markerScrollPositionItemId = identityContext.service.getLocalLastReadId(markerTimeline) + if let timeline = collectionService.positionTimeline { + if identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition { + markerScrollPositionItemId = identityContext.service.getLocalLastReadId(timeline: timeline) } lastReadId.compactMap { $0 } .removeDuplicates() .debounce(for: .seconds(Self.lastReadIdDebounceInterval), scheduler: DispatchQueue.global()) - .flatMap { identityContext.service.setLastReadId($0, forMarker: markerTimeline) } + .flatMap { identityContext.service.setLocalLastReadId($0, timeline: timeline) } .sink { _ in } receiveValue: { _ in } .store(in: &cancellables) } @@ -351,8 +351,8 @@ private extension CollectionItemsViewModel { func realMaxId(maxId: String?) -> String? { guard let maxId = maxId else { return nil } - guard let markerTimeline = collectionService.markerTimeline, - identityContext.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition, + guard let timeline = collectionService.positionTimeline, + identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition, let lastItemId = lastUpdate.sections.last?.items.last?.itemId else { return maxId } diff --git a/Views/SwiftUI/PreferencesView.swift b/Views/SwiftUI/PreferencesView.swift index 19814e5..5975956 100644 --- a/Views/SwiftUI/PreferencesView.swift +++ b/Views/SwiftUI/PreferencesView.swift @@ -175,7 +175,7 @@ extension AppPreferences.Autoplay { extension AppPreferences.PositionBehavior { var localizedStringKey: LocalizedStringKey { switch self { - case .rememberPosition: + case .localRememberPosition: return "preferences.position.remember-position" case .newest: return "preferences.position.newest"