Skip to content

Commit

Permalink
Cleanup and refactor in the refresh functionality (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: Pantelis Giazitsis <[email protected]>
  • Loading branch information
pantelisss and Pantelis Giazitsis authored Sep 13, 2024
1 parent 5f71e62 commit 295488d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,24 @@ class StationForecastViewModel: ObservableObject {
.itemId: .dailyForecast])
}

private func getDeviceForecastDaily(deviceId: String?) async -> Result<[NetworkDeviceForecastResponse], NetworkErrorResponse>? {
guard let deviceId = deviceId else {
return nil
}

do {
let getUserDeviceForecastById = try useCase?.getUserDeviceForecastById(deviceId: deviceId,
fromDate: getCurrentDateInStringForForecast(),
toDate: getΤοDateForWeeklyForecastCall())
return await withCheckedContinuation { continuation in

getUserDeviceForecastById?.sink { response in
continuation.resume(returning: response.result)
}.store(in: &cancellables)
}
} catch { return nil }
}

}

private extension StationForecastViewModel {
@MainActor
func getDeviceForecastDaily(deviceId: String?) async -> Result<[NetworkDeviceForecastResponse], NetworkErrorResponse>? {
guard let deviceId = deviceId else {
return nil
}

do {
let getUserDeviceForecastById = try await useCase?.getUserDeviceForecastById(deviceId: deviceId,
fromDate: getCurrentDateInStringForForecast(),
toDate: getΤοDateForWeeklyForecastCall()).toAsync()
return getUserDeviceForecastById?.result
} catch { return nil }
}

func observeOffset() {
offsetObject.$diffOffset.sink { [weak self] value in
guard let self = self else {
Expand Down Expand Up @@ -180,24 +178,22 @@ private extension StationForecastViewModel {
// MARK: - StationDetailsViewModelChild

extension StationForecastViewModel: StationDetailsViewModelChild {
@MainActor
func refreshWithDevice(_ device: DeviceDetails?, followState: UserDeviceFollowState?, error: NetworkErrorResponse?) async {
self.device = device
self.followState = followState

if followState == nil {
DispatchQueue.main.async {
self.hiddenViewConfiguration = self.generateHiddenViewConfiguration()
self.viewState = .hidden
}
return
}
if followState == nil {
self.hiddenViewConfiguration = self.generateHiddenViewConfiguration()
self.viewState = .hidden
return
}

guard let res = await getDeviceForecastDaily(deviceId: device?.id) else {
return
}
DispatchQueue.main.async {
self.handleResponseResult(res)
}

self.handleResponseResult(res)
}

func showLoading() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@ private extension ObservationsViewModel {
// MARK: - StationDetailsViewModelChild

extension ObservationsViewModel: StationDetailsViewModelChild {
func refreshWithDevice(_ device: DeviceDetails?, followState: UserDeviceFollowState?, error: NetworkErrorResponse?) async {
DispatchQueue.main.async {
self.device = device
self.followState = followState
self.ctaObject = followState == nil ? self.generateCtaObject() : nil

if let error {
let info = error.uiInfo
self.failObj = info.defaultFailObject(type: .observations, retryAction: self.handleRetryButtonTap)
self.viewState = .fail
} else {
self.viewState = .content
}
}
}
@MainActor
func refreshWithDevice(_ device: DeviceDetails?, followState: UserDeviceFollowState?, error: NetworkErrorResponse?) async {
self.device = device
self.followState = followState
self.ctaObject = followState == nil ? self.generateCtaObject() : nil

if let error {
let info = error.uiInfo
self.failObj = info.defaultFailObject(type: .observations, retryAction: self.handleRetryButtonTap)
self.viewState = .fail
} else {
self.viewState = .content
}
}

func showLoading() {
viewState = .loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,21 @@ private extension StationRewardsViewModel {
}

extension StationRewardsViewModel: StationDetailsViewModelChild {
@MainActor
func refreshWithDevice(_ device: DeviceDetails?, followState: UserDeviceFollowState?, error: NetworkErrorResponse?) async {
self.device = device
self.followState = followState

let tuple = await getRewards()
if let error = tuple.error {
DispatchQueue.main.async {
self.showErrorView(error: error)
}
return
}
self.showErrorView(error: error)

DispatchQueue.main.async {
self.response = tuple.response
self.viewState = tuple.response?.isEmpty == false ? .content : .empty
return
}
}

self.response = tuple.response
self.viewState = tuple.response?.isEmpty == false ? .content : .empty
}

func showLoading() {
viewState = .loading
Expand Down
12 changes: 2 additions & 10 deletions wxm-ios/DomainLayer/DomainLayer/UseCases/RewardsUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ public struct RewardsUseCase {
}

public func getDeviceRewardsSummary(deviceId: String) async throws -> Result<NetworkDeviceRewardsSummaryResponse, NetworkErrorResponse> {
let rewardsPublisher = try devicesRepository.deviceRewardsSummary(deviceId: deviceId)
return await withUnsafeContinuation { continuation in
rewardsPublisher.sink { response in
if let error = response.error {
continuation.resume(returning: .failure(error))
} else {
continuation.resume(returning: .success(response.value!))
}
}.store(in: &cancellables.cancellableSet)
}
let rewardsPublisher = try await devicesRepository.deviceRewardsSummary(deviceId: deviceId).toAsync().result
return rewardsPublisher
}
}

0 comments on commit 295488d

Please sign in to comment.