-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from babybuddy/new-timer-implementation
New timer implementation
- Loading branch information
Showing
86 changed files
with
2,989 additions
and
1,764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/eu/pkgsoftware/babybuddywidgets/ActivityStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package eu.pkgsoftware.babybuddywidgets | ||
|
||
import android.content.Context | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import eu.pkgsoftware.babybuddywidgets.debugging.GlobalDebugObject | ||
import java.io.IOException | ||
|
||
class ActivityDatabaseV1(context: Context) : SQLiteOpenHelper( | ||
context, "store", null, 1 | ||
) { | ||
override fun onCreate(db: SQLiteDatabase) { | ||
db.execSQL("create table global_kv (key_name text primary key, value text)") | ||
db.execSQL("create table login_kv (key_name text primary key, value text)") | ||
db.execSQL("create table child_kv (child INTEGER, key_name text, value text, primary key (child, key_name))") | ||
} | ||
|
||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
class ActivityStore(context: Context) { | ||
val openHelper = ActivityDatabaseV1(context) | ||
val database = openHelper.writableDatabase | ||
val jackOM = jacksonObjectMapper() | ||
|
||
inline fun <reified K> genSet(table: String, value: K?, selectors: Map<String, String>) { | ||
if (value == null) { | ||
val query = "delete from $table where " + selectors.keys.joinToString(" and ") { "$it = ?" } | ||
val args = selectors.values.toTypedArray() | ||
database.execSQL(query, args) | ||
} else { | ||
val query = "insert or replace into $table (" + | ||
selectors.keys.joinToString(", ") + ", value) values (" + | ||
selectors.keys.joinToString(", ") { "?" } + ", ?)" | ||
val valueString = jackOM.writeValueAsString(value) | ||
database.execSQL(query, selectors.values.toTypedArray() + valueString) | ||
} | ||
} | ||
|
||
inline fun <reified K> genGet(table: String, selectors: Map<String, String>): K? { | ||
val query = "select value from $table where " + selectors.keys.joinToString(" and ") { "$it = ?" } | ||
val cursor = database.rawQuery(query, selectors.values.toTypedArray()) | ||
var value = "" | ||
try { | ||
if (cursor.moveToNext()) { | ||
value = cursor.getString(0) | ||
cursor.close() | ||
return jackOM.readValue(value, K::class.java) | ||
} | ||
} | ||
catch (e: IOException) { | ||
GlobalDebugObject.log("Failed to deserialize value from table $table: '$value'") | ||
} | ||
finally { | ||
cursor.close() | ||
} | ||
return null | ||
} | ||
|
||
inline fun <reified K> globals(key: String): K? { | ||
return genGet("global_kv", mapOf("key_name" to key)) | ||
} | ||
|
||
inline fun <reified K> globals(key: String, value: K) { | ||
genSet("global_kv", value, mapOf("key_name" to key)) | ||
} | ||
|
||
inline fun <reified K> login(key: String): K? { | ||
return genGet("login_kv", mapOf("key_name" to key)) | ||
} | ||
|
||
inline fun <reified K> login(key: String, value: K) { | ||
genSet("login_kv", value, mapOf("key_name" to key)) | ||
} | ||
|
||
inline fun <reified K> child(child: Int, key: String): K? { | ||
return genGet("child_kv", mapOf("child" to child.toString(), "key_name" to key)) | ||
} | ||
|
||
inline fun <reified K> child(child: Int, key: String, value: K) { | ||
genSet("child_kv", value, mapOf("child" to child.toString(), "key_name" to key)) | ||
} | ||
|
||
fun close() { | ||
database.close() | ||
openHelper.close() | ||
} | ||
|
||
fun deleteAllData() { | ||
database.delete("global_kv", "1", arrayOf()) | ||
database.delete("login_kv", "1", arrayOf()) | ||
database.delete("child_kv", "1", arrayOf()) | ||
} | ||
} |
Oops, something went wrong.