Skip to content

Commit

Permalink
fix: always join alerts on app startup (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaylaBrady authored Nov 1, 2024
1 parent 5fbeb58 commit ba0ade2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
60 changes: 31 additions & 29 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,39 @@ struct ContentView: View {
@State private var selectedTab = SelectedTab.nearby

var body: some View {
contents
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.onAppear {
Task { await contentVM.loadOnboardingScreens() }
VStack {
contents
}
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.onAppear {
Task { await contentVM.loadOnboardingScreens() }
}
.task {
// We can't set stale caches in ResponseCache on init because of our Koin setup,
// so this is here to get the cached data into the global flow and kick off an async request asap.
do {
_ = try await RepositoryDI().global.getGlobalData()
} catch {}
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
socketProvider.socket.attach()
nearbyVM.joinAlertsChannel()
} else if newPhase == .background {
nearbyVM.leaveAlertsChannel()
socketProvider.socket.detach()
}
.task {
// We can't set stale caches in ResponseCache on init because of our Koin setup,
// so this is here to get the cached data into the global flow and kick off an async request asap.
do {
_ = try await RepositoryDI().global.getGlobalData()
} catch {}
}
.onChange(of: contentVM.configResponse) { response in
switch onEnum(of: response) {
case let .ok(response): contentVM.configureMapboxToken(token: response.data.mapboxPublicToken)
default: debugPrint("Skipping mapbox token configuration")
}
}
.onReceive(mapVM.lastMapboxErrorSubject
.debounce(for: .seconds(1), scheduler: DispatchQueue.main)) { _ in
Task { await contentVM.loadConfig() }
}
}

@ViewBuilder
Expand Down Expand Up @@ -148,25 +169,6 @@ struct ContentView: View {
Task { await contentVM.loadHideMaps() }
Task { await settingsVM.getSections() }
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
socketProvider.socket.attach()
nearbyVM.joinAlertsChannel()
} else if newPhase == .background {
nearbyVM.leaveAlertsChannel()
socketProvider.socket.detach()
}
}
.onChange(of: contentVM.configResponse) { response in
switch onEnum(of: response) {
case let .ok(response): contentVM.configureMapboxToken(token: response.data.mapboxPublicToken)
default: debugPrint("Skipping mapbox token configuration")
}
}
.onReceive(mapVM.lastMapboxErrorSubject
.debounce(for: .seconds(1), scheduler: DispatchQueue.main)) { _ in
Task { await contentVM.loadConfig() }
}
}

@ViewBuilder var mapSection: some View {
Expand Down
18 changes: 10 additions & 8 deletions iosApp/iosAppTests/Views/ContentViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ final class ContentViewTests: XCTestCase {

ViewHosting.host(view: sut)

try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
.vStack().callOnChange(newValue: ScenePhase.background)
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView()
.vStack()
.callOnChange(newValue: ScenePhase.background)
wait(for: [disconnectedExpectation], timeout: 5)
}

Expand All @@ -56,8 +57,9 @@ final class ContentViewTests: XCTestCase {

ViewHosting.host(view: sut)

try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
.vStack().callOnChange(newValue: ScenePhase.background)
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView()
.vStack()
.callOnChange(newValue: ScenePhase.background)
wait(for: [disconnectedExpectation], timeout: 1)
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
.vStack().callOnChange(newValue: ScenePhase.active)
Expand All @@ -77,7 +79,7 @@ final class ContentViewTests: XCTestCase {

ViewHosting.host(view: sut)

try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView()
.vStack().callOnChange(newValue: ScenePhase.active)
wait(for: [joinAlertsExp], timeout: 5)
}
Expand All @@ -93,8 +95,8 @@ final class ContentViewTests: XCTestCase {

ViewHosting.host(view: sut)

try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
.vStack().callOnChange(newValue: ScenePhase.background)
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().vStack()
.callOnChange(newValue: ScenePhase.background)
wait(for: [leavesAlertsExp], timeout: 5)
}

Expand Down Expand Up @@ -123,7 +125,7 @@ final class ContentViewTests: XCTestCase {

let newConfig: ApiResult<ConfigResponse>? = ApiResultOk(data: .init(mapboxPublicToken: "FAKE_TOKEN"))

try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView().implicitAnyView().implicitAnyView()
try sut.inspect().implicitAnyView().view(ContentView.self).implicitAnyView()
.vStack()
.callOnChange(newValue: newConfig)
wait(for: [tokenConfigExpectation], timeout: 5)
Expand Down

0 comments on commit ba0ade2

Please sign in to comment.