This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
CREATE TABLE Movie( | |
id INTEGER PRIMARY KEY, | |
backdropPath TEXT, | |
posterPath TEXT, | |
title TEXT, | |
overview TEXT, | |
releaseDate TEXT as Date | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ObjectBox { | |
lateinit var boxStore: BoxStore | |
private set | |
fun init(context: Context) { | |
boxStore = MyObjectBox.builder() | |
.androidContext(context.applicationContext) | |
.build() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 = |
NewerOlder