12 Commits

Author SHA1 Message Date
895264de2a [#34] Add contact button and play store button to about page.
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build was killed
closes [#34]
2023-03-10 15:08:38 +01:00
343d1d8e75 [#20] Add missing translation key.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
closes #20
2023-03-07 22:41:49 +01:00
801a17d759 Increase version to 2.1
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-07 20:53:57 +01:00
8e26f6b337 fixBuild (#30)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: fabian/TichuCounter#30
2023-03-04 08:34:26 +01:00
9e658853eb Update Gradle and Java
Some checks failed
continuous-integration/drone/push Build is failing
2023-03-03 15:31:36 +01:00
5a229d6c57 [#29] Disable swap button on invalid score.
Some checks failed
continuous-integration/drone/push Build is failing
closed [#29]
2023-03-03 15:04:02 +01:00
bcc3bd3848 Freeze build image version
Some checks failed
continuous-integration/drone/push Build is failing
2023-03-03 14:28:13 +01:00
17d861403e [#24] Add long press to delete functionality.
Some checks are pending
continuous-integration/drone/push Build is running
Reformat code.
closes #24
2023-03-03 13:20:22 +01:00
a1f344580d [#23] Prevent trailing zeros stacking up.
Some checks failed
continuous-integration/drone/push Build was killed
closes #23
2023-03-03 11:49:39 +01:00
b3bdbfbc05 [#23] Limit input to 5 digits. 2023-03-03 11:27:18 +01:00
a611de6da4 Add instrumented test to test repository together with room database. Improve data handling on first boot.
Some checks failed
continuous-integration/drone/push Build was killed
2023-02-17 15:19:00 +01:00
4108512139 Merge pull request 'release/2.0' (#22) from release/2.0 into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: fabian/TichuCounter#22
2023-01-28 23:30:57 +01:00
19 changed files with 522 additions and 135 deletions

View File

@@ -39,9 +39,10 @@ steps:
from_secret: SeafileApiKey from_secret: SeafileApiKey
APK_FILE: app/build/outputs/apk/release/app-release.apk APK_FILE: app/build/outputs/apk/release/app-release.apk
BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab
SEAFILE_REPO: daffda8b-5840-4a65-b6d0-73b991facfb6 SEAFILE_REPO: 6debeef9-121e-46ba-acc7-81e109fdcbdd
commands: commands:
- 'UPLOAD_URL=$(curl -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/upload-link/ | tr -d "\"")' - 'UPLOAD_URL=$(curl -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/upload-link/ | tr -d "\"")'
- echo $UPLOAD_URL
- 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$APK_FILE -F parent_dir=/ -F relative_path=latest/ -F replace=1 "$UPLOAD_URL"' - 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$APK_FILE -F parent_dir=/ -F relative_path=latest/ -F replace=1 "$UPLOAD_URL"'
- 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$BUNDLE_FILE -F parent_dir=/ -F relative_path=latest/ -F replace=1 "$UPLOAD_URL"' - 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$BUNDLE_FILE -F parent_dir=/ -F relative_path=latest/ -F replace=1 "$UPLOAD_URL"'
@@ -52,7 +53,7 @@ steps:
from_secret: SeafileApiKey from_secret: SeafileApiKey
APK_FILE: app/build/outputs/apk/release/app-release.apk APK_FILE: app/build/outputs/apk/release/app-release.apk
BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab
SEAFILE_REPO: daffda8b-5840-4a65-b6d0-73b991facfb6 SEAFILE_REPO: 6debeef9-121e-46ba-acc7-81e109fdcbdd
commands: commands:
- 'UPLOAD_URL=$(curl -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/upload-link/ | tr -d "\"")' - 'UPLOAD_URL=$(curl -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/upload-link/ | tr -d "\"")'
- 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$APK_FILE -F parent_dir=/ -F relative_path=tagged/$DRONE_TAG/ -F replace=1 "$UPLOAD_URL"' - 'curl -H "Authorization: Token $SEAFILE_API_KEY" -F file=@$APK_FILE -F parent_dir=/ -F relative_path=tagged/$DRONE_TAG/ -F replace=1 "$UPLOAD_URL"'

View File

@@ -16,7 +16,7 @@ def keystoreProperties = new Properties()
def versionProperties = new Properties() def versionProperties = new Properties()
def versionMajor = 2 def versionMajor = 2
def versionMinor = 0 def versionMinor = 1
// Load your keystore.properties file into the keystoreProperties object. // Load your keystore.properties file into the keystoreProperties object.
keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
@@ -72,11 +72,11 @@ android {
} }
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_17
} }
kotlinOptions { kotlinOptions {
jvmTarget = '1.8' jvmTarget = '17'
} }
namespace 'me.zobrist.tichucounter' namespace 'me.zobrist.tichucounter'
packagingOptions { packagingOptions {

View File

@@ -1,22 +0,0 @@
package me.zobrist.tichucounter
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("me.zobrist.tichucounter", appContext.packageName)
}
}

View File

@@ -0,0 +1,275 @@
package me.zobrist.tichucounter
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.test.runTest
import me.zobrist.tichucounter.data.AppDatabase
import me.zobrist.tichucounter.data.GameDao
import me.zobrist.tichucounter.data.RoundDao
import me.zobrist.tichucounter.repository.GameRepository
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.util.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class RepositoryInstrumentedTest {
private lateinit var gameDao: GameDao
private lateinit var roundDao: RoundDao
private lateinit var repository: GameRepository
private lateinit var db: AppDatabase
@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context, AppDatabase::class.java
).build()
roundDao = db.roundDao()
gameDao = db.gameDao()
repository = GameRepository(gameDao, roundDao)
}
@After
@Throws(IOException::class)
fun closeDb() {
db.close()
}
@Test
@Throws(Exception::class)
fun gameInitialisation() = runTest {
repository.getActiveGameFlow().take(1).collect {
assertEquals("TeamA", it.game.nameA)
assertEquals("TeamB", it.game.nameB)
assertTrue(it.game.active)
assertEquals(0, it.rounds.count())
}
}
@Test
@Throws(Exception::class)
fun modifyNames() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
repository.updateActiveTeamName(nameA = "aaa")
repository.getActiveGameFlow().take(1).collect {
assertEquals("aaa", it.game.nameA)
assertEquals("TeamB", it.game.nameB)
}
repository.updateActiveTeamName(nameB = "bbb")
repository.getActiveGameFlow().take(1).collect {
assertEquals("aaa", it.game.nameA)
assertEquals("bbb", it.game.nameB)
}
}
@Test
@Throws(Exception::class)
fun newGame() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.getAllWithRoundFlow().take(1).collect() { it ->
assertEquals(6, it.count())
var uid: Long = 1
it.forEach { game ->
assertEquals(uid++, game.game.uid)
assertEquals(0, game.rounds.count())
}
}
}
@Test
@Throws(Exception::class)
fun setActive() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.getAllWithRoundFlow().take(1).collect() { it ->
val filtered = it.filter { it.game.active }
assertEquals(1, filtered.count())
assertEquals(6, filtered.first().game.uid)
}
repository.setActive(2)
repository.getAllWithRoundFlow().take(1).collect() { it ->
val filtered = it.filter { it.game.active }
assertEquals(1, filtered.count())
assertEquals(2, filtered.first().game.uid)
}
}
@Test
@Throws(Exception::class)
fun addRoundToActiveGame() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.addRoundToActiveGame(1, 1)
repository.addRoundToActiveGame(2, 2)
repository.addRoundToActiveGame(3, 3)
repository.addRoundToActiveGame(4, 4)
repository.addRoundToActiveGame(5, 5)
repository.addRoundToActiveGame(6, 6)
repository.getAllWithRoundFlow().take(1).collect() { it ->
val filtered = it.filter { it.rounds.isNotEmpty() }
assertEquals(1, filtered.count())
assertEquals(6, filtered.first().rounds.count())
}
}
@Test
@Throws(Exception::class)
fun lastRound() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
repository.newGame()
assertNull(repository.getLastRound())
repository.addRoundToActiveGame(1, 1)
repository.addRoundToActiveGame(2, 2)
repository.addRoundToActiveGame(3, 3)
repository.addRoundToActiveGame(4, 4)
repository.addRoundToActiveGame(5, 5)
repository.addRoundToActiveGame(6, 6)
var lastRound = repository.getLastRound()
assertEquals(6, lastRound?.scoreA)
assertEquals(6, lastRound?.scoreB)
repository.deleteLastRound()
lastRound = repository.getLastRound()
assertEquals(5, lastRound?.scoreA)
assertEquals(5, lastRound?.scoreB)
repository.deleteLastRound()
repository.deleteLastRound()
repository.deleteLastRound()
repository.deleteLastRound()
repository.deleteLastRound()
assertNull(repository.getLastRound())
// No error thrown
repository.deleteLastRound()
}
@Test
@Throws(Exception::class)
fun deleteInactive() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
for (i in 1..6) {
repository.newGame()
repository.addRoundToActiveGame(1, 1)
repository.addRoundToActiveGame(2, 2)
repository.addRoundToActiveGame(3, 3)
repository.addRoundToActiveGame(4, 4)
repository.addRoundToActiveGame(5, 5)
repository.addRoundToActiveGame(6, 6)
}
assertEquals(6 * 6, roundDao.getAll().count())
repository.deleteAllInactive()
// Consists of two transactions. Delete games then delete rounds.
repository.getAllWithRoundFlow().take(1).collect() { it ->
assertEquals(1, it.count())
assertEquals(6, it.first().rounds.count())
}
assertEquals(6, roundDao.getAll().count())
}
@Test
@Throws(Exception::class)
fun deleteById() = runTest {
repository.getActiveGameFlow().take(1).collect {
}
for (i in 1..6) {
repository.newGame()
repository.addRoundToActiveGame(1, 1)
repository.addRoundToActiveGame(2, 2)
repository.addRoundToActiveGame(3, 3)
repository.addRoundToActiveGame(4, 4)
repository.addRoundToActiveGame(5, 5)
repository.addRoundToActiveGame(6, 6)
}
// Non existing Id
repository.deleteGame(10)
repository.getAllWithRoundFlow().take(1).collect() { it ->
assertEquals(7, it.count())
}
// Non existing Id
val toDelete: Long = 3
repository.deleteGame(toDelete)
repository.getAllWithRoundFlow().take(1).collect() { it ->
assertEquals(6, it.count())
assertEquals(0, it.count { it.game.uid == toDelete })
}
}
}

