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
import androidx.compose.ui.test.IdlingResource | |
/** | |
* An [IdlingResource] which reports itself as idle until [waitingTime] has elapsed. | |
*/ | |
class ElapsedTimeIdlingResource(private val waitingTime: Long) : IdlingResource { | |
var startTime: Long = System.currentTimeMillis() | |
override val isIdleNow: Boolean |
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 Gist demonstrates the use of MVP in an Android project. | |
// File 1 - The contract. Defines the View and Presenter interface. | |
// File 2 - The Presenter implementation | |
// File 3 - The View implementation (ViewController / Fragment) | |
// I like this architecture because it helps with separation of concerns. The business logic is carried out | |
// in the Presenter, which has no (or limited) knowledge of Android framework components (which also makes it easier to test) | |
// The View Controller (Fragment) is then kept very lightweight, and only deals with updating views and dispatching |
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
class EqualizerAudioProcessor : BaseAudioProcessor() { | |
lateinit var prevXArray: Array<ShortArray> | |
lateinit var prevYArray: Array<ShortArray> | |
override fun configure(sampleRateHz: Int, channelCount: Int, encoding: Int): Boolean { | |
val flush = setInputFormat(sampleRateHz, channelCount, encoding) | |
prevXArray = Array(channelCount) { ShortArray(2) { 0 } } | |
prevYArray = Array(channelCount) { ShortArray(2) { 0 } } |
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
/** | |
* Executes a unit of work at a specified interval. | |
* | |
* An attempt threshold can be specified. If the number of attempts to execute the work exceeds this threshold, `didExceedAttemptThreshold()` is called and the timer is | |
* invalidated. | |
* | |
* An error threshold can also be specified. If the number of errors exceeds this threshold, `didExceedErrorThreshold()` is called, and the latest error is passed in. | |
* | |
* Note: This class uses a Timer to execute the work, ensure to call `stop()` when no longer interested in the result, or a leak may occur. | |
* |
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
call.enqueue { | |
override fun onResult(result: Result) { | |
when(result){ | |
is Success -> { | |
// Handle Success | |
} | |
is Failure -> { | |
presentAlert( | |
title = "Login Failed", | |
message = result.error.userDescription() |
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
val responseErrorMapper: (retrofit2.Response<*>) -> Error? = { response -> | |
// Todo: Map response into your custom error type | |
} |
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
val retrofit = Retrofit.Builder() | |
.addCallAdapterFactory(CallResultAdapterFactory(responseErrorMapper)) | |
.baseUrl(baseUrl) | |
.client(okHttpClient) | |
.addConverterFactory(converterFactory) | |
.build() |
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
val responseErrorMapper: (Response<*>) -> Error? = { response -> | |
response.errorBody()?.string()?.let { errorBody -> | |
// Is this an OAuthError? | |
OAuthError.errorData(response, errorBody)?.let { errorData -> | |
OAuthError(response, errorData) | |
} | |
} | |
} |
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
call.enqueue { | |
override fun onResult(result: Result) { | |
when(result){ | |
is Success -> { | |
// Handle Success | |
} | |
is Failure -> { | |
// Handle failure | |
result.error.doSomething() | |
} |
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
sealed class Result<out T> { | |
data class Success<out T>(val data: T?) : Result<T>() | |
data class Failure(val error: Error) : Result<Nothing>() | |
} |
NewerOlder