From 788a02b2b41270f25cae22ce2d8024b64fc99af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 24 Nov 2024 18:01:15 +0100 Subject: [PATCH] start working on room --- app/build.gradle | 16 +- .../timedsilence/model/data/ScheduleObject.kt | 30 ++- .../model/database/DatabaseHandler.kt | 229 +++--------------- .../model/database/room/ScheduleDao.kt | 46 ++++ build.gradle | 6 +- 5 files changed, 121 insertions(+), 206 deletions(-) create mode 100644 app/src/main/java/de/felixnuesse/timedsilence/model/database/room/ScheduleDao.kt diff --git a/app/build.gradle b/app/build.gradle index 9d0518ab..ad9a4fb1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,3 +1,7 @@ +plugins { + id 'com.google.devtools.ksp' +} + apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlinx-serialization' @@ -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' @@ -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") } diff --git a/app/src/main/java/de/felixnuesse/timedsilence/model/data/ScheduleObject.kt b/app/src/main/java/de/felixnuesse/timedsilence/model/data/ScheduleObject.kt index ac5f5cd2..087a0d47 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/model/data/ScheduleObject.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/model/data/ScheduleObject.kt @@ -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 @@ -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, @@ -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 { diff --git a/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt b/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt index 1174f0e7..dd9ab4e4 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt @@ -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 @@ -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 @@ -163,64 +165,6 @@ class DatabaseHandler (context: Context) : SQLiteOpenHelper(context, DATABASE_NA } } - - fun getAllSchedules(): ArrayList { - if (cachedSchedules.cacheInitialized && cachingEnabled) { - return cachedSchedules - } - - val db = readableDatabase - - // Filter results WHERE "title" = 'My Title' - val selection = "" - val selectionArgs = arrayOf() - - // 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() - 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 { - if (cachedSchedules.cacheInitialized && cachingEnabled) { - return cachedSchedules.filter { it.isValidOnWeekday(weekday) } as ArrayList - } - return getAllSchedules().filter { it.isValidOnWeekday(weekday) } as ArrayList - } - - fun intToBool(value: Int): Boolean{ if(value==0){ return false @@ -228,167 +172,48 @@ class DatabaseHandler (context: Context) : SQLiteOpenHelper(context, DATABASE_NA 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() - 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 { + return ArrayList(db.scheduleDao().getAllSchedules()) } - private fun getScheduleProjection(): Array { - 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 { + return getAllSchedules().filter { it.isValidOnWeekday(weekday) } as ArrayList } - /** - * 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( - 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 { diff --git a/app/src/main/java/de/felixnuesse/timedsilence/model/database/room/ScheduleDao.kt b/app/src/main/java/de/felixnuesse/timedsilence/model/database/room/ScheduleDao.kt new file mode 100644 index 00000000..3857379c --- /dev/null +++ b/app/src/main/java/de/felixnuesse/timedsilence/model/database/room/ScheduleDao.kt @@ -0,0 +1,46 @@ +package de.felixnuesse.timedsilence.model.database.room + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import de.felixnuesse.timedsilence.model.data.ScheduleObject + +@Dao +interface ScheduleDao { + + @Query("SELECT * FROM timetable") + fun getAllSchedules(): List + + + @Query("SELECT * FROM timetable") + fun getSchedulesForWeekday(): List + + + @Query("SELECT * FROM timetable WHERE id LIKE :id LIMIT 1") + fun getScheduleByID(id: Long): ScheduleObject + + + /** + * Deletes ScheduleObject with the given id + * @param id Switch to delete + * @return amount of rows affected + */ + @Query("DELETE FROM timetable WHERE id LIKE :id") + fun deleteScheduleEntry(id: Long) + + @Delete + fun deleteScheduleEntry(scheduleObject: ScheduleObject) + + /** + * Creates a switch entry + * @param so Switch to create + * @return id + */ + @Insert + fun createScheduleEntry(so: ScheduleObject): Long + + @Update + fun updateScheduleEntry(so: ScheduleObject) +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index ecccd63e..0f8cd215 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,4 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. - buildscript { ext.kotlin_version = '2.0.20' repositories { @@ -16,6 +15,11 @@ buildscript { // in the individual module build.gradle files } } + +plugins { + id 'com.google.devtools.ksp' version '2.0.21-1.0.27' apply false +} + allprojects { repositories { google()