Skip to content

Instantly share code, notes, and snippets.

abstract class BaseDao<T : BaseEntity>(
private val tableName: String,
private val roomDatabase: RoomDatabase) {
... the other abstract common methods ...
fun getEntity(ids: Int): LiveData<T> {
return object : ComputableLiveData<List<T>>() {
private var observer: InvalidationTracker.Observer? = null
override fun compute(): List<T>? {
@Dao
abstract class CatDao : BaseDao<CatEntity>("cat_table") {
... the other abstract methods ...
@Query("SELECT * from cat_table WHERE id = :id")
abstract fun getEntity(id: Int): LiveData<CatEntity>
@Query("SELECT * from cat_table WHERE id IN (:ids)")
abstract fun getEntity(ids: List<Int>): LiveData<List<CatEntity>>
abstract class BaseDao<T : BaseEntity>(
private val tableName: String) {
... the other abstract common methods ...
@RawQuery
protected abstract fun getEntitySync(query: SupportSQLiteQuery): List<T>?
fun getEntitySync(id: Int): T? {
return getEntitySync(listOf(id))?.firstOrNull()
abstract class BaseEntity {
abstract val id: Int
}
@Entity(tableName = "cat_table")
data class CatEntity(@NonNull
@PrimaryKey
override
val id: Int,
abstract class BaseDao<T>(private val tableName: String) {
... the other abstract common methods ...
/**
* This code snippet won't compile successfully
*
* @Query("DELETE from tableName")
* abstract fun deleteAll(tableName : String)
*/
interface BaseDao<T> {
@Insert
fun insert(entity: T)
@Insert
fun insert(entities: List<T>)
@Update
fun update(entity: T)
abstract class BaseDao<T : BaseEntity>(
private val tableName: String,
private val roomDatabase: RoomDatabase) {
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract fun insert(entity: T): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract fun insert(entities: List<T>): LongArray
@Entity(tableName = "cat_table")
data class CatEntity(@NonNull
@PrimaryKey
val id : Int,
val name : String)
@Entity(tableName = "dog_table")
data class DogEntity(@NonNull
@PrimaryKey
val id : Int,
val name : String)
@BerryCruise
BerryCruise / HeadlessFragment.java
Created July 4, 2019 05:49 — forked from keyboardr/HeadlessFragment.java
A file template for creating a headless Fragment. The attach() methods add the fragment to the parent if it doesn't already exist and returns the attached fragment. The methods ensure that the parent implements the parent interface. If needed, parameters may be added to the attach() methods to supply fragment arguments.
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
#parse("File Header.java")
public class ${NAME} extends Fragment {
private static final String FRAG_TAG = ${NAME}.class.getCanonicalName();
@BerryCruise
BerryCruise / LiveDataUtils.kt
Created March 28, 2018 10:23 — forked from magneticflux-/LiveDataUtils.kt
Helpful Android-Kotlin LiveData utilities
/*
* Copyright (C) 2017 Mitchell Skaggs, Keturah Gadson, Ethan Holtgrieve, Nathan Skelton, Pattonville School District
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of