Skip to content

Instantly share code, notes, and snippets.

View timusus's full-sized avatar

Tim Malseed timusus

  • Mantel Group
  • Melbourne, Victoria
View GitHub Profile
##BaseRecyclerAdapter##
package ${PACKAGE_NAME};
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder, I extends BaseRecyclerAdapter.OnItemClickListener<T>> extends RecyclerView.Adapter<VH> {
@timusus
timusus / AndroidMediaService.java
Last active October 6, 2021 07:09
Example of a media playback Service
//This file represents several classes. It's just named *.java for syntax highlighting purposes!
//AIDL file: Defines interfaces for communicating between activities and your service.
//You can see these implemented in the service @ 'private static class ServiceStub extends IPlaybackService.Stub'
interface IPlaybackService {
void stop();
void play();
#!/usr/local/bin/ruby
## Created by Tim Malseed 16/07/2016
## This script depends on `onesky-ruby`
## Run gem install onesky-ruby
require 'onesky'
API_PUBLIC_KEY = 'your_public_key'
API_SECRET_KEY = 'your_secret_key'
#!/usr/local/bin/ruby
## Created by Tim Malseed 16/07/2016
## This script depends on `onesky-ruby`
## Run gem install onesky-ruby
require 'onesky'
SOURCE_FILE_NAME = 'strings.xml'
@timusus
timusus / HanekeImageLoader.swift
Last active May 16, 2017 00:53
Library-agnostic image loading (iOS/Swift)
import Foundation
class HanekeImageLoader: ImageLoader {
/**
Haneke requires that we register our 'formats' before they can be used.
**/
func setup() {
@timusus
timusus / CallResultAdapterFactory.kt
Last active March 24, 2023 16:10
Retrofit Error Handling
import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Response
import retrofit2.Retrofit
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.concurrent.Executor
class CallResultAdapterFactory(private val responseErrorMapper: ((Response<*>) -> Error?)? = null) : CallAdapter.Factory() {
@timusus
timusus / ResponseMapperSnippet.kt
Last active October 20, 2018 03:57
ResponseMapperSnippet
override fun onResponse(call: Call<T>, response: Response<T>) {
if (call.isCanceled) return
if (response.isSuccessful) {
callback(Result.Success(response.body()))
} else {
callback(Result.Failure(responseErrorMapper?.invoke(response) ?: RemoteServiceHttpError(response)))
}
}
@timusus
timusus / UserDescriptionSnippet.kt
Last active October 20, 2018 03:58
Error to user description
fun Error.userDescription(): String {
return when (this) {
is RemoteServiceHttpError ->
return when {
isServerError -> "A server error occurred. (${httpStatusCode.code})"
else -> "An error occurred. (${httpStatusCode.code})"
}
}
is OAuthError -> {
return "Authentication failed."
@timusus
timusus / Enqueue.kt
Created October 20, 2018 03:26
Retrofit Enqueue Snippet
call.enqueue {
override fun onResponse(response: Response) {
// Todo: Check if response is valid (>200 & < 300) or call
// Todo: Handle error if not valid
}
override fun onFailure() {
// Todo: Handle error
}
}
@timusus
timusus / Enqueue.kt
Created October 20, 2018 03:26
Retrofit Enqueue Snippet
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
}
}