Skip to content

Instantly share code, notes, and snippets.

View dinorahto's full-sized avatar

Dinorah Tovar dinorahto

View GitHub Profile
@dinorahto
dinorahto / PinEditTextView
Last active August 3, 2022 19:27
Pin view for OTP
/**
* Custom EditText that paints a bottom line and the text or
* a filled circle if it is a password
*/
class PinEntryEditText : AppCompatEditText {
private var mSpace = 24f //24 dp by default, space between the lines
private var mNumChars = 4f
private var mLineSpacing = 8f //8dp by default, height of the text from our lines
@dinorahto
dinorahto / ObserverLifecycle.kt
Created August 16, 2019 18:16
ProcessLifecycle Owner to check foreground or background
ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationOwner())
@dinorahto
dinorahto / Foreground.kt
Created August 16, 2019 18:06
OnResume and OnPause to know if the app is in foreground
override fun onResume() {
super.onResume()
MyApplication.activityResumed()
}
override void onPause() {
super.onPause()
MyApplication.activityPaused()
}
@dinorahto
dinorahto / MigrationFinal.kt
Last active July 16, 2019 15:21
Final migration
@Database(entities = [ProductEntity::class], version = SharedContentDataBase.VERSION)
abstract class SharedContentDataBase : RoomDatabase() {
companion object {
const val VERSION = 2
val migrationRoom: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
val queryForDatabase = """
CREATE TABLE IF NOT EXISTS UpdatedTableProduct (idProduct TEXT PRIMARY KEY NOT NULL, name TEXT, nameClean TEXT,
@dinorahto
dinorahto / indexes.kt
Created July 16, 2019 14:26
Indexes on room
val addIndex = """
CREATE INDEX IF NOT EXISTS index_Product_idProduct_skuId ON Product(idProduct, skuId)
"""
database.execSQL(addIndex)
@dinorahto
dinorahto / ForeignKeys.kt
Created July 16, 2019 14:23
ForeignKeys on Room migration
val queryForDatabase = """
CREATE TABLE IF NOT EXISTS UpdatedTableProduct (idProduct TEXT PRIMARY KEY NOT NULL, name TEXT, nameClean TEXT,
description TEXT, discount INT, skuId TEXT, hasAddedToCart INT, opened INT, notified INT,
FOREIGN KEY (idProduct) REFERENCES Image(imageId) ON UPDATE NO ACTION ON DELETE NO ACTION,
FOREIGN KEY (skuId) REFERENCES Prices(idProduct) ON UPDATE NO ACTION ON DELETE NO ACTION """"
database.execSQL(queryForDatabase)
@dinorahto
dinorahto / CreatingTable.kt
Last active July 16, 2019 15:18
Creating of a Table
val queryForDatabase = """
CREATE TABLE IF NOT EXISTS UpdatedTableProduct (idProduct TEXT PRIMARY KEY NOT NULL, name TEXT, nameClean TEXT,
description TEXT, discount INT, skuId TEXT, hasAddedToCart INT, opened INT, notified INT"""
database.execSQL(queryForDatabase)
val queryCopyDataBase = """
INSERT OR REPLACE INTO UpdatedTableProduct (idProduct, name, nameClean, description, discount,
skuId, hasAddedToCart, opened, notified)
SELECT idProduct, name, nameClean, description, discount,
skuId, hasAddedToCart, opened, notified FROM Product"""
@dinorahto
dinorahto / AddTable.kt
Last active July 16, 2019 14:36
Add Table to old SQLite database
database.execSQL("ALTER TABLE Product ADD COLUMN hasAddedToCart INT")
@dinorahto
dinorahto / Migration.kt
Created July 16, 2019 14:09
Create the migration from SQlite to Room
@Database(entities = [ProductEntity::class], version = SharedContentDataBase.VERSION)
abstract class SharedContentDataBase : RoomDatabase() {
companion object {
const val VERSION = 2
val migrationRoom: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
//This is where the magic happends
@dinorahto
dinorahto / ProductEntity.kt
Last active July 16, 2019 14:02
Entity Data Class
@Entity(tableName = "Products",
foreignKeys = [ForeignKey(entity = ImagesEntity::class,
parentColumns = arrayOf("imageId"), childColumns = arrayOf("cityId")),
ForeignKey(entity = PricesEntity::class,
parentColumns = arrayOf("skuId"), childColumns = arrayOf("idProduct"))],
indices = [Index(value = ["idProduct", "skuId"])])
data class ProductEntity (
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "idProduct")