Skip to content

Instantly share code, notes, and snippets.

Avatar
🔱

Akshay Chordiya AkshayChordiya

🔱
View GitHub Profile
@AkshayChordiya
AkshayChordiya / FeedRepository.kt
Created Feb 25, 2018
Sample FeedRepository using NetworkBoundResouce class
View FeedRepository.kt
class FeedRepository @Inject constructor(
private val feedService: FeedService,
private val feedDao: FeedDao,
private val appExecutors: AppExecutors = AppExecutors()
) {
fun getFeeds(): LiveData<Resource<List<Feed>?>> = object : NetworkBoundResource<List<Feed>, List<Feed>>(appExecutors) {
override fun saveCallResult(item: List<Feed>) {
feedDao.insertAll(item)
}
@AkshayChordiya
AkshayChordiya / NetworkBoundResource.kt
Created Feb 25, 2018
A generic class that can provide a resource backed by both the SQLite database and the network
View NetworkBoundResource.kt
/**
* A generic class that can provide a resource backed by both the sqlite database and the network.
*
*
* You can read more about it in the [Architecture
* Guide](https://developer.android.com/arch).
*
* @param <ResultType>
* @param <RequestType>
</RequestType></ResultType> */
@AkshayChordiya
AkshayChordiya / Resource.kt
Created Feb 25, 2018
Generic class to hold data and state with LiveData
View Resource.kt
/**
* A generic class that holds a value with its loading status.
* @param <T>
</T> */
data class Resource<ResultType>(var status: Status, var data: ResultType? = null, var message: String? = null) {
companion object {
/**
* Creates [Resource] object with `SUCCESS` status and [data].
*/
@AkshayChordiya
AkshayChordiya / components-action.csv
Created Feb 24, 2018
Android Architecture Guidelines Cheatsheet
View components-action.csv
Component Action
UI Controllers (Activity and Fragment) Only UI related logic
ViewModel Container for data required by UI
Repository Single source of truth for data
Room Local Database
Retrofit Web Service
@AkshayChordiya
AkshayChordiya / testing.csv
Created Feb 24, 2018
Android Architecture Guidelines Testing
View testing.csv
Component Test Mock
UI Espresso ViewModel
ViewModel JUnit Repository
Repository JUnit DAO and WebService
DAO Instrumented -
WebService Instrumented MockWebServer
@AkshayChordiya
AkshayChordiya / ApiResponse.java
Last active Oct 14, 2020
LiveData adapter for Retrofit
View ApiResponse.java
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@AkshayChordiya
AkshayChordiya / RecyclerViewAdapterTemplate.kt
Created Feb 1, 2018
File Template for Android Studio to instantly create RecyclerViewAdapter in Kotlin without boilerplate
View RecyclerViewAdapterTemplate.kt
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
#parse("File Header.java")
class ${NAME} : RecyclerView.Adapter<${ViewHolder_Class}>() {
@AkshayChordiya
AkshayChordiya / DateHeaderHelper.kt
Last active May 14, 2020
Use these date related top-level Kotlin functions to show sections like "Today", "Yesterday" and so on
View DateHeaderHelper.kt
import org.joda.time.LocalDate
/**
* @return true if the supplied date is in the future else false
*/
fun isUpcoming(millis: Long): Boolean {
return !isTomorrow(millis) && LocalDate(millis).isAfter(LocalDate.now())
}
/**
View DateConverters.kt
class DateConverters {
@TypeConverter
fun fromTimestamp(value: Long?): Date? {
return if (value == null) null else Date(value)
}
@TypeConverter
fun dateToTimestamp(date: Date?): Long? {
return date?.time
}
@AkshayChordiya
AkshayChordiya / UserDao.kt
Created Dec 9, 2017
User DAO with transaction
View UserDao.kt
@Dao
abstract class UserDao {
@Insert
abstract fun insert(user: User)
@Update
abstract fun update(user: User)
@Transaction