From 7f6e9fb90703587690c3568db1698ee18a9f655a Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Thu, 22 Apr 2021 10:29:53 +0800 Subject: [PATCH] fix: suggestions account order --- Mastodon/Scene/Search/SearchViewModel.swift | 5 +++- .../SuggestionAccountViewModel.swift | 25 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Mastodon/Scene/Search/SearchViewModel.swift b/Mastodon/Scene/Search/SearchViewModel.swift index 04a977e0d..5f1ff5f46 100644 --- a/Mastodon/Scene/Search/SearchViewModel.swift +++ b/Mastodon/Scene/Search/SearchViewModel.swift @@ -320,7 +320,10 @@ final class SearchViewModel: NSObject { } }() if let users = mastodonUsers { - recommendAccounts = users.map(\.objectID) + let sortedUsers = users.sorted { (user1, user2) -> Bool in + (ids.firstIndex(of: user1.id) ?? 0) < (ids.firstIndex(of: user2.id) ?? 0) + } + recommendAccounts = sortedUsers.map(\.objectID) } } diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift index 494b00293..ac8803188 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift @@ -16,6 +16,7 @@ import UIKit protocol SuggestionAccountViewModelDelegate: AnyObject { var homeTimelineNeedRefresh: PassthroughSubject { get } } + final class SuggestionAccountViewModel: NSObject { var disposeBag = Set() @@ -110,20 +111,27 @@ final class SuggestionAccountViewModel: NSObject { } func receiveAccounts(ids: [String]) { - guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return } - let users: [MastodonUser]? = { - let request = MastodonUser.sortedFetchRequest - request.predicate = MastodonUser.predicate(domain: activeMastodonAuthenticationBox.domain, ids: ids) - request.returnsObjectsAsFaults = false + guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { + return + } + let userFetchRequest = MastodonUser.sortedFetchRequest + userFetchRequest.predicate = MastodonUser.predicate(domain: activeMastodonAuthenticationBox.domain, ids: ids) + let mastodonUsers: [MastodonUser]? = { + let userFetchRequest = MastodonUser.sortedFetchRequest + userFetchRequest.predicate = MastodonUser.predicate(domain: activeMastodonAuthenticationBox.domain, ids: ids) + userFetchRequest.returnsObjectsAsFaults = false do { - return try context.managedObjectContext.fetch(request) + return try self.context.managedObjectContext.fetch(userFetchRequest) } catch { assertionFailure(error.localizedDescription) return nil } }() - if let accounts = users?.map(\.objectID) { - self.accounts.value = accounts + if let users = mastodonUsers { + let sortedUsers = users.sorted { (user1, user2) -> Bool in + (ids.firstIndex(of: user1.id) ?? 0) < (ids.firstIndex(of: user2.id) ?? 0) + } + accounts.value = sortedUsers.map(\.objectID) } } @@ -142,7 +150,6 @@ final class SuggestionAccountViewModel: NSObject { os_log("%{public}s[%{public}ld], %{public}s: follow failed. %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription) case .finished: self.delegate?.homeTimelineNeedRefresh.send() - break } } receiveValue: { _ in }