mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 08:15:24 +00:00
Stop blocking on loading the current user display name and avatar. Improve perceived app startup times.
This commit is contained in:
@@ -54,11 +54,7 @@ final class HomeScreenCoordinator: Coordinator, Presentable {
|
|||||||
init(parameters: HomeScreenCoordinatorParameters) {
|
init(parameters: HomeScreenCoordinatorParameters) {
|
||||||
self.parameters = parameters
|
self.parameters = parameters
|
||||||
|
|
||||||
let userDisplayName = parameters.userSession.userDisplayName ?? parameters.userSession.userIdentifier
|
viewModel = HomeScreenViewModel(attributedStringBuilder: parameters.attributedStringBuilder)
|
||||||
viewModel = HomeScreenViewModel(userDisplayName: userDisplayName,
|
|
||||||
userAvatarURL: parameters.userSession.userAvatarURL,
|
|
||||||
mediaProvider: parameters.mediaProvider,
|
|
||||||
attributedStringBuilder: parameters.attributedStringBuilder)
|
|
||||||
|
|
||||||
let view = HomeScreen(context: viewModel.context)
|
let view = HomeScreen(context: viewModel.context)
|
||||||
hostingController = UIHostingController(rootView: view)
|
hostingController = UIHostingController(rootView: view)
|
||||||
@@ -82,6 +78,22 @@ final class HomeScreenCoordinator: Coordinator, Presentable {
|
|||||||
}.store(in: &cancellables)
|
}.store(in: &cancellables)
|
||||||
|
|
||||||
updateRoomsList()
|
updateRoomsList()
|
||||||
|
|
||||||
|
parameters.userSession.userAvatarURL { [weak self] result in
|
||||||
|
if case let .success(avatarURL) = result {
|
||||||
|
self?.parameters.mediaProvider.loadImageFromURL(avatarURL) { result in
|
||||||
|
if case let .success(avatar) = result {
|
||||||
|
self?.viewModel.updateWithUserAvatar(avatar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters.userSession.userDisplayName { [weak self] result in
|
||||||
|
if case let .success(displayName) = result {
|
||||||
|
self?.viewModel.updateWithUserDisplayName(displayName)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Public
|
// MARK: - Public
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ enum HomeScreenViewAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct HomeScreenViewState: BindableState {
|
struct HomeScreenViewState: BindableState {
|
||||||
let userDisplayName: String
|
var userDisplayName: String?
|
||||||
var userAvatar: UIImage?
|
var userAvatar: UIImage?
|
||||||
|
|
||||||
var rooms: [HomeScreenRoom] = []
|
var rooms: [HomeScreenRoom] = []
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ typealias HomeScreenViewModelType = StateStoreViewModel<HomeScreenViewState,
|
|||||||
@available(iOS 14, *)
|
@available(iOS 14, *)
|
||||||
class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol {
|
class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol {
|
||||||
|
|
||||||
private let mediaProvider: MediaProviderProtocol
|
|
||||||
private let attributedStringBuilder: AttributedStringBuilderProtocol
|
private let attributedStringBuilder: AttributedStringBuilderProtocol
|
||||||
|
|
||||||
private var roomUpdateListeners = Set<AnyCancellable>()
|
private var roomUpdateListeners = Set<AnyCancellable>()
|
||||||
@@ -34,27 +33,15 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
|
|||||||
self.state.isLoadingRooms = (roomList?.count ?? 0 == 0)
|
self.state.isLoadingRooms = (roomList?.count ?? 0 == 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var completion: ((HomeScreenViewModelResult) -> Void)?
|
var completion: ((HomeScreenViewModelResult) -> Void)?
|
||||||
|
|
||||||
// MARK: - Setup
|
// MARK: - Setup
|
||||||
|
|
||||||
init(userDisplayName: String,
|
init(attributedStringBuilder: AttributedStringBuilderProtocol) {
|
||||||
userAvatarURL: String?,
|
|
||||||
mediaProvider: MediaProviderProtocol,
|
|
||||||
attributedStringBuilder: AttributedStringBuilderProtocol) {
|
|
||||||
self.mediaProvider = mediaProvider
|
|
||||||
self.attributedStringBuilder = attributedStringBuilder
|
self.attributedStringBuilder = attributedStringBuilder
|
||||||
|
|
||||||
super.init(initialViewState: HomeScreenViewState(userDisplayName: userDisplayName, isLoadingRooms: true))
|
super.init(initialViewState: HomeScreenViewState(isLoadingRooms: true))
|
||||||
|
|
||||||
if let userAvatarURL = userAvatarURL {
|
|
||||||
mediaProvider.loadImageFromURL(userAvatarURL) { [weak self] result in
|
|
||||||
if case let .success(avatar) = result {
|
|
||||||
self?.state.userAvatar = avatar
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Public
|
// MARK: - Public
|
||||||
@@ -97,10 +84,14 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateWithUserAvatar(_ avatar: UIImage?) {
|
func updateWithUserAvatar(_ avatar: UIImage) {
|
||||||
self.state.userAvatar = avatar
|
self.state.userAvatar = avatar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateWithUserDisplayName(_ displayName: String) {
|
||||||
|
self.state.userDisplayName = displayName
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
||||||
private func loadRoomDataForIdentifier(_ roomIdentifier: String) {
|
private func loadRoomDataForIdentifier(_ roomIdentifier: String) {
|
||||||
|
|||||||
@@ -22,5 +22,7 @@ protocol HomeScreenViewModelProtocol {
|
|||||||
|
|
||||||
var context: HomeScreenViewModelType.Context { get }
|
var context: HomeScreenViewModelType.Context { get }
|
||||||
|
|
||||||
|
func updateWithUserAvatar(_ avatar: UIImage)
|
||||||
|
func updateWithUserDisplayName(_ displayName: String)
|
||||||
func updateWithRoomList(_ roomList: [RoomSummaryProtocol])
|
func updateWithRoomList(_ roomList: [RoomSummaryProtocol])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,18 +73,32 @@ struct HomeScreen: View {
|
|||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem(placement: .navigationBarLeading) {
|
ToolbarItem(placement: .navigationBarLeading) {
|
||||||
HStack {
|
HStack {
|
||||||
if let avatar = context.viewState.userAvatar {
|
ZStack {
|
||||||
Image(uiImage: avatar)
|
if let avatar = context.viewState.userAvatar {
|
||||||
.resizable()
|
Image(uiImage: avatar)
|
||||||
.scaledToFill()
|
.resizable()
|
||||||
.frame(width: 40, height: 40, alignment: .center)
|
.scaledToFill()
|
||||||
.mask(Circle())
|
.frame(width: 40, height: 40, alignment: .center)
|
||||||
|
.mask(Circle())
|
||||||
|
} else {
|
||||||
|
EmptyView()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Text("Hello, \(context.viewState.userDisplayName)!")
|
.animation(.easeInOut, value: context.viewState.userAvatar)
|
||||||
.font(.subheadline)
|
.transition(.opacity)
|
||||||
.fontWeight(.bold)
|
|
||||||
|
ZStack {
|
||||||
|
if let displayName = context.viewState.userDisplayName {
|
||||||
|
Text("Hello, \(displayName)!")
|
||||||
|
.font(.subheadline)
|
||||||
|
.fontWeight(.bold)
|
||||||
|
} else {
|
||||||
|
EmptyView()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.animation(.easeInOut, value: context.viewState.userDisplayName)
|
||||||
|
.transition(.opacity)
|
||||||
}
|
}
|
||||||
.animation(.easeInOut, value: context.viewState.userAvatar)
|
|
||||||
}
|
}
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
Button("Logout") {
|
Button("Logout") {
|
||||||
@@ -153,10 +167,7 @@ struct RoomCell: View {
|
|||||||
|
|
||||||
struct HomeScreen_Previews: PreviewProvider {
|
struct HomeScreen_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
let viewModel = HomeScreenViewModel(userDisplayName: "Johnny Appleseed",
|
let viewModel = HomeScreenViewModel(attributedStringBuilder: AttributedStringBuilder())
|
||||||
userAvatarURL: nil,
|
|
||||||
mediaProvider: MockMediaProvider(),
|
|
||||||
attributedStringBuilder: AttributedStringBuilder())
|
|
||||||
|
|
||||||
let eventBrief = EventBrief(eventId: "id",
|
let eventBrief = EventBrief(eventId: "id",
|
||||||
senderId: "senderId",
|
senderId: "senderId",
|
||||||
@@ -171,7 +182,9 @@ struct HomeScreen_Previews: PreviewProvider {
|
|||||||
|
|
||||||
viewModel.updateWithRoomList(rooms)
|
viewModel.updateWithRoomList(rooms)
|
||||||
|
|
||||||
viewModel.updateWithUserAvatar(UIImage(systemName: "person.fill.questionmark"))
|
if let avatarImage = UIImage(systemName: "person.fill.questionmark") {
|
||||||
|
viewModel.updateWithUserAvatar(avatarImage)
|
||||||
|
}
|
||||||
|
|
||||||
return HomeScreen(context: viewModel.context)
|
return HomeScreen(context: viewModel.context)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ enum UserSessionCallback {
|
|||||||
case updatedRoomsList
|
case updatedRoomsList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum UserSessionError: Error {
|
||||||
|
case failedRetrievingAvatarURL
|
||||||
|
case failedRetrievingDisplayName
|
||||||
|
}
|
||||||
|
|
||||||
private class WeakUserSessionWrapper: ClientDelegate {
|
private class WeakUserSessionWrapper: ClientDelegate {
|
||||||
private weak var userSession: UserSession?
|
private weak var userSession: UserSession?
|
||||||
|
|
||||||
@@ -70,21 +75,35 @@ class UserSession: ClientDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var userDisplayName: String? {
|
func userDisplayName(_ completion: @escaping (Result<String, UserSessionError>) -> Void) {
|
||||||
do {
|
processingQueue.async {
|
||||||
return try client.displayName()
|
do {
|
||||||
} catch {
|
let displayName = try self.client.displayName()
|
||||||
MXLog.error("Failed retrieving the user's display name with error: \(error)")
|
|
||||||
return nil
|
DispatchQueue.main.async {
|
||||||
|
completion(.success(displayName))
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completion(.failure(UserSessionError.failedRetrievingDisplayName))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var userAvatarURL: String? {
|
func userAvatarURL(_ completion: @escaping (Result<String, UserSessionError>) -> Void) {
|
||||||
do {
|
processingQueue.async {
|
||||||
return try client.avatarUrl()
|
do {
|
||||||
} catch {
|
let displayName = try self.client.avatarUrl()
|
||||||
MXLog.error("Failed retrieving the user's avatar URL with error: \(error)")
|
|
||||||
return nil
|
DispatchQueue.main.async {
|
||||||
|
completion(.success(displayName))
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completion(.failure(UserSessionError.failedRetrievingDisplayName))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user