Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-07 15:31:01 -07:00
parent ce3618dcbe
commit a92d95d229
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
2 changed files with 13 additions and 17 deletions

View File

@ -110,13 +110,8 @@ extension IdentityDatabase {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func identitiesObservation() -> AnyPublisher<[Identity], Error> { func identitiesObservation(excluding: String) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking( ValueObservation.tracking(Self.identitiesRequest(excluding: excluding).fetchAll)
StoredIdentity
.including(optional: StoredIdentity.instance)
.including(optional: StoredIdentity.account)
.asRequest(of: IdentityResult.self)
.fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseQueue, scheduling: .immediate) .publisher(in: databaseQueue, scheduling: .immediate)
.map { $0.map(Identity.init(result:)) } .map { $0.map(Identity.init(result:)) }
@ -124,15 +119,7 @@ extension IdentityDatabase {
} }
func recentIdentitiesObservation(excluding: String) -> AnyPublisher<[Identity], Error> { func recentIdentitiesObservation(excluding: String) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking( ValueObservation.tracking(Self.identitiesRequest(excluding: excluding).limit(9).fetchAll)
StoredIdentity
.filter(Column("id") != excluding)
.order(Column("lastUsedAt").desc)
.limit(10)
.including(optional: StoredIdentity.instance)
.including(optional: StoredIdentity.account)
.asRequest(of: IdentityResult.self)
.fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseQueue, scheduling: .immediate) .publisher(in: databaseQueue, scheduling: .immediate)
.map { $0.map(Identity.init(result:)) } .map { $0.map(Identity.init(result:)) }
@ -145,6 +132,15 @@ extension IdentityDatabase {
} }
private extension IdentityDatabase { private extension IdentityDatabase {
private static func identitiesRequest(excluding: String) -> QueryInterfaceRequest<IdentityResult> {
StoredIdentity
.filter(Column("id") != excluding)
.order(Column("lastUsedAt").desc)
.including(optional: StoredIdentity.instance)
.including(optional: StoredIdentity.account)
.asRequest(of: IdentityResult.self)
}
private static func migrate(_ writer: DatabaseWriter) throws { private static func migrate(_ writer: DatabaseWriter) throws {
var migrator = DatabaseMigrator() var migrator = DatabaseMigrator()

View File

@ -69,7 +69,7 @@ extension IdentityRepository {
} }
func identitiesObservation() -> AnyPublisher<[Identity], Error> { func identitiesObservation() -> AnyPublisher<[Identity], Error> {
appEnvironment.identityDatabase.identitiesObservation() appEnvironment.identityDatabase.identitiesObservation(excluding: identity.id)
} }
func recentIdentitiesObservation() -> AnyPublisher<[Identity], Error> { func recentIdentitiesObservation() -> AnyPublisher<[Identity], Error> {