generated from JetBrains/compose-multiplatform-ios-android-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces Coroutine Dispatcher.IO with seperate BackgroundExecutor in …
…repos
- Loading branch information
1 parent
875d788
commit 30d7602
Showing
10 changed files
with
92 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Subscription Premium is added for premium features. | ||
Subscription Premium is added for premium features. | ||
Fixed crashes when server returns error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+1.65 KB
(100%)
iosApp/iosApp.xcworkspace/xcuserdata/mirzemehdi.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package data | ||
|
||
import domain.model.result.ErrorEntity | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.cancellation.CancellationException | ||
import domain.model.result.Result | ||
|
||
class BackgroundExecutor(val scope: CoroutineContext = Dispatchers.IO) { | ||
|
||
companion object { | ||
val IO by lazy { BackgroundExecutor(Dispatchers.IO) } | ||
} | ||
|
||
suspend fun <T> execute( | ||
func: suspend () -> Result<T> | ||
): Result<T> = withContext(scope) { | ||
try { | ||
func.invoke() | ||
} catch (e: Exception) { | ||
if (e is CancellationException) throw e | ||
else Result.error(ErrorEntity.unexpected(e)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 15 additions & 20 deletions
35
shared/src/commonMain/kotlin/data/repository/FlightsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,36 @@ | ||
package data.repository | ||
|
||
import data.BackgroundExecutor | ||
import data.source.remote.apiservice.FlightsApiService | ||
import domain.model.FlightLocation | ||
import domain.model.FlightSort | ||
import domain.model.Top5Flights | ||
import domain.model.result.ErrorEntity | ||
import domain.model.result.Result | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class FlightsRepository( | ||
private val flightsApiService: FlightsApiService, | ||
private val backgroundScope: CoroutineContext = Dispatchers.IO, | ||
private val backgroundExecutor: BackgroundExecutor = BackgroundExecutor.IO, | ||
) { | ||
|
||
suspend fun getTop5Flights( | ||
origin: FlightLocation? = null, | ||
maxPrice: Int = 50, | ||
sortBy: FlightSort = FlightSort.BY_PRICE, | ||
): Result<Top5Flights> = withContext(backgroundScope) { | ||
try { | ||
val apiResponse = flightsApiService.getTop5Flights( | ||
origin?.iataCode, maxPrice.toString(), sortBy.value | ||
) | ||
if (apiResponse.hasSuccessfulData()) Result.success( | ||
apiResponse.getSuccessfulData().mapToDomainModel() | ||
) | ||
else Result.error( | ||
ErrorEntity.apiError( | ||
errorMessage = apiResponse.errorMessage, responseCode = apiResponse.responseCode | ||
) | ||
): Result<Top5Flights> = backgroundExecutor.execute { | ||
|
||
val apiResponse = flightsApiService.getTop5Flights( | ||
origin?.iataCode, maxPrice.toString(), sortBy.value | ||
) | ||
if (apiResponse.hasSuccessfulData()) Result.success( | ||
apiResponse.getSuccessfulData().mapToDomainModel() | ||
) | ||
else Result.error( | ||
ErrorEntity.apiError( | ||
errorMessage = apiResponse.errorMessage, responseCode = apiResponse.responseCode | ||
) | ||
} catch (e: Exception) { | ||
Result.error(ErrorEntity.apiError(exception = e)) | ||
} | ||
) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters