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")
}
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
}
}

View File

@ -399,8 +399,8 @@ public extension ContentDatabase {
.eraseToAnyPublisher()
}
func setLastReadId(_ id: String, markerTimeline: Marker.Timeline) -> AnyPublisher<Never, Error> {
databaseWriter.writePublisher(updates: LastReadIdRecord(markerTimeline: markerTimeline, id: id).save)
func setLastReadId(_ id: String, timelineId: Timeline.Id) -> AnyPublisher<Never, Error> {
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) {

View File

@ -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)
}
}

View File

@ -12,7 +12,7 @@ public protocol CollectionService {
var title: AnyPublisher<String, Never> { 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<Never, Error>
}
@ -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 }
}

View File

@ -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<Never, Error> {
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<Never, Error> {
contentDatabase.setLastReadId(id, timelineId: timeline.id)
}
func identityPublisher(immediate: Bool) -> AnyPublisher<Identity, Error> {

View File

@ -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<Never, Error> {
mastodonAPIClient.pagedRequest(timeline.endpoint, maxId: maxId, minId: minId)
.handleEvents(receiveOutput: {

View File

@ -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
}
}

View File

@ -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 }

View File

@ -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"