Skip to content

Instantly share code, notes, and snippets.

View AkshayChordiya's full-sized avatar
🔱

Akshay Chordiya AkshayChordiya

🔱
View GitHub Profile
@AkshayChordiya
AkshayChordiya / FeedRepository.kt
Created February 25, 2018 05:57
Sample FeedRepository using NetworkBoundResouce class
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 February 25, 2018 05:24
A generic class that can provide a resource backed by both the SQLite database and the network
/**
* 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 February 25, 2018 05:01
Generic class to hold data and state with LiveData
/**
* 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 February 24, 2018 14:44
Android Architecture Guidelines Cheatsheet
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 February 24, 2018 14:33
Android Architecture Guidelines Testing
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 February 3, 2024 01:47
LiveData adapter for Retrofit
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 February 1, 2018 05:16
File Template for Android Studio to instantly create RecyclerViewAdapter in Kotlin without boilerplate
#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 26, 2023 13:39
Use these date related top-level Kotlin functions to show sections like "Today", "Yesterday" and so on
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())
}
/**
@AkshayChordiya
AkshayChordiya / DateConverters.kt
Created December 9, 2017 08:55
Date Converter for Room
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 December 9, 2017 07:08
User DAO with transaction
@Dao
abstract class UserDao {
@Insert
abstract fun insert(user: User)
@Update
abstract fun update(user: User)
@Transaction