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) | |
} |
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> */ |
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]. | |
*/ |
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 |
View testing.csv
Component | Test | Mock | |
---|---|---|---|
UI | Espresso | ViewModel | |
ViewModel | JUnit | Repository | |
Repository | JUnit | DAO and WebService | |
DAO | Instrumented | - | |
WebService | Instrumented | MockWebServer |
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; |
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}>() { |
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 | |
} |
View UserDao.kt
@Dao | |
abstract class UserDao { | |
@Insert | |
abstract fun insert(user: User) | |
@Update | |
abstract fun update(user: User) | |
@Transaction |
NewerOlder