Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.example.lambda_bbb_events.data

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.model.local.LocalDatabase
import com.example.lambda_bbb_events.model.local.SqliteRecentlyViewedStore
import com.example.lambda_bbb_events.model.local.SqliteThemePreferenceStore
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

/**
* Tests [SqliteThemePreferenceStore] against a real SQLite database held in memory,
* and that upgrading [LocalDatabase] from version 1 keeps recently viewed campsites.
*
* Reference:
* Anthropic. (2026). Claude Code (Claude Opus 5.5) [Large language model].
* https://claude.com/claude-code
* Written with the help of this AI tool.
* Prompts:
* - "Okay so one thing I found was that dark mode doesn't stay if you select dark mode within
* the app. it disapears after app resets. Let's save it into the sqlite3 db we already have."
* - "Please make sure to also document and cite your changes."
* - "And then add a third button to the settings as system settings."
*/
@RunWith(AndroidJUnit4::class)
class ThemePreferenceDatabaseTest {
private val context = ApplicationProvider.getApplicationContext<Context>()
private lateinit var database: LocalDatabase
private lateinit var store: SqliteThemePreferenceStore

@Before
fun setUp() {
database = LocalDatabase(context, name = null)
store = SqliteThemePreferenceStore(database)
}

@After
fun cleanUp() {
database.close()
}

@Test
fun nothingSavedFollowsSystem() {
assertNull(store.load())
}

@Test
fun savesDarkTheme() {
store.save(true)

assertTrue(store.load()!!)
}

@Test
fun laterChoiceReplacesEarlierOne() {
store.save(true)
store.save(false)

assertFalse(store.load()!!)
}

@Test
fun choosingSystemClearsSavedChoice() {
store.save(true)
store.save(null)

assertNull(store.load())
}

@Test
fun choiceSurvivesReopeningDatabase() {
val name = "theme_test_${System.nanoTime()}.db"
try {
LocalDatabase(context, name).use { SqliteThemePreferenceStore(it).save(true) }

LocalDatabase(context, name).use {
assertTrue(SqliteThemePreferenceStore(it).load()!!)
}
} finally {
context.deleteDatabase(name)
}
}

@Test
fun upgradeFromVersionOneKeepsRecentlyViewed() {
SqliteRecentlyViewedStore(database).record("a", viewedAt = 1L, limit = 10)
val db = database.writableDatabase
db.execSQL("DROP TABLE ${LocalDatabase.SETTINGS_TABLE}")

database.onUpgrade(db, 1, LocalDatabase.DATABASE_VERSION)
store.save(true)

assertEquals(listOf("a"), SqliteRecentlyViewedStore(database).load(10))
assertTrue(store.load()!!)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ import com.example.lambda_bbb_events.view.ui.user_screens.explore.SeeAllCampsite
* Manages application startup such as loading, signup, or main application screens.
*
* @param controllers Shared application controllers.
* @param darkTheme Whether dark mode is enabled.
* @param onDarkThemeChange Updates the application's theme.
* @param darkTheme The theme the user picked: `true` for dark, `false` for light,
* or `null` to follow Android's setting.
* @param onDarkThemeChange Updates the application's theme with the picked option.
*/
@Composable
fun LambdaEventsApp(
controllers: AppControllers,
darkTheme: Boolean,
onDarkThemeChange: (Boolean) -> Unit
darkTheme: Boolean?,
onDarkThemeChange: (Boolean?) -> Unit
) {
if (!controllers.users.usersFullyLoaded) {
// Shown while checking whether this device already has an account
Expand Down Expand Up @@ -156,14 +157,15 @@ fun LambdaEventsApp(
* Defines routes for browsing, bookings, and account management.
*
* @param controllers Shared application controllers.
* @param darkTheme Whether dark mode is enabled.
* @param onDarkThemeChange Updates the application's theme.
* @param darkTheme The theme the user picked: `true` for dark, `false` for light,
* or `null` to follow Android's setting.
* @param onDarkThemeChange Updates the application's theme with the picked option.
*/
@Composable
private fun MainApp(
controllers: AppControllers,
darkTheme: Boolean,
onDarkThemeChange: (Boolean) -> Unit
darkTheme: Boolean?,
onDarkThemeChange: (Boolean?) -> Unit
) {
val navController = rememberNavController()
val currentUser = controllers.users.currentUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.example.lambda_bbb_events.controller.NotificationController
import com.example.lambda_bbb_events.model.local.LocalDatabase
import com.example.lambda_bbb_events.model.local.SharedPreferencesSessionStore
import com.example.lambda_bbb_events.model.local.SqliteRecentlyViewedStore
import com.example.lambda_bbb_events.model.local.SqliteThemePreferenceStore
import com.example.lambda_bbb_events.model.repository.LotteryRepository
import com.example.lambda_bbb_events.model.repository.ReservationRepository
import com.example.lambda_bbb_events.model.repository.ReviewRepository
Expand Down Expand Up @@ -80,6 +81,24 @@ class MainActivity : ComponentActivity() {
}
}

/**
* Builds the repositories and controllers, then shows the app.
*
* Opens one [LocalDatabase] shared by the recently viewed campsites and the
* saved theme. The theme the user picked in the app (light, dark, or System to
* follow Android) is loaded from it before the first frame, and saved to it in
* the background whenever they change it.
*
* Reference:
* Anthropic. (2026). Claude Code (Claude Opus 5.5) [Large language model].
* https://claude.com/claude-code
* Changed with the help of this AI tool (saving the theme choice, System option).
* Prompts:
* - "Okay so one thing I found was that dark mode doesn't stay if you select dark mode within
* the app. it disapears after app resets. Let's save it into the sqlite3 db we already have."
* - "Please make sure to also document and cite your changes."
* - "And then add a third button to the settings as system settings."
*/
@SuppressLint("HardwareIds")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -104,6 +123,9 @@ class MainActivity : ComponentActivity() {
val waitlistEntryRepository = WaitlistEntryRepository()
val reviewRepository = ReviewRepository()
val imageProcessor = ImageProcessor()
val localDatabase = LocalDatabase(applicationContext)
val themePreferenceStore = SqliteThemePreferenceStore(localDatabase)
val savedDarkTheme = themePreferenceStore.load()
val userController = UserController(userRepository, deviceId,SharedPreferencesSessionStore(applicationContext))
val notifications = NotificationController(applicationContext,userController)
notifications.createNotificationChannel()
Expand All @@ -121,9 +143,7 @@ class MainActivity : ComponentActivity() {
users = userController,
campsites = CampsiteController(
campsiteStore,
SqliteRecentlyViewedStore(
LocalDatabase(applicationContext)
),
SqliteRecentlyViewedStore(localDatabase),
lotteriesForCampsite = lotteryRepository::getLotteriesForCampsite
),
lotteries = LotteryController(lotteryRepository),
Expand Down Expand Up @@ -244,10 +264,11 @@ class MainActivity : ComponentActivity() {

// Follow Android's dark mode setting, unless the
// user picked a theme in the app.
// Only their choice is saved, so changing Android's
// setting still takes effect.
// Only their choice is saved (in the local SQLite
// database), so changing Android's setting still
// takes effect until they pick one.
var chosenDarkTheme by rememberSaveable {
mutableStateOf<Boolean?>(null)
mutableStateOf(savedDarkTheme)
}

val darkTheme =
Expand Down Expand Up @@ -276,9 +297,12 @@ class MainActivity : ComponentActivity() {
) {
LambdaEventsApp(
controllers = controllers,
darkTheme = darkTheme,
onDarkThemeChange = {
chosenDarkTheme = it
darkTheme = chosenDarkTheme,
onDarkThemeChange = { choice ->
chosenDarkTheme = choice
lifecycleScope.launch(Dispatchers.IO) {
themePreferenceStore.save(choice)
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,22 @@ class InMemoryRecentlyViewedStore : RecentlyViewedStore {
/**
* The app's local SQLite database on this device.
*
* Tables:
* - [RECENTLY_VIEWED_TABLE]: campsites viewed on this device, used by [SqliteRecentlyViewedStore].
* - [SETTINGS_TABLE]: device settings as key-value pairs, such as the theme saved by
* [SqliteThemePreferenceStore]. Added in version 2.
*
* @param context The application context.
* @param name The database file name, or `null` for a database held in memory.
*
* Reference:
* Anthropic. (2026). Claude Code (Claude Opus 5.5) [Large language model].
* https://claude.com/claude-code
* Changed with the help of this AI tool (settings table, version 2 upgrade).
* Prompts:
* - "Okay so one thing I found was that dark mode doesn't stay if you select dark mode within
* the app. it disapears after app resets. Let's save it into the sqlite3 db we already have."
* - "Please make sure to also document and cite your changes."
*/
class LocalDatabase(
context: Context,
Expand All @@ -74,11 +88,15 @@ class LocalDatabase(

companion object {
const val DATABASE_NAME = "lambda_local.db"
const val DATABASE_VERSION = 1
const val DATABASE_VERSION = 2

const val RECENTLY_VIEWED_TABLE = "recently_viewed"
const val COLUMN_CAMPSITE_ID = "campsite_id"
const val COLUMN_VIEWED_AT = "viewed_at"

const val SETTINGS_TABLE = "settings"
const val COLUMN_KEY = "key"
const val COLUMN_VALUE = "value"
}

override fun onCreate(db: SQLiteDatabase) {
Expand All @@ -90,11 +108,33 @@ class LocalDatabase(
)
""".trimIndent()
)
createSettingsTable(db)
}

/**
* Adds the settings table to databases made before version 2,
* keeping the recently viewed campsites already saved.
*/
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS $RECENTLY_VIEWED_TABLE")
onCreate(db)
if (oldVersion < 2) {
createSettingsTable(db)
}
}

/**
* Creates the table of device settings, one value per key.
*
* @param db The database to create the table in.
*/
private fun createSettingsTable(db: SQLiteDatabase) {
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS $SETTINGS_TABLE (
$COLUMN_KEY TEXT PRIMARY KEY NOT NULL,
$COLUMN_VALUE TEXT NOT NULL
)
""".trimIndent()
)
}
}

Expand Down
Loading
Loading