Skip to content

Commit

Permalink
start working on room
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Nov 24, 2024
1 parent d824c7c commit 788a02b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 206 deletions.
16 changes: 14 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'com.google.devtools.ksp'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlinx-serialization'
Expand Down Expand Up @@ -56,7 +60,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Expand All @@ -67,7 +71,15 @@ dependencies {
implementation 'com.github.AppIntro:AppIntro:6.3.1'
implementation 'androidx.preference:preference-ktx:1.2.1'

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation("androidx.core:core-splashscreen:1.0.1")


// Room:
implementation("androidx.room:room-runtime:2.6.1")
implementation 'androidx.room:room-common:2.6.1'
implementation 'androidx.room:room-ktx:2.6.1'
ksp("androidx.room:room-compiler:2.6.1")
annotationProcessor("androidx.room:room-compiler:2.6.1")
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.felixnuesse.timedsilence.model.data

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable
import java.time.DayOfWeek

Expand Down Expand Up @@ -31,7 +34,25 @@ import java.time.DayOfWeek
*
*/
@Serializable
data class ScheduleObject(var name: String, var timeStart: Long, var timeEnd: Long, var timeSetting: Int, var id: Long) {
@Entity(tableName="timetable")
data class ScheduleObject(

@ColumnInfo(name = "SCHEDULE_NAME")
var name: String?,

@ColumnInfo(name = "time_start")
var timeStart: Long?,

@ColumnInfo(name = "time_end")
var timeEnd: Long?,

@ColumnInfo(name = "schedule_volume")
var timeSetting: Int?,

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id: Long?
) {


constructor(name: String, timeStart: Long, timeEnd: Long, timeSetting: Int, id: Long,
Expand All @@ -48,12 +69,19 @@ data class ScheduleObject(var name: String, var timeStart: Long, var timeEnd: Lo

}

@ColumnInfo(name = "schedule_active_mon")
var mon: Boolean = false
@ColumnInfo(name = "schedule_active_tue")
var tue: Boolean = false
@ColumnInfo(name = "schedule_active_wed")
var wed: Boolean = false
@ColumnInfo(name = "schedule_active_thu")
var thu: Boolean = false
@ColumnInfo(name = "schedule_active_fri")
var fri: Boolean = false
@ColumnInfo(name = "schedule_active_sat")
var sat: Boolean = false
@ColumnInfo(name = "schedule_active_sun")
var sun: Boolean = false

fun isValidOnWeekday(weekday: DayOfWeek): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import de.felixnuesse.timedsilence.model.database.DatabaseInfo.Companion.SCHEDUL
import android.content.ContentValues
import android.database.Cursor
import android.util.Log
import androidx.room.Room
import de.felixnuesse.timedsilence.Constants.Companion.REASON_MANUALLY_SET
import de.felixnuesse.timedsilence.extensions.TAG
import de.felixnuesse.timedsilence.extensions.e
Expand Down Expand Up @@ -54,6 +55,7 @@ import de.felixnuesse.timedsilence.model.database.DatabaseInfo.Companion.WIFI_SS
import de.felixnuesse.timedsilence.model.database.DatabaseInfo.Companion.WIFI_TABLE
import de.felixnuesse.timedsilence.model.database.DatabaseInfo.Companion.WIFI_TYPE
import de.felixnuesse.timedsilence.model.database.DatabaseInfo.Companion.WIFI_VOL_MODE
import de.felixnuesse.timedsilence.model.database.room.AppDatabase
import java.time.DayOfWeek


Expand Down Expand Up @@ -163,232 +165,55 @@ class DatabaseHandler (context: Context) : SQLiteOpenHelper(context, DATABASE_NA
}
}


fun getAllSchedules(): ArrayList<ScheduleObject> {
if (cachedSchedules.cacheInitialized && cachingEnabled) {
return cachedSchedules
}

val db = readableDatabase

// Filter results WHERE "title" = 'My Title'
val selection = ""
val selectionArgs = arrayOf<String>()

// How you want the results sorted in the resulting Cursor
val sortOrder = SCHEDULE_ID + " ASC"

val cursor = db.query(
SCHEDULE_TABLE, // The table to query
getScheduleProjection(), // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // don't group the rows
null, null, // don't filter by row groups
sortOrder // The sort order
)// The values for the WHERE clause
val results = arrayListOf<ScheduleObject>()
while (cursor.moveToNext()) {
val so = ScheduleObject(
cursor.getString(1),
cursor.getLong(2),
cursor.getLong(3),
cursor.getInt(4),
cursor.getLong(0),
intToBool(cursor.getInt(5)),
intToBool(cursor.getInt(6)),
intToBool(cursor.getInt(7)),
intToBool(cursor.getInt(8)),
intToBool(cursor.getInt(9)),
intToBool(cursor.getInt(10)),
intToBool(cursor.getInt(11))
)
results.add(so)
}
cursor.close()

db.close()
cachedSchedules.set(results)
//Log.e("Database", "content: ${results.size}")
//Log.e("Database", "content: ${cachedSchedules.size}")
return cachedSchedules
}

fun getSchedulesForWeekday(weekday: DayOfWeek): ArrayList<ScheduleObject> {
if (cachedSchedules.cacheInitialized && cachingEnabled) {
return cachedSchedules.filter { it.isValidOnWeekday(weekday) } as ArrayList<ScheduleObject>
}
return getAllSchedules().filter { it.isValidOnWeekday(weekday) } as ArrayList<ScheduleObject>
}


fun intToBool(value: Int): Boolean{
if(value==0){
return false
}
return true
}

@Deprecated("This is not cached!")
fun getScheduleByID(id: Long): ScheduleObject {
val db = readableDatabase

val selection = SCHEDULE_ID + " = ?"
val selectionArgs = arrayOf(id.toString())

val sortOrder = SCHEDULE_ID + " DESC"


val cursor = db.query(
SCHEDULE_TABLE, // The table to query
getScheduleProjection(), // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // don't group the rows
null, null, // don't filter by row groups
sortOrder // The sort order
)// The values for the WHERE clause

val results = arrayListOf<ScheduleObject>()
while (cursor.moveToNext()) {

val so = ScheduleObject(
cursor.getString(1),
cursor.getLong(2),
cursor.getLong(3),
cursor.getInt(4),
cursor.getLong(0),
intToBool(cursor.getInt(5)),
intToBool(cursor.getInt(6)),
intToBool(cursor.getInt(7)),
intToBool(cursor.getInt(8)),
intToBool(cursor.getInt(9)),
intToBool(cursor.getInt(10)),
intToBool(cursor.getInt(11))
)
val db = Room.databaseBuilder(
context,
AppDatabase::class.java,
DATABASE_NAME
).build()


results.add(so)
}
cursor.close()
return results.get(0)
/* ######################### */
/* ROOM DB - SCHEDULE OBJECT */
/* ######################### */
fun getAllSchedules(): ArrayList<ScheduleObject> {
return ArrayList(db.scheduleDao().getAllSchedules())
}

private fun getScheduleProjection(): Array<String> {
return arrayOf(
SCHEDULE_ID,
SCHEDULE_NAME,
SCHEDULE_START,
SCHEDULE_END,
SCHEDULE_SETTING,
SCHEDULE_MON,
SCHEDULE_TUE,
SCHEDULE_WED,
SCHEDULE_THU,
SCHEDULE_FRI,
SCHEDULE_SAT,
SCHEDULE_SUN
)
fun getSchedulesForWeekday(weekday: DayOfWeek): ArrayList<ScheduleObject> {
return getAllSchedules().filter { it.isValidOnWeekday(weekday) } as ArrayList<ScheduleObject>
}

/**
* Deletes ScheduleObject with the given id
* @param id Switch to delete
* @return amount of rows affected
*/
fun deleteScheduleEntry(id: Long): Int {
val db = writableDatabase
// Define 'where' part of query.
val selection = SCHEDULE_ID + " LIKE ?"
// Specify arguments in placeholder order.
val selectionArgs = arrayOf(id.toString())

// Issue SQL statement.
val retcode: Int = db.delete(SCHEDULE_TABLE, selection, selectionArgs)
db.close()

return retcode


@Deprecated("This is not cached!")
fun getScheduleByID(id: Long): ScheduleObject {
return db.scheduleDao().getScheduleByID(id)
}

fun deleteScheduleEntry(id: Long) {
db.scheduleDao().deleteScheduleEntry(id)
}

/**
* Creates a switch entry
* @param so Switch to create
* @return id
*/
fun createScheduleEntry(so: ScheduleObject): ScheduleObject {
val db = writableDatabase

// Create a new map of values, where column names are the keys
val values = ContentValues()
//values.put(SCHEDULE_ID, so.id)
values.put(SCHEDULE_NAME, so.name)
values.put(SCHEDULE_START, so.timeStart)
values.put(SCHEDULE_END, so.timeEnd)
values.put(SCHEDULE_SETTING, so.timeSetting)
values.put(SCHEDULE_MON, so.mon)
values.put(SCHEDULE_TUE, so.tue)
values.put(SCHEDULE_WED, so.wed)
values.put(SCHEDULE_THU, so.thu)
values.put(SCHEDULE_FRI, so.fri)
values.put(SCHEDULE_SAT, so.sat)
values.put(SCHEDULE_SUN, so.sun)

// Insert the new row, returning the primary key value of the new row
val newRowId = db.insert(SCHEDULE_TABLE, null, values)

val newObject = ScheduleObject("",0,0,0,newRowId)

newObject.name=so.name
newObject.timeStart=so.timeStart
newObject.timeEnd=so.timeEnd
newObject.timeSetting=so.timeSetting
newObject.mon=so.mon
newObject.tue=so.tue
newObject.wed=so.wed
newObject.thu=so.thu
newObject.fri=so.fri
newObject.sat=so.sat
newObject.sun=so.sun

db.close()
return newObject
val id = db.scheduleDao().createScheduleEntry(so)
return db.scheduleDao().getScheduleByID(id)

}

fun updateScheduleEntry(so: ScheduleObject) {
val db = writableDatabase

// Create a new map of values, where column names are the keys
val values = ContentValues()
values.put(SCHEDULE_ID, so.id)
values.put(SCHEDULE_NAME, so.name)
values.put(SCHEDULE_START, so.timeStart)
values.put(SCHEDULE_END, so.timeEnd)
values.put(SCHEDULE_SETTING, so.timeSetting)
values.put(SCHEDULE_MON, so.mon)
values.put(SCHEDULE_TUE, so.tue)
values.put(SCHEDULE_WED, so.wed)
values.put(SCHEDULE_THU, so.thu)
values.put(SCHEDULE_FRI, so.fri)
values.put(SCHEDULE_SAT, so.sat)
values.put(SCHEDULE_SUN, so.sun)

val idofchangedobject = arrayOf<String>(
so.id.toString()
)

// Insert the new row, returning the primary key value of the new row
db.update(
SCHEDULE_TABLE,
values,
SCHEDULE_ID + " = ?",
idofchangedobject
)
db.scheduleDao().updateScheduleEntry(so)
}

db.close()
/* ############################# */
/* ROOM DB - SCHEDULE OBJECT - END */
/* ############################# */

}


fun getAllWifiEntries(): ArrayList<WifiObject> {
Expand Down
Loading

0 comments on commit 788a02b

Please sign in to comment.