Skip to content

Instantly share code, notes, and snippets.

View robertlevonyan's full-sized avatar
🏠
Working from home

Robert Levonyan robertlevonyan

🏠
Working from home
View GitHub Profile
class TokenInterceptor(
private val preferencesService: PreferencesService, // shared prefs or data store
private val retrofit: Retrofit,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = runBlocking {
val request: Request = chain.request()
val response = chain.proceed(request = request)
if (!response.isSuccessful && response.code == 401) {
package com.robertlevonyan.demo.progressbar
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
class SqlDelightDao() {
/**
* Create a SqlDriver instance, which will maintain connections to an underlying SQL database and provide APIs
* for managing transactions and executing SQL statements.
* Params are a Schema of the generated database, an application context and a name of db
*/
private val driver: SqlDriver = AndroidSqliteDriver(Database.Schema, context, "caching.db")
// Instance of the generated database instance with adapter. Movie is a generated entoty
private val database = Database(driver, Movie.Adapter(DateAdapter()))
// Instance of generated MoviesQueries interface
Name Support by Google Caching RxJava Coroutines Additional files Write* Read*
Room Yes SQLite Yes Yes No 247** 181**
Realm No C++ core Yes Partial No 18 12
ObjectBox No JSON Yes No No 4 4
SqlDelight No SQLite Yes Yes Yes 127 14
class DateAdapter : ColumnAdapter<Date, String> {
companion object {
private val format = SimpleDateFormat("yyyy-MM-dd", Locale.US)
}
override fun decode(databaseValue: String): Date = format.parse(databaseValue) ?: Date()
override fun encode(value: Date): String = format.format(value)
}
import java.util.Date;
CREATE TABLE Movie(
id INTEGER PRIMARY KEY,
backdropPath TEXT,
posterPath TEXT,
title TEXT,
overview TEXT,
releaseDate TEXT as Date
);
class ObjectBoxDao() : DbRepository {
// create a Box instance for Movie type
private val moviesBox: Box<Movie> = ObjectBox.boxStore.boxFor(ObMovie::class.java)
override suspend fun save(movies: List<Movie>) {
moviesBox.put(movies)
}
override fun getMovies(): Flow<List<Movie>> = callbackFlow { // wrap response in a callback flow
val subscription = moviesBox.query().build().subscribe()
object ObjectBox {
lateinit var boxStore: BoxStore
private set
fun init(context: Context) {
boxStore = MyObjectBox.builder()
.androidContext(context.applicationContext)
.build()
}
}
class RealmDao() {
private val realm: Realm = Realm.getDefaultInstance()
suspend fun save(movies: List<Movie>) {
realm.executeTransactionAwait { r -> // open a realm transaction
for (movie in movies) {
if (r.where(RealmMovie::class.java).equalTo("id", movie.id).findFirst() != null) {
continue
}
@Database(entities = [RoomMovie::class], version = 1) // an array with entites and the current db version
@TypeConverters(value = [DateTypeConverter::class]) // an array of all type converters
abstract class AppDatabase : RoomDatabase() {
abstract fun movieDao(): MovieDao
companion object {
@Volatile
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase =