From 2bb46d3b6f5dc9648de4543fc070084775aaecb9 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 12 Jun 2024 19:38:43 +0200 Subject: [PATCH] Add a semaphore to avoid the empty page in the DetailsView and get the user infos when the menu is open :bug: --- Sources/AllInApp/AllIn/Components/Menu.swift | 10 ++++++---- .../AllInApp/AllIn/ViewModels/DetailsViewModel.swift | 11 +++++++++-- Sources/AllInApp/AllIn/Views/DetailsView.swift | 1 - 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Sources/AllInApp/AllIn/Components/Menu.swift b/Sources/AllInApp/AllIn/Components/Menu.swift index 333901e..aed26b4 100644 --- a/Sources/AllInApp/AllIn/Components/Menu.swift +++ b/Sources/AllInApp/AllIn/Components/Menu.swift @@ -12,7 +12,6 @@ struct Menu: View { @Inject var authService: IAuthService - let parameters: [(String, String, String, String)] = [ ("videoGameImage", String(localized: "drawer_create_a_bet"), String(localized: "drawer_create_a_bet_subtitle"), "CreationBet"), ("globeImage", String(localized: "drawer_public_bets"), String(localized: "drawer_public_bets_subtitle"), "Bet"), @@ -40,7 +39,7 @@ struct Menu: View { HStack(spacing: 30) { Spacer() VStack(){ - Text(AppStateContainer.shared.user?.bestWin.description ?? "0") + Text(AppStateContainer.shared.user?.bestWin ?? 0, format: .number) .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) @@ -50,7 +49,7 @@ struct Menu: View { .foregroundColor(AllInColors.grey600Color) } VStack(){ - Text(AppStateContainer.shared.user?.bestWin.description ?? "0") + Text(AppStateContainer.shared.user!.bestWin, format: .number) .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) @@ -60,7 +59,7 @@ struct Menu: View { .foregroundColor(AllInColors.grey600Color) } VStack(){ - Text(AppStateContainer.shared.user?.nbFriends.description ?? "0") + Text(AppStateContainer.shared.user!.nbFriends, format: .number) .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) @@ -104,6 +103,9 @@ struct Menu: View { .frame(maxWidth: .infinity,alignment: .leading) .background(AllInColors.primaryColor) .edgesIgnoringSafeArea(.bottom) + .onAppear { + authService.refreshAuthentication() + } } } diff --git a/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift index 913a165..790035c 100644 --- a/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift @@ -26,13 +26,20 @@ class DetailsViewModel: ObservableObject { } func getItem(withId id: String) { + let semaphore = DispatchSemaphore(value: 0) manager.getBet(withId: id) { bet in - DispatchQueue.main.async { self.betDetail = bet if let firstAnswer = bet.answers.first { self.selectedAnswer = firstAnswer } - } + semaphore.signal() + } + + let result = semaphore.wait(timeout: DispatchTime.now() + .seconds(2)) + + if result == .timedOut { + print("The request has exceeded the deadline") + return } } diff --git a/Sources/AllInApp/AllIn/Views/DetailsView.swift b/Sources/AllInApp/AllIn/Views/DetailsView.swift index 8104a9f..2e3d144 100644 --- a/Sources/AllInApp/AllIn/Views/DetailsView.swift +++ b/Sources/AllInApp/AllIn/Views/DetailsView.swift @@ -6,7 +6,6 @@ struct DetailsView: View { @Binding var isModalPresented: Bool @Binding var isModalParticipated: Bool @StateObject private var viewModel: DetailsViewModel - @State private var isLoading = true var isFinished: Bool { viewModel.betDetail?.wonParticipation == nil ? false : true