View File

@@ -12,7 +12,7 @@ interface GameDao : DaoBase<Game> {
fun getAll(): Flow<List<Game>> fun getAll(): Flow<List<Game>>
@Transaction @Transaction
@Query("SELECT * FROM game where uid ") @Query("SELECT * FROM game")
fun getGamesWithRounds(): Flow<List<GameWithScores>> fun getGamesWithRounds(): Flow<List<GameWithScores>>
@Transaction @Transaction
@@ -20,10 +20,13 @@ interface GameDao : DaoBase<Game> {
fun getActiveWithRounds(): Flow<GameWithScores?> fun getActiveWithRounds(): Flow<GameWithScores?>
@Query("SELECT * FROM game WHERE uid is :gameId") @Query("SELECT * FROM game WHERE uid is :gameId")
fun getGameById(gameId: Long): Flow<Game> fun getGameById(gameId: Long): Game
@Query("SELECT * FROM game WHERE active is 1") @Query("SELECT * FROM game WHERE active is 1")
fun getActive(): Flow<Game?> fun getActiveAsFlow(): Flow<Game?>
@Query("SELECT * FROM game WHERE active is 1")
fun getActive(): Game?
@Query("UPDATE game SET active = 1 WHERE uid is :gameId;") @Query("UPDATE game SET active = 1 WHERE uid is :gameId;")

View File

@@ -8,10 +8,10 @@ import me.zobrist.tichucounter.data.entity.Round
@Entity @Entity
data class GameWithScores( data class GameWithScores(
@Embedded val game: Game, @Embedded val game: Game = Game(),
@Relation( @Relation(
parentColumn = "uid", parentColumn = "uid",
entityColumn = "gameId" entityColumn = "gameId"
) )
val rounds: List<Round> val rounds: List<Round> = emptyList()
) )

View File

@@ -6,10 +6,10 @@ import java.util.*
@Entity @Entity
data class Game( data class Game(
var active: Boolean, var active: Boolean = true,
var nameA: String, var nameA: String = "TeamA",
var nameB: String, var nameB: String = "TeamB",
val created: Date, val created: Date = Date(),
var modified: Date, var modified: Date = Date(),
@PrimaryKey(autoGenerate = true) override val uid: Long = 0 @PrimaryKey(autoGenerate = true) override val uid: Long = 0
) : IEntity ) : IEntity

View File

@@ -5,7 +5,7 @@ import androidx.navigation.*
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
fun NavController.navigate(route: Route) { fun NavController.navigate(route: Route) {
this.navigate(route.name){ this.navigate(route.name) {
// Pop up to the start destination of the graph to // Pop up to the start destination of the graph to
// avoid building up a large stack of destinations // avoid building up a large stack of destinations
// on the back stack as users select items // on the back stack as users select items

View File

@@ -0,0 +1,12 @@
package me.zobrist.tichucounter.domain
fun String.digitCount(): Int {
var count = 0
this.forEach {
if (it.isDigit()) {
count++
}
}
return count
}

View File

@@ -3,6 +3,8 @@ package me.zobrist.tichucounter.repository
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@@ -19,20 +21,15 @@ class GameRepository @Inject constructor(
private val roundDao: RoundDao private val roundDao: RoundDao
) { ) {
private var _activeGame: Game? = null private var activeGame: Game = Game(true, "TeamA", "TeamB", Date(), Date())
val activeGame: Game
get() {
return _activeGame!!
}
init { init {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
gameDao.getActive().collect { gameDao.getActiveAsFlow().collect {
if (it == null) { if (it == null) {
gameDao.insert(Game(true, "TeamA", "TeamB", Date(), Date())) newGame()
} else { } else {
_activeGame = it activeGame = it
} }
} }
} }
@@ -40,16 +37,25 @@ class GameRepository @Inject constructor(
suspend fun newGame() { suspend fun newGame() {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val id = val id = gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
gameDao.insert(Game(true, activeGame.nameA, activeGame.nameB, Date(), Date()))
setActive(id) setActive(id)
} }
} }
suspend fun updateGame(game: Game) { suspend fun updateActiveTeamName(nameA: String? = null, nameB: String? = null) {
game.modified = Date()
val newA = nameA ?: activeGame.nameA
val newB = nameB ?: activeGame.nameB
if (newA == activeGame.nameA && newB == activeGame.nameB) {
return
}
activeGame.modified = Date()
activeGame.nameA = newA
activeGame.nameB = newB
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
gameDao.update(game) gameDao.update(activeGame)
} }
} }
@@ -92,11 +98,10 @@ class GameRepository @Inject constructor(
suspend fun deleteGame(uid: Long) { suspend fun deleteGame(uid: Long) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
try { try {
gameDao.getGameById(uid).take(1).collect { val game = gameDao.getGameById(uid)
gameDao.delete(it) gameDao.delete(game)
val rounds = roundDao.getAllForGame(it.uid) val rounds = roundDao.getAllForGame(game.uid)
roundDao.delete(rounds) roundDao.delete(rounds)
}
} catch (_: NullPointerException) { } catch (_: NullPointerException) {
} }
} }
@@ -107,9 +112,8 @@ class GameRepository @Inject constructor(
try { try {
gameDao.getAll().take(1).collect { games -> gameDao.getAll().take(1).collect { games ->
val activeId = games.first { it.active }.uid val gamesToDelete = games.filter { it.uid != activeGame.uid }
val gamesToDelete = games.filter { !it.active } val roundsToDelete = roundDao.getAll().filter { it.gameId != activeGame.uid }
val roundsToDelete = roundDao.getAll().filter { it.gameId != activeId }
gameDao.delete(gamesToDelete) gameDao.delete(gamesToDelete)
roundDao.delete(roundsToDelete) roundDao.delete(roundsToDelete)
@@ -119,8 +123,8 @@ class GameRepository @Inject constructor(
} }
} }
fun getActiveGameFlow(): Flow<GameWithScores?> { fun getActiveGameFlow(): Flow<GameWithScores> {
return gameDao.getActiveWithRounds() return gameDao.getActiveWithRounds().filter { it != null }.map { it!! }
} }
fun getAllWithRoundFlow(): Flow<List<GameWithScores>> { fun getAllWithRoundFlow(): Flow<List<GameWithScores>> {

View File

@@ -34,17 +34,15 @@ class MainViewModel @Inject constructor(
gameRepository.getActiveGameFlow().collect { gameRepository.getActiveGameFlow().collect {
activeGameHasRounds = it?.rounds?.isNotEmpty() == true activeGameHasRounds = it.rounds.isNotEmpty() == true
if (it != null) { isUndoActionActive = it.rounds.isNotEmpty()
isUndoActionActive = it.rounds.isNotEmpty()
if (expectedRoundCount != it.rounds.count()) { if (expectedRoundCount != it.rounds.count()) {
redoRounds.clear() redoRounds.clear()
}
expectedRoundCount = it.rounds.count()
} }
expectedRoundCount = it.rounds.count()
} }
} }
} }

View File

@@ -2,17 +2,16 @@ package me.zobrist.tichucounter.ui.about
import android.content.res.Configuration import android.content.res.Configuration
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Row import androidx.compose.material.icons.Icons
import androidx.compose.foundation.layout.height import androidx.compose.material.icons.outlined.Mail
import androidx.compose.foundation.layout.padding import androidx.compose.material.icons.outlined.Shop
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.*
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment.Companion.Top import androidx.compose.ui.Alignment.Companion.Top
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
@@ -24,24 +23,62 @@ import me.zobrist.tichucounter.ui.AppTheme
@Composable @Composable
fun AboutView() { fun AboutView() {
Row(Modifier.padding(20.dp)) {
Image(
modifier = Modifier
.height(80.dp)
.padding(end = 10.dp)
.align(Top),
painter = painterResource(R.drawable.app_logo),
contentDescription = null,
contentScale = ContentScale.Fit
)
Column { val uriHandler = LocalUriHandler.current
Text(
text = stringResource(id = R.string.app_name), Column(
style = MaterialTheme.typography.headlineMedium modifier = Modifier
.padding(
top = 20.dp,
start = 20.dp,
end = 20.dp,
bottom = 40.dp
),
) {
Row() {
Image(
modifier = Modifier
.height(80.dp)
.padding(end = 10.dp)
.align(Top),
painter = painterResource(R.drawable.app_logo),
contentDescription = null,
contentScale = ContentScale.Fit
) )
Text(text = "V" + BuildConfig.VERSION_NAME)
Column {
Text(
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.headlineMedium
)
Text(text = "V" + BuildConfig.VERSION_NAME)
}
} }
Button(
modifier = Modifier
.fillMaxWidth()
.padding(top = 30.dp),
onClick = { uriHandler.openUri("market://details?id=me.zobrist.tichucounter") }
) {
Icon(imageVector = Icons.Outlined.Shop, contentDescription = null)
Text(stringResource(id = R.string.play_store))
}
Button(
modifier = Modifier
.fillMaxWidth()
.padding(top = 30.dp),
onClick = { uriHandler.openUri("mailto:app@zobrist.me") }
) {
Icon(imageVector = Icons.Outlined.Mail, contentDescription = null)
Text(stringResource(id = R.string.contact_us))
}
} }
} }

View File

@@ -103,7 +103,7 @@ internal class PreviewViewModel : ICounterViewModel {
override var teamNameB: String = "Team B" override var teamNameB: String = "Team B"
override var currentScoreA: String = "" override var currentScoreA: String = ""
override var currentScoreB: String = "45" override var currentScoreB: String = "45"
override var enableSubmit: Boolean = false override var isValidRound: Boolean = false
override var isAFocused: Boolean = false override var isAFocused: Boolean = false
override var isBFocused: Boolean = false override var isBFocused: Boolean = false
override var requestFocusA: FocusRequester = FocusRequester() override var requestFocusA: FocusRequester = FocusRequester()
@@ -137,9 +137,6 @@ internal class PreviewViewModel : ICounterViewModel {
override fun addSub100Clicked(toAdd: Int) { override fun addSub100Clicked(toAdd: Int) {
} }
override fun deleteClicked() {
}
override fun updateNameA(value: String) { override fun updateNameA(value: String) {
} }
@@ -161,4 +158,7 @@ internal class PreviewViewModel : ICounterViewModel {
override fun showKeyboard() { override fun showKeyboard() {
} }
override fun deleteState(pressed: Boolean) {
}
} }

View File

@@ -7,9 +7,12 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.data.entity.Round import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.domain.Tichu import me.zobrist.tichucounter.domain.Tichu
import me.zobrist.tichucounter.domain.digitCount
import me.zobrist.tichucounter.domain.getTotalPoints import me.zobrist.tichucounter.domain.getTotalPoints
import me.zobrist.tichucounter.repository.GameRepository import me.zobrist.tichucounter.repository.GameRepository
import javax.inject.Inject import javax.inject.Inject
@@ -20,7 +23,7 @@ interface IKeyBoardViewModel {
val currentScoreA: String val currentScoreA: String
val currentScoreB: String val currentScoreB: String
val enableSubmit: Boolean val isValidRound: Boolean
val isAFocused: Boolean val isAFocused: Boolean
val isBFocused: Boolean val isBFocused: Boolean
val requestFocusA: FocusRequester val requestFocusA: FocusRequester
@@ -37,12 +40,12 @@ interface IKeyBoardViewModel {
fun digitClicked(digit: String) fun digitClicked(digit: String)
fun negateClicked() fun negateClicked()
fun addSub100Clicked(toAdd: Int) fun addSub100Clicked(toAdd: Int)
fun deleteClicked()
fun updateFocusStateA(state: Boolean) fun updateFocusStateA(state: Boolean)
fun updateFocusStateB(state: Boolean) fun updateFocusStateB(state: Boolean)
fun swapInputScores() fun swapInputScores()
fun hideKeyboard() fun hideKeyboard()
fun showKeyboard() fun showKeyboard()
fun deleteState(pressed: Boolean)
} }
@@ -84,7 +87,7 @@ class CounterViewModel @Inject constructor(
override var currentScoreB by mutableStateOf("") override var currentScoreB by mutableStateOf("")
private set private set
override var enableSubmit by mutableStateOf(false) override var isValidRound by mutableStateOf(false)
private set private set
override var isAFocused by mutableStateOf(false) override var isAFocused by mutableStateOf(false)
@@ -137,6 +140,10 @@ class CounterViewModel @Inject constructor(
private var lastFocused = Focused.TEAM_A private var lastFocused = Focused.TEAM_A
private var deletePressed = false
private var deleteJob: Job? = null
init { init {
viewModelScope.launch { viewModelScope.launch {
gameRepository.getActiveGameFlow().collect { gameRepository.getActiveGameFlow().collect {
@@ -187,7 +194,7 @@ class CounterViewModel @Inject constructor(
} }
override fun updateSubmitButton() { override fun updateSubmitButton() {
enableSubmit = isValidTichuRound() isValidRound = isValidTichuRound()
} }
override fun submitClicked() { override fun submitClicked() {
@@ -196,13 +203,25 @@ class CounterViewModel @Inject constructor(
} }
currentScoreA = "" currentScoreA = ""
currentScoreB = "" currentScoreB = ""
enableSubmit = false isValidRound = false
} }
override fun digitClicked(digit: String) { override fun digitClicked(digit: String) {
focusLastInput() focusLastInput()
activeValue += digit
if (activeValue.digitCount() >= 5) {
// 5 digits is enough
return
}
val newValue = activeValue + digit
try {
activeValue = newValue.toInt().toString()
} catch (_: NumberFormatException) {
}
updateOtherScore() updateOtherScore()
updateSubmitButton() updateSubmitButton()
} }
@@ -235,27 +254,15 @@ class CounterViewModel @Inject constructor(
updateSubmitButton() updateSubmitButton()
} }
override fun deleteClicked() {
if (activeValue != "") {
activeValue = activeValue.dropLast(1)
}
updateOtherScore()
updateSubmitButton()
}
override fun updateNameA(value: String) { override fun updateNameA(value: String) {
viewModelScope.launch { viewModelScope.launch {
val game = gameRepository.activeGame gameRepository.updateActiveTeamName(nameA = value)
game.nameA = value
gameRepository.updateGame(game)
} }
} }
override fun updateNameB(value: String) { override fun updateNameB(value: String) {
viewModelScope.launch { viewModelScope.launch {
val game = gameRepository.activeGame gameRepository.updateActiveTeamName(nameB = value)
game.nameB = value
gameRepository.updateGame(game)
} }
} }
@@ -286,4 +293,35 @@ class CounterViewModel @Inject constructor(
override fun showKeyboard() { override fun showKeyboard() {
keyboardHidden = false keyboardHidden = false
} }
override fun deleteState(pressed: Boolean) {
deletePressed = pressed
if (deletePressed) {
if (deleteJob?.isActive != true) {
deleteJob = deleteRepeatedlyUntilRelease()
}
} else {
deleteJob?.cancel()
}
}
private fun deleteLastDigitActive() {
if (activeValue != "") {
activeValue = activeValue.dropLast(1)
}
updateOtherScore()
updateSubmitButton()
}
private fun deleteRepeatedlyUntilRelease(): Job {
return viewModelScope.launch {
deleteLastDigitActive()
delay(500)
while (deletePressed) {
deleteLastDigitActive()
delay(100)
}
}
}
} }

View File

@@ -2,6 +2,8 @@ package me.zobrist.tichucounter.ui.counter
import android.content.res.Configuration import android.content.res.Configuration
import androidx.compose.animation.core.* import androidx.compose.animation.core.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Backspace import androidx.compose.material.icons.outlined.Backspace
@@ -30,18 +32,18 @@ fun KeyBoardView(viewModel: IKeyBoardViewModel) {
viewModel.currentScoreB, viewModel.currentScoreB,
viewModel.requestFocusA, viewModel.requestFocusA,
viewModel.requestFocusB, viewModel.requestFocusB,
viewModel.enableSubmit, viewModel.isValidRound,
viewModel.isAFocused, viewModel.isAFocused,
viewModel.isBFocused, viewModel.isBFocused,
{ viewModel.updateFocusStateA(it) }, { viewModel.updateFocusStateA(it) },
{ viewModel.updateFocusStateB(it) }, { viewModel.updateFocusStateB(it) },
{ viewModel.digitClicked(it) }, { viewModel.digitClicked(it) },
{ viewModel.addSub100Clicked(it) }, { viewModel.addSub100Clicked(it) },
{ viewModel.deleteClicked() },
{ viewModel.negateClicked() }, { viewModel.negateClicked() },
{ viewModel.submitClicked() }, { viewModel.submitClicked() },
{ viewModel.hideKeyboard() }, { viewModel.hideKeyboard() },
{ viewModel.swapInputScores() } { viewModel.swapInputScores() },
{ viewModel.deleteState(it) }
) )
} }
@@ -51,18 +53,18 @@ fun KeyboardView(
scoreB: String, scoreB: String,
requestFocusA: FocusRequester, requestFocusA: FocusRequester,
requestFocusB: FocusRequester, requestFocusB: FocusRequester,
enableSubmit: Boolean, isValidScore: Boolean,
focusStateA: Boolean, focusStateA: Boolean,
focusStateB: Boolean, focusStateB: Boolean,
updateFocusStateA: (Boolean) -> Unit, updateFocusStateA: (Boolean) -> Unit,
updateFocusStateB: (Boolean) -> Unit, updateFocusStateB: (Boolean) -> Unit,
digitClicked: (String) -> Unit, digitClicked: (String) -> Unit,
addSub100Clicked: (Int) -> Unit, addSub100Clicked: (Int) -> Unit,
deleteClicked: () -> Unit,
negateClicked: () -> Unit, negateClicked: () -> Unit,
submitClicked: () -> Unit, submitClicked: () -> Unit,
hideKeyboardClicked: () -> Unit, hideKeyboardClicked: () -> Unit,
onSwapClicked: () -> Unit onSwapClicked: () -> Unit,
deleteButtonPressedState: (Boolean) -> Unit
) { ) {
Column { Column {
Row(Modifier.height(IntrinsicSize.Max)) { Row(Modifier.height(IntrinsicSize.Max)) {
@@ -83,7 +85,7 @@ fun KeyboardView(
shape = MaterialTheme.shapes.extraSmall shape = MaterialTheme.shapes.extraSmall
) { ) {
Column { Column {
IconButton(onClick = onSwapClicked) { IconButton(onClick = onSwapClicked, enabled = isValidScore) {
Icon(Icons.Outlined.SwapHoriz, null) Icon(Icons.Outlined.SwapHoriz, null)
} }
} }
@@ -164,9 +166,16 @@ fun KeyboardView(
} }
} }
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
KeyboardIconButton(Icons.Outlined.Backspace) {
deleteClicked() val interactionSource = remember { MutableInteractionSource() }
} val deletePressed by interactionSource.collectIsPressedAsState()
deleteButtonPressedState(deletePressed)
KeyboardIconButton(
icon = Icons.Outlined.Backspace,
interactionSource = interactionSource
) {}
} }
} }
@@ -188,7 +197,7 @@ fun KeyboardView(
} }
} }
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
KeyboardIconButton(Icons.Outlined.Check, enableSubmit) { KeyboardIconButton(Icons.Outlined.Check, isValidScore) {
submitClicked() submitClicked()
} }
} }
@@ -219,7 +228,12 @@ fun KeyboardTextButton(text: String, onClicked: () -> Unit) {
} }
@Composable @Composable
fun KeyboardIconButton(icon: ImageVector, enabled: Boolean = true, onClicked: () -> Unit) { fun KeyboardIconButton(
icon: ImageVector,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
onClicked: () -> Unit
) {
ElevatedButton( ElevatedButton(
onClick = { onClicked() }, onClick = { onClicked() },
@@ -228,6 +242,7 @@ fun KeyboardIconButton(icon: ImageVector, enabled: Boolean = true, onClicked: ()
.height(50.dp) .height(50.dp)
.padding(2.dp), .padding(2.dp),
enabled = enabled, enabled = enabled,
interactionSource = interactionSource
) { ) {
Icon( Icon(
icon, icon,
@@ -308,22 +323,22 @@ fun KeyboardViewPreview() {
AppTheme { AppTheme {
Surface { Surface {
KeyboardView( KeyboardView(
"1", "10",
"3511", "190",
FocusRequester(), FocusRequester(),
FocusRequester(), FocusRequester(),
enableSubmit = false, isValidScore = false,
focusStateA = true, focusStateA = true,
focusStateB = false, focusStateB = false,
updateFocusStateA = {}, updateFocusStateA = {},
updateFocusStateB = {}, updateFocusStateB = {},
digitClicked = {}, digitClicked = {},
addSub100Clicked = {}, addSub100Clicked = {},
deleteClicked = {},
negateClicked = {}, negateClicked = {},
submitClicked = {}, submitClicked = {},
hideKeyboardClicked = {}, hideKeyboardClicked = {},
onSwapClicked = {}) onSwapClicked = {},
deleteButtonPressedState = {})
} }
} }
} }

View File

@@ -22,5 +22,7 @@
<string name="active">Aktives Spiel</string> <string name="active">Aktives Spiel</string>
<string name="inactive">Vergangene Spiele</string> <string name="inactive">Vergangene Spiele</string>
<string name="menu_counter">Counter</string> <string name="menu_counter">Counter</string>
<string name="menu_about">About</string>
<string name="contact_us">Schreib uns</string>
</resources> </resources>

View File

@@ -26,4 +26,6 @@
<string name="inactive">Old Games</string> <string name="inactive">Old Games</string>
<string name="menu_counter">Counter</string> <string name="menu_counter">Counter</string>
<string name="menu_about">About</string> <string name="menu_about">About</string>
<string name="contact_us">Contact us</string>
<string name="play_store" translatable="false">Play Store</string>
</resources> </resources>

View File

@@ -0,0 +1,22 @@
package me.zobrist.tichucounter
import me.zobrist.tichucounter.domain.digitCount
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class StringExtensionTest {
@Test
fun calculation_isCorrect() {
assertEquals(0, "-".digitCount())
assertEquals(0, "".digitCount())
assertEquals(2, "-10".digitCount())
assertEquals(2, "10".digitCount())
assertEquals(10, "1234567890".digitCount())
assertEquals(10, "-1234567890".digitCount())
}
}

View File

@@ -9,7 +9,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.4.0' classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong