package me.eternal.purrfect.storage import android.database.sqlite.SQLiteDatabase import me.eternal.purrfect.RemoteSideContext import me.eternal.purrfect.common.data.MessagingFriendInfo import me.eternal.purrfect.common.data.MessagingGroupInfo import me.eternal.purrfect.common.util.SQLiteDatabaseHelper import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import java.util.concurrent.ExecutorService import java.util.concurrent.Executors class AppDatabase( val context: RemoteSideContext, ) { val executor: ExecutorService = Executors.newSingleThreadExecutor() lateinit var database: SQLiteDatabase // Multi-subscriber event stream for messaging data updates val messagingDataFlow = MutableSharedFlow, List>>( extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST ) fun executeAsync(block: () -> Unit) { executor.execute { runCatching { block() }.onFailure { context.log.error("Failed to execute async block", it) } } } fun init() { database = context.androidContext.openOrCreateDatabase("main.db", 0, null) SQLiteDatabaseHelper.createTablesFromSchema(database, mapOf( "friends" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "userId CHAR(36) UNIQUE", "dmConversationId VARCHAR(36)", "displayName VARCHAR", "mutableUsername VARCHAR", "bitmojiId VARCHAR", "selfieId VARCHAR" ), "groups" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "conversationId CHAR(36) UNIQUE", "name VARCHAR", "participantsCount INTEGER" ), "rules" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "type VARCHAR", "targetUuid VARCHAR" ), "streaks" to listOf( "id VARCHAR PRIMARY KEY", "notify BOOLEAN", "expirationTimestamp BIGINT", "length INTEGER" ), "enabled_scripts" to listOf( "name VARCHAR PRIMARY KEY", ), "tracker_rules" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "enabled BOOLEAN DEFAULT 1", "name VARCHAR", "author VARCHAR", ), "tracker_scopes" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "rule_id INTEGER", "scope_type VARCHAR", "scope_id CHAR(36)" ), "tracker_rules_events" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "rule_id INTEGER", "flags INTEGER DEFAULT 1", "event_type VARCHAR", "params TEXT", "actions TEXT" ), "friend_scores" to listOf( "userId CHAR(36) PRIMARY KEY", "score BIGINT" ), "quick_tiles" to listOf( "key VARCHAR PRIMARY KEY", "position INTEGER", ), "location_coordinates" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "name VARCHAR", "latitude DOUBLE", "longitude DOUBLE", "radius DOUBLE", ), "themes" to listOf( "id INTEGER PRIMARY KEY AUTOINCREMENT", "enabled BOOLEAN DEFAULT 0", "name VARCHAR", "description TEXT", "version VARCHAR", "author VARCHAR", "updateUrl VARCHAR", "content TEXT", ), "repositories" to listOf( "url VARCHAR", "type VARCHAR", "PRIMARY KEY (url, type)" ), "notes" to listOf( "id CHAR(36) PRIMARY KEY", "content TEXT", ), "assistant_registry" to listOf( "id VARCHAR PRIMARY KEY", "kind VARCHAR", "title VARCHAR", "category VARCHAR", "path TEXT", "description TEXT", "settingKey VARCHAR", "screenRoute VARCHAR", "allowedActions TEXT", "allowedValues TEXT", "aliases TEXT", "commonTypos TEXT", "examples TEXT", "searchTokens TEXT", ), )) } }