Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert robertlevonyan

  • Yerevan, Armenia
View GitHub Profile
View ProgressBar.kt
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
View comparison.csv
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
View SqlDelightDao.kt
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
View DateAdapter.kt
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)
}
View movies.sq
import java.util.Date;
CREATE TABLE Movie(
id INTEGER PRIMARY KEY,
backdropPath TEXT,
posterPath TEXT,
title TEXT,
overview TEXT,
releaseDate TEXT as Date
);
View ObjectBoxDao.kt
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()
View ObjectBox.kt
object ObjectBox {
lateinit var boxStore: BoxStore
private set
fun init(context: Context) {
boxStore = MyObjectBox.builder()
.androidContext(context.applicationContext)
.build()
}
}
View RealmDao.kt
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
}
View AppDatabase.kt
@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 =
View MovieDao.kt
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMovies(vararg movies: RoomMovie)
@Query("SELECT * FROM RoomMovie")
fun getMovies(): Flow<List<RoomMovie>>
}