Pith - wedge_android
wedge_android/app/src/main/java/com/vgmlr/wedge/WedgeModel.kt [5.2 kb]
Modified: 19:18:29 63 026 (21 May 026)
9 Days Ago
package com.vgmlr.wedge

import android.content.Context
import androidx.room.*
import androidx.datastore.preferences.core.*
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import androidx.compose.ui.graphics.Color
import androidx.core.graphics.toColorInt

@Entity(tableName = "notes")
data class NoteEntity(
    @PrimaryKey val id: Int = 1,
    val content: String = "",
    val lastModified: Long = System.currentTimeMillis()
)

@Dao
interface NoteDao {
    @Query("SELECT * FROM notes WHERE id = 1")
    fun getNote(): Flow<NoteEntity?>

    @Query("SELECT * FROM notes WHERE id = 1")
    suspend fun getNoteSync(): NoteEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun save(note: NoteEntity)
}

@Database(entities = [NoteEntity::class], version = 3, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun noteDao(): NoteDao
    companion object {
        @Volatile private var INSTANCE: AppDatabase? = null
        fun getInstance(context: Context): AppDatabase = INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "wedge_db")
                .fallbackToDestructiveMigration()
                .build()
            INSTANCE = instance
            instance
        }
    }
}

private val Context.dataStore by preferencesDataStore("settings")

data class SettingsState(
    val textColor: String = WedgeConfig.TEXT_COLOR_DEFAULT,
    val bgColor: String = WedgeConfig.BG_COLOR_DEFAULT,
    val widgetTextColor: String = WedgeConfig.WIDGET_TEXT_COLOR_DEFAULT,
    val widgetBgColor: String = WedgeConfig.WIDGET_BG_COLOR_DEFAULT,
    val editorFontSize: Float = WedgeConfig.FONT_SIZE_DEFAULT,
    val widgetFontSize: Float = WedgeConfig.WIDGET_FONT_SIZE_DEFAULT,
    val lineHeight: Float = WedgeConfig.LINE_HEIGHT_DEFAULT,
    val boldColor: String = WedgeConfig.BOLD_COLOR_DEFAULT,
    val incognitoMode: Boolean = false
)

class PreferenceManager(private val context: Context) {
    private val keyTextCol = stringPreferencesKey("text_color")
    private val keyBgCol = stringPreferencesKey("bg_color")
    private val keyWTextCol = stringPreferencesKey("widget_text_color")
    private val keyWBgCol = stringPreferencesKey("widget_bg_color")
    private val keyBoldCol = stringPreferencesKey("bold_color")
    private val keyIncognito = booleanPreferencesKey("incognito_mode")
    private val keyESize = floatPreferencesKey("editor_font_size_abs")
    private val keyWSize = floatPreferencesKey("widget_font_size_abs")
    private val keyLHeight = floatPreferencesKey("line_height_multiplier")

    val settings: Flow<SettingsState> = context.dataStore.data.map { prefs ->
        SettingsState(
            textColor = prefs[keyTextCol] ?: WedgeConfig.TEXT_COLOR_DEFAULT,
            bgColor = prefs[keyBgCol] ?: WedgeConfig.BG_COLOR_DEFAULT,
            widgetTextColor = prefs[keyWTextCol] ?: WedgeConfig.WIDGET_TEXT_COLOR_DEFAULT,
            widgetBgColor = prefs[keyWBgCol] ?: WedgeConfig.WIDGET_BG_COLOR_DEFAULT,
            editorFontSize = prefs[keyESize] ?: WedgeConfig.FONT_SIZE_DEFAULT,
            widgetFontSize = prefs[keyWSize] ?: WedgeConfig.WIDGET_FONT_SIZE_DEFAULT,
            lineHeight = prefs[keyLHeight] ?: WedgeConfig.LINE_HEIGHT_DEFAULT,
            boldColor = prefs[keyBoldCol] ?: WedgeConfig.BOLD_COLOR_DEFAULT,
            incognitoMode = prefs[keyIncognito] ?: false
        )
    }

    val textColor: Flow<String> = context.dataStore.data.map { it[keyTextCol] ?: WedgeConfig.TEXT_COLOR_DEFAULT }
    val bgColor: Flow<String> = context.dataStore.data.map { it[keyBgCol] ?: WedgeConfig.BG_COLOR_DEFAULT }
    val widgetTextColor: Flow<String> = context.dataStore.data.map { it[keyWTextCol] ?: WedgeConfig.WIDGET_TEXT_COLOR_DEFAULT }
    val widgetBgColor: Flow<String> = context.dataStore.data.map { it[keyWBgCol] ?: WedgeConfig.WIDGET_BG_COLOR_DEFAULT }
    val boldColor: Flow<String> = context.dataStore.data.map { it[keyBoldCol] ?: WedgeConfig.BOLD_COLOR_DEFAULT }
    val incognitoMode: Flow<Boolean> = context.dataStore.data.map { it[keyIncognito] ?: false }
    val editorFontSize: Flow<Float> = context.dataStore.data.map { it[keyESize] ?: WedgeConfig.FONT_SIZE_DEFAULT }
    val widgetFontSize: Flow<Float> = context.dataStore.data.map { it[keyWSize] ?: WedgeConfig.FONT_SIZE_DEFAULT }
    val lineHeight: Flow<Float> = context.dataStore.data.map { it[keyLHeight] ?: WedgeConfig.LINE_HEIGHT_DEFAULT }
    
    suspend fun setColors(txt: String, bg: String, wtxt: String, wbg: String, bcol: String) = context.dataStore.edit {
        it[keyTextCol] = txt; it[keyBgCol] = bg; it[keyWTextCol] = wtxt; it[keyWBgCol] = wbg; it[keyBoldCol] = bcol
    }

    suspend fun setFontSettings(eSize: Float, wSize: Float, lHeight: Float) = context.dataStore.edit {
        it[keyESize] = eSize; it[keyWSize] = wSize; it[keyLHeight] = lHeight
    }

    suspend fun setIncognito(incognito: Boolean) = context.dataStore.edit {
        it[keyIncognito] = incognito
    }
}

fun parseColor(hex: String): Color {
    return try { Color(hex.toColorInt()) } catch (_: Exception) { if (hex.length > 7) Color.White else Color.Black }
}
Updates
Shim - Android 70.026.1
Wedge - Linux 68.026.1
Wedge - Android 68.026.1
Taper - Linux 64.026.1
Ayh Extension - Chrome 63.026.1
Dev
TVShow (227) 'CSA'
TVShow (228) 'APT'
TVProgram (83) 'BXT'
Miter Update(s)
Shim (Dictation)

Menu
Calendar
Project Tin (024/029)
Miter
RSS Feed
User Avatar
@vgmlr
=SUM(parts)