Skip to content

Commit

Permalink
Put the current user at the top of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Nov 22, 2024
1 parent 1d98366 commit 4889813
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
46 changes: 39 additions & 7 deletions WordPress/Classes/Users/ViewModel/UserListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,40 @@ import WordPressShared
@MainActor
class UserListViewModel: ObservableObject {

enum RoleSection: Hashable, Comparable {
case me
case role(String)
case searchResult

/// Order in the users list.
static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
// The current user section and the search result section always at the top.
case (.me, _), (.searchResult, _):
return true
case (_, .me), (_, .searchResult):
return false

case let (.role(lhs), .role(rhs)):
return lhs < rhs
}
}
}

struct Section: Identifiable {
var id: String { role }
let role: String
var id: RoleSection
let users: [DisplayUser]

var headerText: String {
switch id {
case .me:
return ""
case let .role(role):
return role
case .searchResult:
return NSLocalizedString("userList.searchResults.header", value: "Search Results", comment: "Header text fo the search results section in the users list")
}
}
}

/// The initial set of users fetched by `fetchItems`
Expand All @@ -19,6 +49,7 @@ class UserListViewModel: ObservableObject {
}
private var updateUsersTask: Task<Void, Never>?
private let userService: UserServiceProtocol
private let currentUserId: Int32
private var initialLoad = false

@Published
Expand All @@ -37,13 +68,14 @@ class UserListViewModel: ObservableObject {
setSearchResults(sortUsers(users))
} else {
let searchResults = users.search(searchTerm, using: \.searchString)
setSearchResults([Section(role: "Search Results", users: searchResults)])
setSearchResults([Section(id: .searchResult, users: searchResults)])
}
}
}

init(userService: UserServiceProtocol) {
init(userService: UserServiceProtocol, currentUserId: Int32) {
self.userService = userService
self.currentUserId = currentUserId
}

deinit {
Expand Down Expand Up @@ -94,8 +126,8 @@ class UserListViewModel: ObservableObject {
}

private func sortUsers(_ users: [DisplayUser]) -> [Section] {
Dictionary(grouping: users, by: { $0.role })
.map { Section(role: $0.key, users: $0.value.sorted(by: { $0.username < $1.username })) }
.sorted { $0.role < $1.role }
Dictionary(grouping: users) { $0.id == currentUserId ? RoleSection.me : RoleSection.role($0.role) }
.map { Section(id: $0.key, users: $0.value.sorted(by: { $0.username < $1.username })) }
.sorted { $0.id < $1.id }
}
}
4 changes: 2 additions & 2 deletions WordPress/Classes/Users/Views/UserListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct UserListView: View {
public init(userService: UserServiceProtocol, applicationTokenListDataProvider: ApplicationTokenListDataProvider) {
self.userService = userService
self.applicationTokenListDataProvider = applicationTokenListDataProvider
_viewModel = StateObject(wrappedValue: UserListViewModel(userService: userService))
_viewModel = StateObject(wrappedValue: UserListViewModel(userService: userService, currentUserId: currentUserId))
}

public var body: some View {
Expand All @@ -26,7 +26,7 @@ public struct UserListView: View {
ProgressView()
} else {
List(viewModel.sortedUsers) { section in
Section(section.role) {
Section(section.headerText) {
if section.users.isEmpty {
Text(Strings.noUsersFound)
.font(.body)
Expand Down

0 comments on commit 4889813

Please sign in to comment.