Skip to content

Instantly share code, notes, and snippets.

View lomza's full-sized avatar
🗺️

Tonia lomza

🗺️
View GitHub Profile
@lomza
lomza / .java
Last active May 21, 2020 12:59
Room db migration
package net.ahammad.roomsample;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
object AppPreferences {
private const val NAME = "SpinKotlin"
private const val MODE = Context.MODE_PRIVATE
private lateinit var preferences: SharedPreferences
// list of app specific preferences
private val IS_FIRST_RUN_PREF = Pair("is_first_run", false)
fun init(context: Context) {
preferences = context.getSharedPreferences(NAME, MODE)
}
class SpInKotlinApp : Application() {
override fun onCreate() {
super.onCreate()
AppPreferences.init(this)
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!AppPreferences.firstRun) {
AppPreferences.firstRun = true
Log.d("SpinKotlin", "The value of our pref is: ${AppPreferences.firstRun}")
}
@Entity(tableName = "DAY_LOOK")
data class DayLookEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
var id: Long? = null,
@ColumnInfo(name = "DATE")
var date: String,
@Database(entities = [DayLookEntity::class], version = 2)
abstract class ScreenCounterDb : RoomDatabase() {
private var dbClearFlag = false
abstract fun dayLookDao(): DayLookDbDao
companion object {
@Volatile
/**
* Service which starts a receiver for catching SCREEN_ON, SCREEN_OFF, and USER_PRESENT events.
*
* @author Antonina
*/
class LookCounterService : Service() {
private val SERVICE_ID = 1
private lateinit var screenLookReceiver: ScreenLookReceiver
@Dao
interface DayLookDbDao {
@Query("SELECT * FROM DAY_LOOK WHERE DATE = :dateToSearch LIMIT 1")
fun getDayLookByDate(dateToSearch: String?): Maybe<DayLookEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(dayLook: DayLookEntity)
@Query("SELECT * FROM DAY_LOOK")
interface CalendarContract {
interface Presenter : BaseMvpPresenter<CalendarContract.View> {
fun populateList(context: Context?, date: Calendar, screenLookCategory: ScreenLookCategory)
}
interface View : BaseView {
fun setViewTitle(titleMonth: Calendar)
fun setWeekLabels(weekLabels: List<String>)
interface BaseView {
@LayoutRes
fun getContentResource(): Int
fun init(view: View, @Nullable state: Bundle?)
}