Skip to content

Instantly share code, notes, and snippets.

@alwarren
alwarren / RetrofitRepo.kt
Last active March 16, 2023 21:52
Using generics with Retrofit repositories. A work in progress.
package com.example.domain.api
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
open class RetrofitRepo<out S>(clazz: Class<S>) {
val service: S = Api.service(clazz)
@alwarren
alwarren / RecyclerAdapter.kt
Last active July 30, 2018 22:33
Kotlin RecyclerView with LayoutInflater extension.
class RecyclerAdapter(private val users: ArrayList<User>) : RecyclerView.Adapter<RecyclerAdapter.UserHolder>() {
override fun getItemCount(): Int {
return users.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.UserHolder {
// see Extensions.kt
val inflatedView = parent.inflate(R.layout.list_row, false)
return UserHolder(inflatedView)
}
@alwarren
alwarren / MainActivity.kt
Last active August 9, 2018 20:27
An Updated Notification Example
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import android.app.AlarmManager
import android.app.Notification
import android.os.SystemClock
import android.app.PendingIntent
import android.content.Context
@alwarren
alwarren / ApiResponse.kt
Created August 16, 2018 13:28
A generic Kotlin wrapper class for determining the success or failure of some method
sealed class ApiResponse {
class Loading(val loading: Boolean = true) : ApiResponse()
class Success<T>(val data: T) : ApiResponse()
class Error(val message: String) : ApiResponse()
// For demonstration purposes only
companion object {
fun evaluate(response: ApiResponse) {
when(response) {
is ApiResponse.Loading -> {
@alwarren
alwarren / Android_Dagger2_Readme.md
Last active September 5, 2018 13:18
Step-by-step instructions for setting up Dagger2 in an Android Project (Kotlin)

A Step-by-step Process for Adding Dagger2 to Android

Dagger2 is a powerful dependency injection library. However, it can be confusing for the first-time user. And unless things are created in the correct order, builds fail with cryptic error messages.

These step-by-step instructions were created while setting up a new project to document the process.

Project Setup

1. Setup Gradle version variables (optional)
2. Setup Gradle dependencies
  1. Create an App class that extends Application
@alwarren
alwarren / AndroidDaggerConstructorInjection.kt
Last active September 7, 2018 22:14
Android and Dagger2 Construcor Injection using Named (Kotlin)
interface Greeting {
fun getData(): String
}
class GreetingRepository
@Inject
constructor(
@Named(HELLO) private val hello: Greeting,
@Named(GOODBYE) private val goodbye: Greeting
) {
@alwarren
alwarren / MainPresenterTest.kt
Last active September 24, 2018 23:14
Integration Testing MVP With Mockito - Kotlin
/**
* An MVP integration test
*
* This test verifies integration of all three MVP components without actually implementing
* any of those components in the application.
*
* Passing on 9/24/2018
*/
class MainPresenterTest {
private val repository: Repository = mock(Repository::class.java)
@alwarren
alwarren / ColorInterpolator.java
Last active February 14, 2019 13:29
Calculates a color value using a range pointer that lies somewhere between two colors. Originaly used an Android ProgressBar as the pointer.
/*
* Copyright (c) 2016. Al Warren.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@alwarren
alwarren / TimberLoggingActivity.kt
Last active October 3, 2018 17:50
Android Timber Logging Activity/Fragment
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import timber.log.Timber
open class TimberLoggingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
Timber.d("onCreate is called")
super.onCreate(savedInstanceState, persistentState)
}
@alwarren
alwarren / TimberDebugTree.kt
Last active October 3, 2018 16:25
A Custom Timber Debug Tree with Prepended Tags
import android.os.Build
import timber.log.Timber
const val TIMBER_TAG = "--->"
const val MAX_TAG_LENGTH = 23
class TimberDebugTree : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String? {
val tag = TIMBER_TAG + super.createStackElementTag(element)