Skip to content
Open
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
Expand Up @@ -4,8 +4,10 @@ import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import io.github.lordofpolls.shellwave.core.db.entities.ColorSchemeEntity
import io.github.lordofpolls.shellwave.terminal.DEFAULT_COLOR_SCHEME
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -30,4 +32,19 @@ interface ColorSchemeDao {

@Delete
suspend fun delete(scheme: ColorSchemeEntity)

/**
* Inserts a new per-host row and returns it.
*
* The app-wide default is the lowest-id row and Settings only inserts it on the first edit, so
* a row inserted into an empty table would become the default for every host. The default is
* therefore written first when missing. One transaction keeps two quick calls from inserting
* two defaults.
*/
@Transaction
suspend fun insertOverride(): ColorSchemeEntity {
if (getDefault() == null) insert(DEFAULT_COLOR_SCHEME.copy(id = 0))
val fresh = DEFAULT_COLOR_SCHEME.copy(id = 0, name = "Custom")
return fresh.copy(id = insert(fresh))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import io.github.lordofpolls.shellwave.core.db.entities.TerminalProfileEntity
import io.github.lordofpolls.shellwave.terminal.DEFAULT_TERMINAL_PROFILE
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -30,4 +32,19 @@ interface TerminalProfileDao {

@Delete
suspend fun delete(profile: TerminalProfileEntity)

/**
* Inserts a new per-host row and returns it.
*
* The app-wide default is the lowest-id row and Settings only inserts it on the first edit, so
* a row inserted into an empty table would become the default for every host. The default is
* therefore written first when missing. One transaction keeps two quick calls from inserting
* two defaults.
*/
@Transaction
suspend fun insertOverride(): TerminalProfileEntity {
if (getDefault() == null) insert(DEFAULT_TERMINAL_PROFILE.copy(id = 0))
val fresh = DEFAULT_TERMINAL_PROFILE.copy(id = 0, name = "Host override")
return fresh.copy(id = insert(fresh))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ import io.github.lordofpolls.shellwave.ssh.publicKeyLineOf
import io.github.lordofpolls.shellwave.ssh.readKeyText
import io.github.lordofpolls.shellwave.ssh.resolveProxyHops
import io.github.lordofpolls.shellwave.terminal.DEFAULT_COLOR_SCHEME
import io.github.lordofpolls.shellwave.terminal.DEFAULT_TERMINAL_PROFILE
import io.github.lordofpolls.shellwave.ui.design.BackTopBar
import io.github.lordofpolls.shellwave.ui.design.CollapsibleSection
import io.github.lordofpolls.shellwave.ui.design.ColorPickerDialog
Expand Down Expand Up @@ -217,8 +216,7 @@ fun AddEditHostScreen(
fun enableProfileOverride() {
overridesToggled = true
scope.launch {
val fresh = DEFAULT_TERMINAL_PROFILE.copy(id = 0, name = "Host override")
terminalProfileOverride = fresh.copy(id = terminalProfileDao.insert(fresh))
terminalProfileOverride = terminalProfileDao.insertOverride()
}
}

Expand All @@ -237,8 +235,7 @@ fun AddEditHostScreen(
fun enableSchemeOverride() {
overridesToggled = true
scope.launch {
val fresh = DEFAULT_COLOR_SCHEME.copy(id = 0, name = "Custom")
colorSchemeOverride = fresh.copy(id = colorSchemeDao.insert(fresh))
colorSchemeOverride = colorSchemeDao.insertOverride()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.github.lordofpolls.shellwave.feature.host

import io.github.lordofpolls.shellwave.core.db.dao.ColorSchemeDao
import io.github.lordofpolls.shellwave.core.db.dao.TerminalProfileDao
import io.github.lordofpolls.shellwave.core.db.entities.ColorSchemeEntity
import io.github.lordofpolls.shellwave.core.db.entities.TerminalProfileEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test

private class InMemoryProfileDao(val rows: MutableMap<Long, TerminalProfileEntity> = mutableMapOf()) :
TerminalProfileDao {
private var nextId = 1L

override suspend fun getAll(): List<TerminalProfileEntity> = rows.values.toList()
override suspend fun getDefault(): TerminalProfileEntity? = rows.minByOrNull { it.key }?.value
override fun observeDefault(): Flow<TerminalProfileEntity?> = TODO()
override suspend fun getById(id: Long): TerminalProfileEntity? = rows[id]
override suspend fun insert(profile: TerminalProfileEntity): Long {
val id = nextId++
rows[id] = profile.copy(id = id)
return id
}
override suspend fun update(profile: TerminalProfileEntity) = TODO()
override suspend fun delete(profile: TerminalProfileEntity) = TODO()
}

private class InMemorySchemeDao(val rows: MutableMap<Long, ColorSchemeEntity> = mutableMapOf()) : ColorSchemeDao {
private var nextId = 1L

override suspend fun getAll(): List<ColorSchemeEntity> = rows.values.toList()
override suspend fun getDefault(): ColorSchemeEntity? = rows.minByOrNull { it.key }?.value
override fun observeDefault(): Flow<ColorSchemeEntity?> = TODO()
override suspend fun getById(id: Long): ColorSchemeEntity? = rows[id]
override suspend fun insert(scheme: ColorSchemeEntity): Long {
val id = nextId++
rows[id] = scheme.copy(id = id)
return id
}
override suspend fun update(scheme: ColorSchemeEntity) = TODO()
override suspend fun delete(scheme: ColorSchemeEntity) = TODO()
}

class InsertOverrideTest {

@Test
fun profileOverride_inEmptyTable_doesNotBecomeDefault() = runBlocking {
val dao = InMemoryProfileDao()
val override = dao.insertOverride()
assertNotEquals(override.id, dao.getDefault()?.id)
assertEquals("Default", dao.getDefault()?.name)
assertEquals(2, dao.rows.size)
}

@Test
fun profileOverride_withExistingDefault_addsOnlyTheOverride() = runBlocking {
val dao = InMemoryProfileDao()
dao.insertOverride()
val defaultId = dao.getDefault()?.id
dao.insertOverride()
assertEquals(defaultId, dao.getDefault()?.id)
assertEquals(3, dao.rows.size)
}

@Test
fun schemeOverride_inEmptyTable_doesNotBecomeDefault() = runBlocking {
val dao = InMemorySchemeDao()
val override = dao.insertOverride()
assertNotEquals(override.id, dao.getDefault()?.id)
assertEquals(2, dao.rows.size)
}

@Test
fun schemeOverride_withExistingDefault_addsOnlyTheOverride() = runBlocking {
val dao = InMemorySchemeDao()
dao.insertOverride()
val defaultId = dao.getDefault()?.id
dao.insertOverride()
assertEquals(defaultId, dao.getDefault()?.id)
assertEquals(3, dao.rows.size)
}
}
Loading