Skip to content

Instantly share code, notes, and snippets.

Tim Malseed timusus

View GitHub Profile
@timusus
timusus / HomeContract.kt
Last active Apr 21, 2020
Shuttle - Home Contract
View HomeContract.kt
// 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
@timusus
timusus / EqualizerAudioProcessor.kt
Last active Nov 16, 2019
EqualizerAudioProcessor
View EqualizerAudioProcessor.kt
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 } }
View AttemptEngine.kt
/**
* 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.
*
View Enqueue.kt
call.enqueue {
override fun onResult(result: Result) {
when(result){
is Success -> {
// Handle Success
}
is Failure -> {
presentAlert(
title = "Login Failed",
message = result.error.userDescription()
View ResponseMapper.kt
val responseErrorMapper: (retrofit2.Response<*>) -> Error? = { response ->
// Todo: Map response into your custom error type
}
@timusus
timusus / Retrofit.kt
Created Oct 20, 2018
Retrofit CallAdapter
View Retrofit.kt
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(CallResultAdapterFactory(responseErrorMapper))
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(converterFactory)
.build()
View ErrorMapper.kt
val responseErrorMapper: (Response<*>) -> Error? = { response ->
response.errorBody()?.string()?.let { errorBody ->
// Is this an OAuthError?
OAuthError.errorData(response, errorBody)?.let { errorData ->
OAuthError(response, errorData)
}
}
}
@timusus
timusus / Enqueue.kt
Created Oct 20, 2018
Sealed result Retrofit enqueue snippet
View Enqueue.kt
call.enqueue {
override fun onResult(result: Result) {
when(result){
is Success -> {
// Handle Success
}
is Failure -> {
// Handle failure
result.error.doSomething()
}
@timusus
timusus / Result.kt
Created Oct 20, 2018
Sealed Class Snippet
View Result.kt
sealed class Result<out T> {
data class Success<out T>(val data: T?) : Result<T>()
data class Failure(val error: Error) : Result<Nothing>()
}
@timusus
timusus / Enqueue.kt
Created Oct 20, 2018
Retrofit Enqueue Snippet
View Enqueue.kt
call.enqueue {
override fun onSuccess(response: Response) {
// Todo: Check if response is valid (>200 & < 300) or call
}
override fun onError(error: Error) {
// Todo: Handle error
}
}
You can’t perform that action at this time.