Refactoring

This commit is contained in:
Justin Mazzocchi 2021-02-08 12:33:51 -08:00
parent 3149fd8edf
commit a21e597339
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
9 changed files with 34 additions and 41 deletions

View file

@ -284,6 +284,12 @@ extension ContentDatabase {
try db.rename(table: "new_accountListJoin", to: "accountListJoin") 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 return migrator
} }
} }

View file

@ -399,8 +399,8 @@ public extension ContentDatabase {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func setLastReadId(_ id: String, markerTimeline: Marker.Timeline) -> AnyPublisher<Never, Error> { func setLastReadId(_ id: String, timelineId: Timeline.Id) -> AnyPublisher<Never, Error> {
databaseWriter.writePublisher(updates: LastReadIdRecord(markerTimeline: markerTimeline, id: id).save) databaseWriter.writePublisher(updates: LastReadIdRecord(timelineId: timelineId, id: id).save)
.ignoreOutput() .ignoreOutput()
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
@ -685,11 +685,11 @@ public extension ContentDatabase {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func lastReadId(_ markerTimeline: Marker.Timeline) -> String? { func lastReadId(timelineId: Timeline.Id) -> String? {
try? databaseWriter.read { try? databaseWriter.read {
try String.fetchOne( try String.fetchOne(
$0, $0,
LastReadIdRecord.filter(LastReadIdRecord.Columns.markerTimeline == markerTimeline.rawValue) LastReadIdRecord.filter(LastReadIdRecord.Columns.timelineId == timelineId)
.select(LastReadIdRecord.Columns.id)) .select(LastReadIdRecord.Columns.id))
} }
} }
@ -704,7 +704,6 @@ private extension ContentDatabase {
try FileManager.default.databaseDirectoryURL(name: id.uuidString, appGroup: appGroup) try FileManager.default.databaseDirectoryURL(name: id.uuidString, appGroup: appGroup)
} }
// swiftlint:disable:next function_body_length
static func clean(_ databaseWriter: DatabaseWriter, static func clean(_ databaseWriter: DatabaseWriter,
useHomeTimelineLastReadId: Bool) throws { useHomeTimelineLastReadId: Bool) throws {
try databaseWriter.write { try databaseWriter.write {
@ -723,7 +722,7 @@ private extension ContentDatabase {
if let lastReadId = try Status.Id.fetchOne( if let lastReadId = try Status.Id.fetchOne(
$0, $0,
LastReadIdRecord.filter(LastReadIdRecord.Columns.markerTimeline == Marker.Timeline.home.rawValue) LastReadIdRecord.filter(LastReadIdRecord.Columns.timelineId == Timeline.home.id)
.select(LastReadIdRecord.Columns.id)) .select(LastReadIdRecord.Columns.id))
?? statusIds.first, ?? statusIds.first,
let index = statusIds.firstIndex(of: lastReadId) { let index = statusIds.firstIndex(of: lastReadId) {

View file

@ -5,13 +5,13 @@ import GRDB
import Mastodon import Mastodon
struct LastReadIdRecord: ContentDatabaseRecord, Hashable { struct LastReadIdRecord: ContentDatabaseRecord, Hashable {
let markerTimeline: Marker.Timeline let timelineId: Timeline.Id
let id: String let id: String
} }
extension LastReadIdRecord { extension LastReadIdRecord {
enum Columns { enum Columns {
static let markerTimeline = Column(CodingKeys.markerTimeline) static let timelineId = Column(CodingKeys.timelineId)
static let id = Column(CodingKeys.id) static let id = Column(CodingKeys.id)
} }
} }

View file

@ -12,7 +12,7 @@ public protocol CollectionService {
var title: AnyPublisher<String, Never> { get } var title: AnyPublisher<String, Never> { get }
var titleLocalizationComponents: AnyPublisher<[String], Never> { get } var titleLocalizationComponents: AnyPublisher<[String], Never> { get }
var navigationService: NavigationService { get } var navigationService: NavigationService { get }
var markerTimeline: Marker.Timeline? { get } var positionTimeline: Timeline? { get }
func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher<Never, Error> func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher<Never, Error>
} }
@ -29,5 +29,5 @@ extension CollectionService {
public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() } public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() }
public var markerTimeline: Marker.Timeline? { nil } public var positionTimeline: Timeline? { nil }
} }

View file

@ -33,7 +33,7 @@ public struct IdentityService {
contentDatabase = try ContentDatabase( contentDatabase = try ContentDatabase(
id: id, id: id,
useHomeTimelineLastReadId: appPreferences.homeTimelineBehavior == .rememberPosition, useHomeTimelineLastReadId: appPreferences.homeTimelineBehavior == .localRememberPosition,
inMemory: environment.inMemoryContent, inMemory: environment.inMemoryContent,
appGroup: AppEnvironment.appGroup, appGroup: AppEnvironment.appGroup,
keychain: environment.keychain) keychain: environment.keychain)
@ -125,17 +125,12 @@ public extension IdentityService {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func getLocalLastReadId(_ markerTimeline: Marker.Timeline) -> String? { func getLocalLastReadId(timeline: Timeline) -> String? {
contentDatabase.lastReadId(markerTimeline) contentDatabase.lastReadId(timelineId: timeline.id)
} }
func setLastReadId(_ id: String, forMarker markerTimeline: Marker.Timeline) -> AnyPublisher<Never, Error> { func setLocalLastReadId(_ id: String, timeline: Timeline) -> AnyPublisher<Never, Error> {
switch AppPreferences(environment: environment).positionBehavior(markerTimeline: markerTimeline) { contentDatabase.setLastReadId(id, timelineId: timeline.id)
case .rememberPosition:
return contentDatabase.setLastReadId(id, markerTimeline: markerTimeline)
case .newest:
return Empty().eraseToAnyPublisher()
}
} }
func identityPublisher(immediate: Bool) -> AnyPublisher<Identity, Error> { func identityPublisher(immediate: Bool) -> AnyPublisher<Identity, Error> {

View file

@ -50,19 +50,12 @@ public struct TimelineService {
} }
extension TimelineService: CollectionService { extension TimelineService: CollectionService {
public var positionTimeline: Timeline? { timeline }
public var preferLastPresentIdOverNextPageMaxId: Bool { public var preferLastPresentIdOverNextPageMaxId: Bool {
!timeline.ordered !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<Never, Error> { public func request(maxId: String?, minId: String?, search: Search?) -> AnyPublisher<Never, Error> {
mastodonAPIClient.pagedRequest(timeline.endpoint, maxId: maxId, minId: minId) mastodonAPIClient.pagedRequest(timeline.endpoint, maxId: maxId, minId: minId)
.handleEvents(receiveOutput: { .handleEvents(receiveOutput: {

View file

@ -39,7 +39,7 @@ public extension AppPreferences {
} }
enum PositionBehavior: String, CaseIterable, Identifiable { enum PositionBehavior: String, CaseIterable, Identifiable {
case rememberPosition case localRememberPosition
case newest case newest
public var id: String { rawValue } public var id: String { rawValue }
@ -110,7 +110,7 @@ public extension AppPreferences {
return value return value
} }
return .rememberPosition return .localRememberPosition
} }
set { self[.homeTimelineBehavior] = newValue.rawValue } set { self[.homeTimelineBehavior] = newValue.rawValue }
} }
@ -140,11 +140,11 @@ public extension AppPreferences {
systemReduceMotion() && useSystemReduceMotionForMedia systemReduceMotion() && useSystemReduceMotionForMedia
} }
func positionBehavior(markerTimeline: Marker.Timeline) -> PositionBehavior { func positionBehavior(timeline: Timeline) -> PositionBehavior {
switch markerTimeline { switch timeline {
case .home: case .home:
return homeTimelineBehavior return homeTimelineBehavior
case .notifications: default:
return .newest return .newest
} }
} }

View file

@ -49,15 +49,15 @@ public class CollectionItemsViewModel: ObservableObject {
.sink { _ in } .sink { _ in }
.store(in: &cancellables) .store(in: &cancellables)
if let markerTimeline = collectionService.markerTimeline { if let timeline = collectionService.positionTimeline {
if identityContext.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition { if identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition {
markerScrollPositionItemId = identityContext.service.getLocalLastReadId(markerTimeline) markerScrollPositionItemId = identityContext.service.getLocalLastReadId(timeline: timeline)
} }
lastReadId.compactMap { $0 } lastReadId.compactMap { $0 }
.removeDuplicates() .removeDuplicates()
.debounce(for: .seconds(Self.lastReadIdDebounceInterval), scheduler: DispatchQueue.global()) .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 } .sink { _ in } receiveValue: { _ in }
.store(in: &cancellables) .store(in: &cancellables)
} }
@ -351,8 +351,8 @@ private extension CollectionItemsViewModel {
func realMaxId(maxId: String?) -> String? { func realMaxId(maxId: String?) -> String? {
guard let maxId = maxId else { return nil } guard let maxId = maxId else { return nil }
guard let markerTimeline = collectionService.markerTimeline, guard let timeline = collectionService.positionTimeline,
identityContext.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition, identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition,
let lastItemId = lastUpdate.sections.last?.items.last?.itemId let lastItemId = lastUpdate.sections.last?.items.last?.itemId
else { return maxId } else { return maxId }

View file

@ -175,7 +175,7 @@ extension AppPreferences.Autoplay {
extension AppPreferences.PositionBehavior { extension AppPreferences.PositionBehavior {
var localizedStringKey: LocalizedStringKey { var localizedStringKey: LocalizedStringKey {
switch self { switch self {
case .rememberPosition: case .localRememberPosition:
return "preferences.position.remember-position" return "preferences.position.remember-position"
case .newest: case .newest:
return "preferences.position.newest" return "preferences.position.newest"