Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / SwitchMapViewModel.kt
Created June 13, 2018 16:25
Android ViewModel Using SwitchMap
class SwitchMapViewModel @Inject constructor(private val dataSource: Data) : ViewModel() {
// observed by ViewModel as a trigger to retrieve data from the source
private var liveTrigger: MutableLiveData<String> = MutableLiveData()
// observed by Activity/Fragment or some other class as a trigger to do something with the data
private var liveData: LiveData<String>
init {
// default observable trigger
@alwarren
alwarren / ResponseObject.kt
Last active June 11, 2018 19:29
A generic Kotlin wrapper class for determining the success or failure of some method
/**
* A generic wrapper class for determining the success or failure of some method
*
* In the business logic, examine the instance class to determine success or failure
*
* Example -
* <pre>
* <code>
* val result: ResponseObject = SomeRepository().getData(true)
* if (result is ResponseObject.Success) {
@alwarren
alwarren / SingletonClass.java
Created May 23, 2018 17:33
Java thread-safe singleton
public class SingletonClass {
// dummy object for synchronization
private static final Object sLock = new Object();
// single instance of the class
private static SingletonClass INSTANCE;
public static SingletonClass getInstance() {
// force threads to wait until synchronized block completes
synchronized (sLock) {