67 Commits

Author SHA1 Message Date
d1b4ab1e8c WIP: Add setting for counter mode.
All checks were successful
Build Android / build (push) Successful in 7m57s
2023-07-01 11:37:10 +02:00
ff5495249f Merge pull request 'feature/act_runner' (#43) from feature/act_runner into develop
All checks were successful
Build Android / build (push) Successful in 7m41s
Reviewed-on: #43
2023-07-01 11:28:47 +02:00
217370079d Publish tagged builds.
All checks were successful
Build Android / build (push) Successful in 7m58s
Build Android / build (pull_request) Has been cancelled
2023-06-30 16:21:13 +02:00
add100146d Notify on slack.
All checks were successful
Build Android / build (push) Successful in 8m40s
2023-06-30 12:06:24 +02:00
ceebe92f8b Deploy latest to nextcloud.
All checks were successful
Build Android / build (push) Successful in 7m46s
2023-06-30 10:47:19 +02:00
4f87321e2c Use android build box iamge.
All checks were successful
Build Android / build (push) Successful in 7m25s
2023-06-30 10:33:02 +02:00
81c540c2a5 Add signing and version code.
Some checks failed
Build Android / build (push) Failing after 10m19s
2023-06-25 20:02:44 +02:00
a45043424e Use act runner
All checks were successful
Build Android / build (push) Has been cancelled
2023-06-25 19:48:31 +02:00
18326e952f Merge pull request 'release/2.2' (#40) from release/2.2 into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #40
2023-06-16 11:11:33 +02:00
a099659b2c Try to make DRONE_TAG work.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2023-06-02 22:16:40 +02:00
61745c95a4 Fix tagged upload url
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-06-02 16:58:36 +02:00
164cf6900f fix tagged upload link
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-02 15:03:13 +02:00
89ab0afaf5 upload to nextcloud instead of seafile
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is failing
fix nextcloud upload
2023-06-02 14:40:23 +02:00
9d8de2dd3c Update dependencies. Code clean up.
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build is passing
2023-05-25 18:09:16 +02:00
fcb8c12454 [#39] Case insensitive team name suggestions.
Some checks failed
continuous-integration/drone Build is failing
closes #39
2023-05-25 17:37:30 +02:00
34e64e6e50 Update Version to 2.2
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-05-16 12:07:52 +02:00
694989a917 Merge pull request 'feature/#14_team_name_suggestions' (#38) from feature/#14_team_name_suggestions into develop
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: fabian/TichuCounter#38
2023-05-16 12:05:49 +02:00
1e8e72557d Move suggestion generation to ViewModel.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2023-05-11 23:40:17 +02:00
2e40d0315c Don't show dropdown when empty. 2023-05-11 23:37:12 +02:00
9ddbdb698c Better filtering and sorting of suggestions. 2023-05-11 23:06:29 +02:00
c40d473fa5 Update value of team name immediately. 2023-05-11 23:05:47 +02:00
507944794f Clear focus on opening drawer. 2023-05-11 23:04:21 +02:00
068e36c5f6 Merge commit '7e722feb275cdb7d47e87413ee7b11efc7b4ec51' into feature/#14_team_name_suggestions
Some checks failed
continuous-integration/drone Build is failing
2023-05-08 19:30:16 +02:00
7e722feb27 Update 'app/src/main/AndroidManifest.xml'
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-07 22:41:51 +02:00
fb4c83d60a Fix display of app logo on About page.
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-07 20:50:42 +02:00
0c80e05f5c Fix logo not found.
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-05 20:10:55 +02:00
8ca82d1734 Merge branch 'develop' into feature/#14_team_name_suggestions
Some checks failed
continuous-integration/drone/push Build was killed
2023-05-05 19:53:55 +02:00
9385deeb24 [#25] Use vector as app logo. Enable monochrom logo.
Some checks failed
continuous-integration/drone/push Build is failing
closes #25
2023-05-05 19:50:58 +02:00
fadd206e9e Update dependencies. 2023-05-05 19:44:50 +02:00
6768391caf Update dependencies. 2023-05-05 19:44:12 +02:00
aa3e68dc5c Merge pull request 'release/2.1' (#35) from release/2.1 into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: fabian/TichuCounter#35
2023-03-10 17:21:32 +01:00
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
3ec8c27b96 First draft
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-10 13:19:09 +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
661b88b961 Keep back stack of navigation controller clean.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2023-01-28 13:06:50 +01:00
c41816898e Add about page.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 12:33:45 +01:00
2e8d6a7a4e Add App name to navigation drawer.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 11:19:37 +01:00
ae0f85bec0 Rename screen to DrawerItem
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 10:25:07 +01:00
e1e25ff607 Move drawerContent to own file.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 10:14:06 +01:00
6aedb0d7f9 Navigate with enum defines. Create new TopBarState
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 10:06:58 +01:00
48374c5980 Move new game back to app bar.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-28 00:18:26 +01:00
02213f41b6 Add default android locale. Simplify language settings.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-27 23:09:08 +01:00
9ae0890f71 Restyle history page
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-27 20:52:17 +01:00
cd8f1959af Simplify history page. Fix warnings.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-27 20:13:14 +01:00
58d4fc0e43 Show blinking cursor in keyboard view. Give focus back to last focused input.
All checks were successful
continuous-integration/drone/push Build is passing
closes #12
2023-01-27 18:07:16 +01:00
c71b608a7b [#13] Hide keyboard in landscape.
All checks were successful
continuous-integration/drone/push Build is passing
closes #13
2023-01-27 13:18:00 +01:00
89f2e3ecd5 Improve default color scheme.
Some checks are pending
continuous-integration/drone/push Build is pending
2023-01-27 13:11:36 +01:00
f52bfa64ce [#18] Add a swap score button.
Some checks are pending
continuous-integration/drone/push Build is pending
closes #18
2023-01-27 12:37:59 +01:00
4346af3d2b Improve settings composable. Remove delay before setting the theme. as this did not help [#11]
Some checks are pending
continuous-integration/drone/push Build is pending
2023-01-27 12:03:36 +01:00
ca88bd1054 Move ui variable from viewModel to compose function. Move TopBar to separate file. 2023-01-27 10:22:32 +01:00
c54f63736e Enable drawer close gesture.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-22 21:42:38 +01:00
8d24e46687 Simplify database handling.
Some checks are pending
continuous-integration/drone/push Build is running
2023-01-22 21:40:47 +01:00
f40b66077b [#11] Add a small delay before applying theme so compose has enough time to update remember states before the theme is applied.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-22 20:45:45 +01:00
9ca830a707 [#11] ApplyDayNight should not be used.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-22 18:36:38 +01:00
db58e475d1 [#11] Change how settings adapter work. Directly set system settings in MainActivity.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-22 17:57:51 +01:00
33e57bcfd7 Show fab only on counter screen.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-21 18:21:40 +01:00
c1567efe52 [#11] Close dropdown list first. the callback might restart the application on a settings change and keep the menu open.
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-21 18:12:30 +01:00
81 changed files with 2156 additions and 894 deletions

View File

@@ -1,74 +0,0 @@
---
kind: pipeline
type: docker
name: Android
steps:
- name: prepare signing
image: busybox
environment:
STOREPASSWORD:
from_secret: StorePassword
KEYPASSWORD:
from_secret: KeyPassword
commands:
- touch keystore.properties
- echo "storePassword=$STOREPASSWORD" >> keystore.properties
- echo "keyPassword=$KEYPASSWORD" >> keystore.properties
- echo "keyAlias=key0" >> keystore.properties
- echo "storeFile=../AndroidKey" >> keystore.properties
- name: generate versionCode
image: busybox
commands:
- touch version.properties
- let timestamp=$(date +%s)/10
- echo "versionCode=$timestamp" >> version.properties
- name: build
image: mingc/android-build-box
commands:
- ./gradlew test
- ./gradlew assembleRelease
- ./gradlew bundleRelease
- name: deploy latest build
image: curlimages/curl
environment:
SEAFILE_API_KEY:
from_secret: SeafileApiKey
APK_FILE: app/build/outputs/apk/release/app-release.apk
BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab
SEAFILE_REPO: daffda8b-5840-4a65-b6d0-73b991facfb6
commands:
- '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=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"'
- name: deploy tagged build
image: curlimages/curl
environment:
SEAFILE_API_KEY:
from_secret: SeafileApiKey
APK_FILE: app/build/outputs/apk/release/app-release.apk
BUNDLE_FILE: app/build/outputs/bundle/release/app-release.aab
SEAFILE_REPO: daffda8b-5840-4a65-b6d0-73b991facfb6
commands:
- '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=@$BUNDLE_FILE -F parent_dir=/ -F relative_path=tagged/$DRONE_TAG/ -F replace=1 "$UPLOAD_URL"'
- 'curl -d "operation=rename&newname=app-release$DRONE_TAG.apk" -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/file/?p=/tagged/$DRONE_TAG/app-release.apk'
- 'curl -d "operation=rename&newname=app-release$DRONE_TAG.aab" -H "Authorization: Token $SEAFILE_API_KEY" https://seafile.zobrist.me/api2/repos/$SEAFILE_REPO/file/?p=/tagged/$DRONE_TAG/app-release.aab'
when:
event:
- tag
- name: slack notification
image: plugins/slack
settings:
webhook:
from_secret: SlackWebhook
when:
status:
- failure
- success

View File

@@ -0,0 +1,48 @@
name: Build Android
on: [pull_request, push]
jobs:
build:
runs-on: ubuntu-latest-android
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Prepare signing
run: |
touch keystore.properties
echo "storePassword=${{ secrets.STOREPASSWORD }}" >> keystore.properties
echo "keyPassword=${{ secrets.KEYPASSWORD }}" >> keystore.properties
echo "keyAlias=key0" >> keystore.properties
echo "storeFile=../AndroidKey" >> keystore.properties
- name: Generate versionCode
run: |
touch version.properties
let timestamp=$(date +%s)/10
echo "versionCode=$timestamp" >> version.properties
- name: Test the app
run: ./gradlew test
- name: Build apk
run: ./gradlew assembleRelease
- name: Build abb
run: ./gradlew bundleRelease
- name: Deploy latest to Nextcloud
run: |
curl -k -u "${{ secrets.NEXTCLOUD_USERNAME }}:${{ secrets.NEXTCLOUD_PASSWORD }}" -T "app/build/outputs/apk/release/app-release.apk" "https://nextcloud.zobrist.me/remote.php/dav/files/deploy/TichuCounter/latest/app-release.apk"
curl -k -u "${{ secrets.NEXTCLOUD_USERNAME }}:${{ secrets.NEXTCLOUD_PASSWORD }}" -T "app/build/outputs/bundle/release/app-release.aab" "https://nextcloud.zobrist.me/remote.php/dav/files/deploy/TichuCounter/latest/app-release.aab"
- name: Deploy tagged build to Nextcloud
run: |
curl -k -u "${{ secrets.NEXTCLOUD_USERNAME }}:${{ secrets.NEXTCLOUD_PASSWORD }}" -T "app/build/outputs/apk/release/app-release.apk" "https://nextcloud.zobrist.me/remote.php/dav/files/deploy/TichuCounter/tagged/app-release-${GITHUB_REF_NAME##*/}.apk"
curl -k -u "${{ secrets.NEXTCLOUD_USERNAME }}:${{ secrets.NEXTCLOUD_PASSWORD }}" -T "app/build/outputs/bundle/release/app-release.aab" "https://nextcloud.zobrist.me/remote.php/dav/files/deploy/TichuCounter/tagged/app-release-${GITHUB_REF_NAME##*/}.aab"
- uses: https://github.com/ravsamhq/notify-slack-action@v2
if: always()
with:
status: ${{ job.status }} # required
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required

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 = 2
// 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))
@@ -32,7 +32,7 @@ android {
targetSdkVersion 33 targetSdkVersion 33
versionCode versionProperties["versionCode"].toInteger() versionCode versionProperties["versionCode"].toInteger()
versionName "${versionMajor}.${versionMinor}.${versionProperties["versionCode"].toInteger()}" versionName "${versionMajor}.${versionMinor}.${versionProperties["versionCode"].toInteger()}"
resConfigs 'de', 'en' resourceConfigurations += ['de', 'en']
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
vectorDrawables { vectorDrawables {
@@ -68,15 +68,15 @@ android {
} }
composeOptions { composeOptions {
kotlinCompilerExtensionVersion = "1.3.2" kotlinCompilerExtensionVersion = "1.4.7"
} }
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 {
@@ -89,9 +89,9 @@ android {
dependencies { dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.0-rc01' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation "androidx.compose.material3:material3:1.0.1" implementation "androidx.compose.material3:material3:1.1.0"
implementation 'com.google.android.play:core-ktx:1.8.1' implementation 'com.google.android.play:core-ktx:1.8.1'
implementation 'com.google.android.play:core-ktx:1.8.1' implementation 'com.google.android.play:core-ktx:1.8.1'
implementation 'com.google.code.gson:gson:2.9.0' implementation 'com.google.code.gson:gson:2.9.0'
@@ -99,32 +99,32 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3' implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation 'androidx.fragment:fragment-ktx:1.5.5' implementation 'androidx.fragment:fragment-ktx:1.5.7'
implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation 'androidx.recyclerview:recyclerview:1.3.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.compose.material:material-icons-extended:1.3.1' implementation 'androidx.compose.material:material-icons-extended:1.4.3'
implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0" implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0"
implementation 'androidx.activity:activity-compose:1.6.1' implementation 'androidx.activity:activity-compose:1.7.2'
implementation "androidx.compose.ui:ui:1.3.3" implementation "androidx.compose.ui:ui:1.4.3"
implementation "androidx.compose.ui:ui-tooling-preview:1.3.3" implementation "androidx.compose.ui:ui-tooling-preview:1.4.3"
implementation "androidx.compose.runtime:runtime-livedata:1.3.3" implementation "androidx.compose.runtime:runtime-livedata:1.4.3"
implementation "androidx.navigation:navigation-compose:2.5.3" implementation "androidx.navigation:navigation-compose:2.5.3"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1" implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "com.google.dagger:hilt-android:2.44" implementation "com.google.dagger:hilt-android:2.44"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.3" androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.4.3"
debugImplementation "androidx.compose.ui:ui-tooling:1.3.3" debugImplementation "androidx.compose.ui:ui-tooling:1.4.3"
debugImplementation "androidx.compose.ui:ui-test-manifest:1.3.3" debugImplementation "androidx.compose.ui:ui-test-manifest:1.4.3"
kapt "com.google.dagger:hilt-compiler:2.44" kapt "com.google.dagger:hilt-compiler:2.44"
implementation "androidx.room:room-runtime:2.5.0" implementation "androidx.room:room-runtime:2.5.1"
annotationProcessor "androidx.room:room-compiler:2.5.0" annotationProcessor "androidx.room:room-compiler:2.5.1"
kapt "androidx.room:room-compiler:2.5.0" kapt "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-ktx:2.5.0" implementation "androidx.room:room-ktx:2.5.1"
implementation "androidx.multidex:multidex:2.0.1" implementation "androidx.multidex:multidex:2.0.1"
api "androidx.navigation:navigation-fragment-ktx:2.5.3" api "androidx.navigation:navigation-fragment-ktx:2.5.3"
} }

View File

@@ -2,11 +2,11 @@
"formatVersion": 1, "formatVersion": 1,
"database": { "database": {
"version": 1, "version": 1,
"identityHash": "f07e88c78e54c69c73890495a2121bf4", "identityHash": "1739540cd7d5436941316932a1036d83",
"entities": [ "entities": [
{ {
"tableName": "Round", "tableName": "Round",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`gameId` INTEGER NOT NULL, `scoreA` INTEGER NOT NULL, `scoreB` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT)", "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`gameId` INTEGER NOT NULL, `scoreA` INTEGER NOT NULL, `scoreB` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [ "fields": [
{ {
"fieldPath": "gameId", "fieldPath": "gameId",
@@ -30,7 +30,7 @@
"fieldPath": "uid", "fieldPath": "uid",
"columnName": "uid", "columnName": "uid",
"affinity": "INTEGER", "affinity": "INTEGER",
"notNull": false "notNull": true
} }
], ],
"primaryKey": { "primaryKey": {
@@ -44,7 +44,7 @@
}, },
{ {
"tableName": "Game", "tableName": "Game",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`active` INTEGER NOT NULL, `nameA` TEXT NOT NULL, `nameB` TEXT NOT NULL, `created` INTEGER NOT NULL, `modified` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT)", "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`active` INTEGER NOT NULL, `nameA` TEXT NOT NULL, `nameB` TEXT NOT NULL, `created` INTEGER NOT NULL, `modified` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [ "fields": [
{ {
"fieldPath": "active", "fieldPath": "active",
@@ -80,7 +80,7 @@
"fieldPath": "uid", "fieldPath": "uid",
"columnName": "uid", "columnName": "uid",
"affinity": "INTEGER", "affinity": "INTEGER",
"notNull": false "notNull": true
} }
], ],
"primaryKey": { "primaryKey": {
@@ -96,7 +96,7 @@
"views": [], "views": [],
"setupQueries": [ "setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f07e88c78e54c69c73890495a2121bf4')" "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1739540cd7d5436941316932a1036d83')"
] ]
} }
} }

View File

@@ -0,0 +1,102 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "1739540cd7d5436941316932a1036d83",
"entities": [
{
"tableName": "Round",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`gameId` INTEGER NOT NULL, `scoreA` INTEGER NOT NULL, `scoreB` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "gameId",
"columnName": "gameId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scoreA",
"columnName": "scoreA",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scoreB",
"columnName": "scoreB",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"uid"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Game",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`active` INTEGER NOT NULL, `nameA` TEXT NOT NULL, `nameB` TEXT NOT NULL, `created` INTEGER NOT NULL, `modified` INTEGER NOT NULL, `uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "active",
"columnName": "active",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "nameA",
"columnName": "nameA",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "nameB",
"columnName": "nameB",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "created",
"columnName": "created",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "modified",
"columnName": "modified",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"uid"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1739540cd7d5436941316932a1036d83')"
]
}
}

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

@@ -9,7 +9,6 @@
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:localeConfig="@xml/locales_config" android:localeConfig="@xml/locales_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -1,83 +0,0 @@
package me.zobrist.tichucounter
import android.content.SharedPreferences
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.preference.PreferenceManager
import dagger.hilt.android.AndroidEntryPoint
import me.zobrist.tichucounter.domain.Language
import me.zobrist.tichucounter.domain.SettingsAdapter
import me.zobrist.tichucounter.domain.Theme
import javax.inject.Inject
@AndroidEntryPoint
abstract class BaseActivity : AppCompatActivity(),
SharedPreferences.OnSharedPreferenceChangeListener {
@Inject
lateinit var settingsAdapter: SettingsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
keepScreenOn(settingsAdapter.keepScreenOn)
updateTheme(settingsAdapter.theme)
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this)
}
override fun onResume() {
super.onResume()
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
when (key) {
settingsAdapter.language::class.simpleName -> setLanguage(settingsAdapter.language)
settingsAdapter.keepScreenOn::class.simpleName -> keepScreenOn(settingsAdapter.keepScreenOn)
settingsAdapter.theme::class.simpleName -> updateTheme(settingsAdapter.theme)
}
}
private fun updateTheme(theme: Theme) {
val themeValue = when (theme) {
Theme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Theme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
Theme.DEFAULT -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
if (themeValue != AppCompatDelegate.getDefaultNightMode()) {
AppCompatDelegate.setDefaultNightMode(themeValue)
delegate.applyDayNight()
}
}
private fun keepScreenOn(keepOn: Boolean) {
if (keepOn) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
private fun setLanguage(language: Language) {
val currentLocale = AppCompatDelegate.getApplicationLocales()[0].toString()
if (language.value != currentLocale) {
val newLocale = LocaleListCompat.forLanguageTags(language.value)
AppCompatDelegate.setApplicationLocales(newLocale)
}
}
}

View File

@@ -1,9 +1,11 @@
package me.zobrist.tichucounter package me.zobrist.tichucounter
import android.os.Bundle import android.os.Bundle
import android.view.WindowManager
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
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.filled.* import androidx.compose.material.icons.filled.*
@@ -11,38 +13,34 @@ import androidx.compose.material.icons.outlined.*
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController import com.google.accompanist.systemuicontroller.rememberSystemUiController
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.domain.NavigationAction import me.zobrist.tichucounter.domain.*
import me.zobrist.tichucounter.domain.TopBarAction
import me.zobrist.tichucounter.repository.GameRepository
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
import me.zobrist.tichucounter.ui.MainViewModel import me.zobrist.tichucounter.ui.MainViewModel
import me.zobrist.tichucounter.ui.about.AboutView
import me.zobrist.tichucounter.ui.composables.DropDownMenu
import me.zobrist.tichucounter.ui.counter.* import me.zobrist.tichucounter.ui.counter.*
import me.zobrist.tichucounter.ui.history.HistoryList import me.zobrist.tichucounter.ui.history.HistoryList
import me.zobrist.tichucounter.ui.history.HistoryViewModel import me.zobrist.tichucounter.ui.history.HistoryViewModel
import me.zobrist.tichucounter.ui.layout.DrawerContent
import me.zobrist.tichucounter.ui.layout.TopBar
import me.zobrist.tichucounter.ui.settings.SettingsView import me.zobrist.tichucounter.ui.settings.SettingsView
import me.zobrist.tichucounter.ui.settings.SettingsViewModel import me.zobrist.tichucounter.ui.settings.SettingsViewModel
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : BaseActivity() { class MainActivity : AppCompatActivity(), ISystemSettingsChangeListener {
@Inject @Inject
lateinit var gameRepository: GameRepository lateinit var settingsAdapter: SettingsAdapter
private val counterViewModel: CounterViewModel by viewModels() private val counterViewModel: CounterViewModel by viewModels()
private val historyViewModel: HistoryViewModel by viewModels() private val historyViewModel: HistoryViewModel by viewModels()
@@ -53,6 +51,8 @@ class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
settingsAdapter.registerOnChangeListener(this)
setContent { setContent {
AppTheme { AppTheme {
val systemUiController = rememberSystemUiController() val systemUiController = rememberSystemUiController()
@@ -62,7 +62,85 @@ class MainActivity : BaseActivity() {
} }
} }
@OptIn(ExperimentalMaterial3Api::class) override fun onDestroy() {
super.onDestroy()
settingsAdapter.unregisterOnChangeListener(this)
}
override fun onLanguageChanged(language: Language) {
AppCompatDelegate.setApplicationLocales(language.value)
}
override fun onThemeChanged(theme: Theme) {
val themeValue = when (theme) {
Theme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Theme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
Theme.DEFAULT -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
AppCompatDelegate.setDefaultNightMode(themeValue)
}
override fun onScreenOnChanged(keepOn: KeepScreenOn) {
if (keepOn.value) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
@Composable
private fun NavigationDrawer() {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
val navController = rememberNavController()
val items = listOf(
DrawerItem(
Route.COUNTER,
Icons.Outlined.Calculate,
stringResource(R.string.menu_counter)
),
DrawerItem(Route.HISTORY, Icons.Outlined.List, stringResource(R.string.menu_history)),
DrawerItem(
Route.SETTINGS,
Icons.Outlined.Settings,
stringResource(R.string.menu_settings)
),
DrawerItem(
Route.ABOUT,
Icons.Outlined.Info,
stringResource(R.string.menu_about)
)
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination =
Route.valueOf(navBackStackEntry?.destination?.route ?: Route.COUNTER.name)
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerState.isOpen,
drawerContent = {
DrawerContent(
drawerItems = items,
selectedDrawerItem = items.first { it.route == currentDestination }) {
scope.launch {
drawerState.close()
}
navController.navigate(it)
}
}
) {
MyScaffoldLayout(
drawerState,
scope,
navController,
counterViewModel.keyboardHidden && (currentDestination == Route.COUNTER)
) { counterViewModel.showKeyboard() }
}
}
@Composable @Composable
fun MyScaffoldLayout( fun MyScaffoldLayout(
drawerState: DrawerState, drawerState: DrawerState,
@@ -72,6 +150,8 @@ class MainActivity : BaseActivity() {
fabAction: () -> Unit fabAction: () -> Unit
) { ) {
var topBarState by remember { mutableStateOf(TopBarState()) }
Scaffold( Scaffold(
floatingActionButton = { floatingActionButton = {
if (showFab) { if (showFab) {
@@ -81,192 +161,77 @@ class MainActivity : BaseActivity() {
} }
} }
}, },
topBar = { topBar = { TopBar(topBarState) }) { paddings ->
TopBar(
mainViewModel.topBarTitle,
mainViewModel.topBarIcon,
{ mainViewModel.onNavigateClicked() },
mainViewModel.topBarActions
)
}) {
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = "counter", startDestination = Route.COUNTER.name,
modifier = Modifier.padding(it) modifier = Modifier.padding(paddings)
) { ) {
composable("counter") { composable(Route.COUNTER) {
Counter(counterViewModel)
mainViewModel.topBarActions = (listOf( var expanded by remember { mutableStateOf(false) }
topBarState = TopBarState(
title = stringResource(R.string.app_name),
actions = (listOf(
TopBarAction( TopBarAction(
Icons.Outlined.Undo, Icons.Outlined.Undo,
mainViewModel.isUndoActionActive mainViewModel.isUndoActionActive,
) { mainViewModel.undoLastRound() }, { mainViewModel.undoLastRound() }),
TopBarAction( TopBarAction(
Icons.Outlined.Redo, Icons.Outlined.Redo,
mainViewModel.isRedoActionActive mainViewModel.isRedoActionActive,
) { mainViewModel.redoLastRound() } { mainViewModel.redoLastRound() }),
TopBarAction(
Icons.Outlined.MoreVert,
mainViewModel.activeGameHasRounds,
{ expanded = true }
) {
DropDownMenu(
mapOf("new" to R.string.newGame),
"",
expanded,
) {
expanded = false
it?.let {
when (it) {
"new" -> mainViewModel.newGame()
}
}
}
},
)) ))
mainViewModel.topBarIcon = Icons.Outlined.Menu
mainViewModel.topBarTitle = stringResource(R.string.app_name)
mainViewModel.topBarNavigationAction =
NavigationAction { scope.launch { drawerState.open() } }
}
composable("history") {
var openDialog by remember { mutableStateOf(false) }
HistoryList(historyViewModel, openDialog, { deleteAll ->
if (deleteAll) {
mainViewModel.deleteAllInactiveGames()
}
openDialog = false
}) { navController.navigate("counter") }
mainViewModel.topBarActions = listOf(
TopBarAction(
Icons.Outlined.DeleteForever,
true
) { ) {
openDialog = true scope.launch {
currentFocus?.clearFocus()
drawerState.open()
} }
)
mainViewModel.topBarIcon = Icons.Outlined.Menu
mainViewModel.topBarTitle = stringResource(R.string.menu_history)
mainViewModel.topBarNavigationAction =
NavigationAction { scope.launch { drawerState.open() } }
} }
composable("settings") {
Counter(counterViewModel)
}
composable(Route.HISTORY) {
topBarState =
TopBarState(title = stringResource(R.string.menu_history)) { scope.launch { drawerState.open() } }
HistoryList(historyViewModel) { navController.navigate(Route.COUNTER) }
}
composable(Route.SETTINGS) {
topBarState =
TopBarState(title = stringResource(R.string.menu_settings)) { scope.launch { drawerState.open() } }
SettingsView(settingsViewModel) SettingsView(settingsViewModel)
mainViewModel.topBarActions = emptyList()
mainViewModel.topBarIcon = Icons.Outlined.Menu
mainViewModel.topBarTitle = stringResource(R.string.menu_settings)
mainViewModel.topBarNavigationAction =
NavigationAction { scope.launch { drawerState.open() } }
}
}
}
} }
@OptIn(ExperimentalMaterial3Api::class) composable(Route.ABOUT) {
@Composable topBarState =
private fun TopBar( TopBarState(title = stringResource(R.string.menu_about)) { scope.launch { drawerState.open() } }
title: String,
icon: ImageVector,
navigateAction: () -> Unit,
actions: List<TopBarAction>
) {
TopAppBar(
title = {
Text(
title,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
navigationIcon = {
IconButton(onClick = { navigateAction() }) {
Icon(
imageVector = icon,
contentDescription = "Localized description"
)
}
},
actions = {
actions.forEach {
IconButton(onClick = { it.action() }, enabled = it.isActive) {
Icon(
imageVector = it.imageVector,
contentDescription = null,
)
}
}
}
)
}
@OptIn(ExperimentalMaterial3Api::class) AboutView()
@Composable
private fun NavigationDrawer() {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
val navController = rememberNavController()
val items = listOf(
Screen("counter", Icons.Outlined.Calculate, R.string.app_name),
Screen("history", Icons.Outlined.List, R.string.menu_history),
Screen("settings", Icons.Outlined.Settings, R.string.menu_settings)
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = false,
drawerContent = {
ModalDrawerSheet {
Spacer(Modifier.height(20.dp))
NavigationDrawerItem(
icon = { Icon(Icons.Outlined.RestartAlt, contentDescription = null) },
colors = NavigationDrawerItemDefaults.colors(
unselectedContainerColor = MaterialTheme.colorScheme.primaryContainer
),
label = { Text(stringResource(R.string.newGame)) },
selected = false,
onClick = {
scope.launch { drawerState.close() }
mainViewModel.newGame()
navController.navigate("counter") {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
Divider(Modifier.padding(top = 20.dp, bottom = 20.dp))
items.forEach { screen ->
NavigationDrawerItem(
icon = { Icon(screen.icon, contentDescription = null) },
label = { Text(stringResource(screen.resourceId)) },
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
onClick = {
scope.launch { drawerState.close() }
navController.navigate(screen.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
},
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
} }
} }
} }
) {
MyScaffoldLayout(
drawerState,
scope,
navController,
counterViewModel.keyboardHidden
) { counterViewModel.keyboardHidden = false }
} }
} }
private class Screen(val route: String, val icon: ImageVector, @StringRes val resourceId: Int)
}

View File

@@ -3,6 +3,8 @@ package me.zobrist.tichucounter.data
import androidx.room.Database import androidx.room.Database
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters import androidx.room.TypeConverters
import me.zobrist.tichucounter.data.entity.Game
import me.zobrist.tichucounter.data.entity.Round
@Database(entities = [Round::class, Game::class], version = 1) @Database(entities = [Round::class, Game::class], version = 1)
@TypeConverters(DateConverter::class) @TypeConverters(DateConverter::class)

View File

@@ -2,7 +2,7 @@ package me.zobrist.tichucounter.data
import androidx.room.ProvidedTypeConverter import androidx.room.ProvidedTypeConverter
import androidx.room.TypeConverter import androidx.room.TypeConverter
import java.util.* import java.util.Date
@ProvidedTypeConverter @ProvidedTypeConverter
object DateConverter { object DateConverter {

View File

@@ -1,15 +0,0 @@
package me.zobrist.tichucounter.data
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*
@Entity
data class Game(
override var active: Boolean,
override var nameA: String,
override var nameB: String,
override val created: Date,
override var modified: Date,
@PrimaryKey(autoGenerate = true) override val uid: Long? = null
) : IGame, IEntity

View File

@@ -1,16 +0,0 @@
package me.zobrist.tichucounter.data
import androidx.room.Entity
import java.util.*
@Entity
data class GameAndScore(
override var active: Boolean,
override var nameA: String,
override var nameB: String,
override val created: Date,
override var modified: Date,
override var gameId: Long,
override var scoreA: Int,
override var scoreB: Int,
) : IGame, IRound

View File

@@ -2,6 +2,7 @@ package me.zobrist.tichucounter.data
import androidx.room.* import androidx.room.*
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import me.zobrist.tichucounter.data.entity.Game
@Dao @Dao
@@ -10,25 +11,23 @@ interface GameDao : DaoBase<Game> {
@Query("SELECT * FROM game") @Query("SELECT * FROM game")
fun getAll(): Flow<List<Game>> fun getAll(): Flow<List<Game>>
@Query( @Transaction
"SELECT active, " + @Query("SELECT * FROM game")
"nameA, " + fun getGamesWithRounds(): Flow<List<GameWithScores>>
"nameB, " +
"created, " + @Transaction
"modified, " + @Query("SELECT * FROM game WHERE active is 1")
"game.uid as gameId, " + fun getActiveWithRounds(): Flow<GameWithScores?>
"COALESCE(SUM(round.scoreA), 0) as scoreA, " +
"COALESCE(SUM(round.scoreB), 0) as scoreB " +
"FROM game " +
"LEFT JOIN round ON round.gameId = game.uid GROUP BY game.uid ORDER BY modified DESC"
)
fun getAllWithPoints(): Flow<List<GameAndScore>>
@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;")
fun setActive(gameId: Long) fun setActive(gameId: Long)
@@ -36,4 +35,7 @@ interface GameDao : DaoBase<Game> {
@Query("UPDATE game SET active = 0 WHERE uid is not :gameId;") @Query("UPDATE game SET active = 0 WHERE uid is not :gameId;")
fun setOthersInactive(gameId: Long) fun setOthersInactive(gameId: Long)
@Query("SELECT names FROM (SELECT nameA AS names FROM game UNION ALL SELECT nameB AS names FROM game) GROUP BY names")
fun getDistinctTeamNames(): Flow<List<String>>
} }

View File

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

View File

@@ -1,5 +0,0 @@
package me.zobrist.tichucounter.data
interface IEntity {
val uid: Long?
}

View File

@@ -1,11 +0,0 @@
package me.zobrist.tichucounter.data
import java.util.*
interface IGame {
var active: Boolean
var nameA: String
var nameB: String
val created: Date
var modified: Date
}

View File

@@ -1,7 +0,0 @@
package me.zobrist.tichucounter.data
interface IRound {
var gameId: Long
var scoreA: Int
var scoreB: Int
}

View File

@@ -1,12 +0,0 @@
package me.zobrist.tichucounter.data
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class Round(
override var gameId: Long,
override var scoreA: Int,
override var scoreB: Int,
@PrimaryKey(autoGenerate = true) override val uid: Long? = null
) : IRound, IEntity

View File

@@ -1,7 +1,7 @@
package me.zobrist.tichucounter.data package me.zobrist.tichucounter.data
import androidx.room.* import androidx.room.*
import kotlinx.coroutines.flow.Flow import me.zobrist.tichucounter.data.entity.Round
@Dao @Dao
interface RoundDao : DaoBase<Round> { interface RoundDao : DaoBase<Round> {
@@ -12,20 +12,4 @@ interface RoundDao : DaoBase<Round> {
@Query("SELECT * FROM round WHERE gameId is :gameId") @Query("SELECT * FROM round WHERE gameId is :gameId")
fun getAllForGame(gameId: Long?): List<Round> fun getAllForGame(gameId: Long?): List<Round>
@Query(
"SELECT gameId, SUM(scoreA) as scoreA, SUM(scoreB) as scoreB " +
"FROM round " +
"LEFT JOIN game ON game.uid = round.gameId " +
"WHERE game.active == 1"
)
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
fun getRoundSumForActiveGame(): Flow<Round>
@Query(
"SELECT gameId, scoreA, scoreB, round.uid " +
"FROM round " +
"LEFT JOIN game ON game.uid = round.gameId " +
"WHERE game.active == 1"
)
fun getForActiveGame(): Flow<List<Round>>
} }

View File

@@ -0,0 +1,15 @@
package me.zobrist.tichucounter.data.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.Date
@Entity
data class Game(
var active: Boolean = true,
var nameA: String = "TeamA",
var nameB: String = "TeamB",
val created: Date = Date(),
var modified: Date = Date(),
@PrimaryKey(autoGenerate = true) override val uid: Long = 0
) : IEntity

View File

@@ -0,0 +1,5 @@
package me.zobrist.tichucounter.data.entity
interface IEntity {
val uid: Long
}

View File

@@ -0,0 +1,12 @@
package me.zobrist.tichucounter.data.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class Round(
var gameId: Long,
var scoreA: Int,
var scoreB: Int,
@PrimaryKey(autoGenerate = true) override val uid: Long = 0
) : IEntity

View File

@@ -0,0 +1,5 @@
package me.zobrist.tichucounter.domain
import androidx.compose.ui.graphics.vector.ImageVector
data class DrawerItem(val route: Route, val menuIcon: ImageVector, val menuName: String)

View File

@@ -0,0 +1,14 @@
package me.zobrist.tichucounter.domain
import me.zobrist.tichucounter.data.GameWithScores
fun GameWithScores.getTotalPoints(): Pair<Int, Int> {
var scoreA = 0
var scoreB = 0
this.rounds.forEach {
scoreA += it.scoreA
scoreB += it.scoreB
}
return Pair(scoreA, scoreB)
}

View File

@@ -0,0 +1,34 @@
package me.zobrist.tichucounter.domain
import androidx.compose.runtime.Composable
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDeepLink
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
fun NavController.navigate(route: Route) {
this.navigate(route.name) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(Route.COUNTER.name) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
fun NavGraphBuilder.composable(
route: Route,
arguments: List<NamedNavArgument> = emptyList(),
deepLinks: List<NavDeepLink> = emptyList(),
content: @Composable (NavBackStackEntry) -> Unit
) {
this.composable(route.name, arguments, deepLinks, content)
}

View File

@@ -1,3 +0,0 @@
package me.zobrist.tichucounter.domain
class NavigationAction(val action: () -> Unit)

View File

@@ -0,0 +1,3 @@
package me.zobrist.tichucounter.domain
enum class Route { COUNTER, HISTORY, SETTINGS, ABOUT }

View File

@@ -1,65 +1,158 @@
package me.zobrist.tichucounter.domain package me.zobrist.tichucounter.domain
import android.content.Context import android.content.Context
import androidx.appcompat.app.AppCompatDelegate.getApplicationLocales import androidx.core.os.LocaleListCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton
enum class Theme { DEFAULT, DARK, LIGHT } enum class Theme { DEFAULT, DARK, LIGHT }
enum class Language(val value: String) { ENGLISH("en"), GERMAN("de") } enum class Language(val value: LocaleListCompat) {
DEFAULT(LocaleListCompat.getEmptyLocaleList()),
ENGLISH(LocaleListCompat.forLanguageTags("en")),
GERMAN(LocaleListCompat.forLanguageTags("de"))
}
enum class KeepScreenOn(val value: Boolean) { ON(true), OFF(false) }
enum class CounterListView { BY_ROUND, CONTINUOUS }
interface ISystemSettingsChangeListener {
fun onLanguageChanged(language: Language)
fun onThemeChanged(theme: Theme)
fun onScreenOnChanged(keepOn: KeepScreenOn)
}
interface IDisplaySettingsChangeListener {
fun onCounterListViewChanged(listView: CounterListView)
}
@Singleton
class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) { class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) {
private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
private var systemListenerList = mutableListOf<ISystemSettingsChangeListener>()
private var displayListenerList = mutableListOf<IDisplaySettingsChangeListener>()
val language: Language
get() { var language: Language
return try { private set
var setting = sharedPreferences.getString(Language::class.simpleName, null)
enumValueOf(setting!!) var theme: Theme
private set
var keepScreenOn: KeepScreenOn
private set
var counterListView: CounterListView
private set
init {
language = try {
enumValueOf(sharedPreferences.getString(Language::class.simpleName, null)!!)
} catch (_: NullPointerException) { } catch (_: NullPointerException) {
val current = getCurrentAppLanguage() Language.DEFAULT
setLanguage(current) }
current
theme = try {
enumValueOf(sharedPreferences.getString(Theme::class.simpleName, null)!!)
} catch (_: java.lang.Exception) {
Theme.DEFAULT
}
keepScreenOn = try {
enumValueOf(sharedPreferences.getString(KeepScreenOn::class.simpleName, null)!!)
} catch (_: java.lang.Exception) {
KeepScreenOn.OFF
}
counterListView = try {
enumValueOf(sharedPreferences.getString(CounterListView::class.simpleName, null)!!)
} catch (_: java.lang.Exception) {
CounterListView.BY_ROUND
} }
} }
val theme: Theme fun registerOnChangeListener(listener: ISystemSettingsChangeListener) {
get() { systemListenerList.add(listener)
val setting = sharedPreferences.getString(Theme::class.simpleName, Theme.DEFAULT.name)
return enumValueOf(setting!!) listener.onThemeChanged(theme)
listener.onLanguageChanged(language)
listener.onScreenOnChanged(keepScreenOn)
} }
val keepScreenOn: Boolean fun registerOnChangeListener(listener: IDisplaySettingsChangeListener) {
get() { displayListenerList.add(listener)
return sharedPreferences.getBoolean("keep_screen_on", false)
listener.onCounterListViewChanged(counterListView)
} }
private fun getCurrentAppLanguage(): Language { fun unregisterOnChangeListener(listener: ISystemSettingsChangeListener?) {
return when (getApplicationLocales()[0].toString()) { if (listener != null) {
"de" -> Language.GERMAN systemListenerList.remove(listener)
else -> Language.ENGLISH }
}
fun unregisterOnChangeListener(listener: IDisplaySettingsChangeListener?) {
if (listener != null) {
displayListenerList.remove(listener)
} }
} }
fun setLanguage(language: Language) { fun setLanguage(language: Language) {
val editor = sharedPreferences.edit() this.language = language
editor.putString(Language::class.simpleName, language.name) updatePreference(Language::class.simpleName, language.name)
editor.apply() notifyListeners(language)
} }
fun setTheme(theme: Theme) { fun setTheme(theme: Theme) {
this.theme = theme
updatePreference(Theme::class.simpleName, theme.name)
notifyListeners(theme)
}
fun setKeepScreenOn(setting: KeepScreenOn) {
this.keepScreenOn = setting
updatePreference(KeepScreenOn::class.simpleName, setting.name)
notifyListeners(setting)
}
fun setCounterViewList(setting: CounterListView) {
this.counterListView = setting
updatePreference(CounterListView::class.simpleName, setting.name)
notifyListeners(setting)
}
private fun updatePreference(name: String?, value: String) {
val editor = sharedPreferences.edit() val editor = sharedPreferences.edit()
editor.putString(Theme::class.simpleName, theme.name) editor.putString(name, value)
editor.apply() editor.apply()
} }
fun setKeepScreenOn(setting: Boolean) { private fun notifyListeners(language: Language) {
val editor = sharedPreferences.edit() systemListenerList.forEach {
editor.putBoolean("keep_screen_on", setting) it.onLanguageChanged(language)
editor.apply() }
}
private fun notifyListeners(theme: Theme) {
systemListenerList.forEach {
it.onThemeChanged(theme)
}
}
private fun notifyListeners(keepScreenOn: KeepScreenOn) {
systemListenerList.forEach {
it.onScreenOnChanged(keepScreenOn)
}
}
private fun notifyListeners(counterListView: CounterListView) {
displayListenerList.forEach {
it.onCounterListViewChanged(counterListView)
}
} }
} }

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

@@ -1,5 +1,11 @@
package me.zobrist.tichucounter.domain package me.zobrist.tichucounter.domain
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
class TopBarAction(val imageVector: ImageVector, val isActive: Boolean, val action: () -> Unit) class TopBarAction(
val imageVector: ImageVector,
val isActive: Boolean,
val action: () -> Unit,
val composeCode: @Composable () -> Unit = {}
)

View File

@@ -0,0 +1,12 @@
package me.zobrist.tichucounter.domain
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Menu
import androidx.compose.ui.graphics.vector.ImageVector
data class TopBarState(
var title: String = "",
var icon: ImageVector = Icons.Outlined.Menu,
var actions: List<TopBarAction> = emptyList(),
var onNavigate: () -> Unit = {}
)

View File

@@ -2,14 +2,18 @@ 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.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
import me.zobrist.tichucounter.data.Game
import me.zobrist.tichucounter.data.GameDao import me.zobrist.tichucounter.data.GameDao
import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.data.GameWithScores
import me.zobrist.tichucounter.data.RoundDao import me.zobrist.tichucounter.data.RoundDao
import java.util.* import me.zobrist.tichucounter.data.entity.Game
import me.zobrist.tichucounter.data.entity.Round
import java.util.Date
import javax.inject.Inject import javax.inject.Inject
class GameRepository @Inject constructor( class GameRepository @Inject constructor(
@@ -17,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
} }
} }
} }
@@ -38,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)
} }
} }
@@ -81,7 +89,7 @@ class GameRepository @Inject constructor(
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val active = activeGame val active = activeGame
active.modified = Date() active.modified = Date()
val round = Round(active.uid!!, scoreA, scoreB) val round = Round(active.uid, scoreA, scoreB)
roundDao.insert(round) roundDao.insert(round)
gameDao.update(active) gameDao.update(active)
} }
@@ -90,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) {
} }
} }
@@ -103,11 +110,10 @@ class GameRepository @Inject constructor(
suspend fun deleteAllInactive() { suspend fun deleteAllInactive() {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
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)
@@ -116,4 +122,16 @@ class GameRepository @Inject constructor(
} }
} }
} }
fun getActiveGameFlow(): Flow<GameWithScores> {
return gameDao.getActiveWithRounds().filter { it != null }.map { it!! }
}
fun getAllWithRoundFlow(): Flow<List<GameWithScores>> {
return gameDao.getGamesWithRounds()
}
fun getDistinctTeamNames(): Flow<List<String>> {
return gameDao.getDistinctTeamNames()
}
} }

View File

@@ -1,21 +1,21 @@
@file:Suppress("unused", "unused", "unused") @file:Suppress("unused")
package me.zobrist.tichucounter.ui package me.zobrist.tichucounter.ui
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
val md_theme_light_primary = Color(0xFFBE0034) val md_theme_light_primary = Color(0xFF9C404D)
val md_theme_light_onPrimary = Color(0xFFFFFFFF) val md_theme_light_onPrimary = Color(0xFFFFFFFF)
val md_theme_light_primaryContainer = Color(0xFFFFDADA) val md_theme_light_primaryContainer = Color(0xFFFFDADB)
val md_theme_light_onPrimaryContainer = Color(0xFF40000B) val md_theme_light_onPrimaryContainer = Color(0xFF40000F)
val md_theme_light_secondary = Color(0xFF6E5D00) val md_theme_light_secondary = Color(0xFF765659)
val md_theme_light_onSecondary = Color(0xFFFFFFFF) val md_theme_light_onSecondary = Color(0xFFFFFFFF)
val md_theme_light_secondaryContainer = Color(0xFFFFE261) val md_theme_light_secondaryContainer = Color(0xFFFFDADB)
val md_theme_light_onSecondaryContainer = Color(0xFF221B00) val md_theme_light_onSecondaryContainer = Color(0xFF2C1517)
val md_theme_light_tertiary = Color(0xFF76592F) val md_theme_light_tertiary = Color(0xFF775930)
val md_theme_light_onTertiary = Color(0xFFFFFFFF) val md_theme_light_onTertiary = Color(0xFFFFFFFF)
val md_theme_light_tertiaryContainer = Color(0xFFFFDDB1) val md_theme_light_tertiaryContainer = Color(0xFFFFDDB5)
val md_theme_light_onTertiaryContainer = Color(0xFF291800) val md_theme_light_onTertiaryContainer = Color(0xFF2A1800)
val md_theme_light_error = Color(0xFFBA1A1A) val md_theme_light_error = Color(0xFFBA1A1A)
val md_theme_light_errorContainer = Color(0xFFFFDAD6) val md_theme_light_errorContainer = Color(0xFFFFDAD6)
val md_theme_light_onError = Color(0xFFFFFFFF) val md_theme_light_onError = Color(0xFFFFFFFF)
@@ -24,29 +24,29 @@ val md_theme_light_background = Color(0xFFFFFBFF)
val md_theme_light_onBackground = Color(0xFF201A1A) val md_theme_light_onBackground = Color(0xFF201A1A)
val md_theme_light_surface = Color(0xFFFFFBFF) val md_theme_light_surface = Color(0xFFFFFBFF)
val md_theme_light_onSurface = Color(0xFF201A1A) val md_theme_light_onSurface = Color(0xFF201A1A)
val md_theme_light_surfaceVariant = Color(0xFFF4DDDD) val md_theme_light_surfaceVariant = Color(0xFFF4DDDE)
val md_theme_light_onSurfaceVariant = Color(0xFF524343) val md_theme_light_onSurfaceVariant = Color(0xFF524344)
val md_theme_light_outline = Color(0xFF857373) val md_theme_light_outline = Color(0xFF857374)
val md_theme_light_inverseOnSurface = Color(0xFFFBEEED) val md_theme_light_inverseOnSurface = Color(0xFFFBEEEE)
val md_theme_light_inverseSurface = Color(0xFF362F2F) val md_theme_light_inverseSurface = Color(0xFF362F2F)
val md_theme_light_inversePrimary = Color(0xFFFFB3B5) val md_theme_light_inversePrimary = Color(0xFFFFB2B9)
val md_theme_light_shadow = Color(0xFF000000) val md_theme_light_shadow = Color(0xFF000000)
val md_theme_light_surfaceTint = Color(0xFFBE0034) val md_theme_light_surfaceTint = Color(0xFF9C404D)
val md_theme_light_outlineVariant = Color(0xFFD7C1C1) val md_theme_light_outlineVariant = Color(0xFFD7C1C2)
val md_theme_light_scrim = Color(0xFF000000) val md_theme_light_scrim = Color(0xFF000000)
val md_theme_dark_primary = Color(0xFFFFB3B5) val md_theme_dark_primary = Color(0xFFFFB2B9)
val md_theme_dark_onPrimary = Color(0xFF680018) val md_theme_dark_onPrimary = Color(0xFF5F1222)
val md_theme_dark_primaryContainer = Color(0xFF920026) val md_theme_dark_primaryContainer = Color(0xFF7D2937)
val md_theme_dark_onPrimaryContainer = Color(0xFFFFDADA) val md_theme_dark_onPrimaryContainer = Color(0xFFFFDADB)
val md_theme_dark_secondary = Color(0xFFE6C500) val md_theme_dark_secondary = Color(0xFFE5BDBF)
val md_theme_dark_onSecondary = Color(0xFF3A3000) val md_theme_dark_onSecondary = Color(0xFF44292C)
val md_theme_dark_secondaryContainer = Color(0xFF534600) val md_theme_dark_secondaryContainer = Color(0xFF5C3F41)
val md_theme_dark_onSecondaryContainer = Color(0xFFFFE261) val md_theme_dark_onSecondaryContainer = Color(0xFFFFDADB)
val md_theme_dark_tertiary = Color(0xFFE6C18D) val md_theme_dark_tertiary = Color(0xFFE8C08E)
val md_theme_dark_onTertiary = Color(0xFF422C05) val md_theme_dark_onTertiary = Color(0xFF442B06)
val md_theme_dark_tertiaryContainer = Color(0xFF5C421A) val md_theme_dark_tertiaryContainer = Color(0xFF5D411B)
val md_theme_dark_onTertiaryContainer = Color(0xFFFFDDB1) val md_theme_dark_onTertiaryContainer = Color(0xFFFFDDB5)
val md_theme_dark_error = Color(0xFFFFB4AB) val md_theme_dark_error = Color(0xFFFFB4AB)
val md_theme_dark_errorContainer = Color(0xFF93000A) val md_theme_dark_errorContainer = Color(0xFF93000A)
val md_theme_dark_onError = Color(0xFF690005) val md_theme_dark_onError = Color(0xFF690005)
@@ -55,16 +55,16 @@ val md_theme_dark_background = Color(0xFF201A1A)
val md_theme_dark_onBackground = Color(0xFFECE0DF) val md_theme_dark_onBackground = Color(0xFFECE0DF)
val md_theme_dark_surface = Color(0xFF201A1A) val md_theme_dark_surface = Color(0xFF201A1A)
val md_theme_dark_onSurface = Color(0xFFECE0DF) val md_theme_dark_onSurface = Color(0xFFECE0DF)
val md_theme_dark_surfaceVariant = Color(0xFF524343) val md_theme_dark_surfaceVariant = Color(0xFF524344)
val md_theme_dark_onSurfaceVariant = Color(0xFFD7C1C1) val md_theme_dark_onSurfaceVariant = Color(0xFFD7C1C2)
val md_theme_dark_outline = Color(0xFF9F8C8C) val md_theme_dark_outline = Color(0xFF9F8C8D)
val md_theme_dark_inverseOnSurface = Color(0xFF201A1A) val md_theme_dark_inverseOnSurface = Color(0xFF201A1A)
val md_theme_dark_inverseSurface = Color(0xFFECE0DF) val md_theme_dark_inverseSurface = Color(0xFFECE0DF)
val md_theme_dark_inversePrimary = Color(0xFFBE0034) val md_theme_dark_inversePrimary = Color(0xFF9C404D)
val md_theme_dark_shadow = Color(0xFF000000) val md_theme_dark_shadow = Color(0xFF000000)
val md_theme_dark_surfaceTint = Color(0xFFFFB3B5) val md_theme_dark_surfaceTint = Color(0xFFFFB2B9)
val md_theme_dark_outlineVariant = Color(0xFF524343) val md_theme_dark_outlineVariant = Color(0xFF524344)
val md_theme_dark_scrim = Color(0xFF000000) val md_theme_dark_scrim = Color(0xFF000000)
val seed = Color(0xFFED0043) val seed = Color(0xFF833842)

View File

@@ -1,7 +1,5 @@
package me.zobrist.tichucounter.ui package me.zobrist.tichucounter.ui
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@@ -10,50 +8,45 @@ 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.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.data.RoundDao
import me.zobrist.tichucounter.domain.NavigationAction
import me.zobrist.tichucounter.domain.TopBarAction
import me.zobrist.tichucounter.repository.GameRepository import me.zobrist.tichucounter.repository.GameRepository
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
class MainViewModel @Inject constructor( class MainViewModel @Inject constructor(
private val gameRepository: GameRepository, private val gameRepository: GameRepository
roundDao: RoundDao
) : ViewModel() { ) : ViewModel() {
private var redoRounds = mutableStateListOf<Round>() private var redoRounds = mutableStateListOf<Round>()
private var expectedRoundCount = 0 private var expectedRoundCount = 0
var topBarTitle by mutableStateOf("")
var topBarActions by mutableStateOf(emptyList<TopBarAction>())
var topBarIcon by mutableStateOf(Icons.Filled.Menu)
var isUndoActionActive by mutableStateOf(false) var isUndoActionActive by mutableStateOf(false)
var topBarNavigationAction by mutableStateOf(NavigationAction {})
val isRedoActionActive: Boolean val isRedoActionActive: Boolean
get() = redoRounds.isNotEmpty() get() = redoRounds.isNotEmpty()
var activeGameHasRounds by mutableStateOf(false)
private set
init { init {
viewModelScope.launch { viewModelScope.launch {
roundDao.getForActiveGame().collect {
isUndoActionActive = it.isNotEmpty()
if (expectedRoundCount != it.count()) { gameRepository.getActiveGameFlow().collect {
activeGameHasRounds = it.rounds.isNotEmpty() == true
isUndoActionActive = it.rounds.isNotEmpty()
if (expectedRoundCount != it.rounds.count()) {
redoRounds.clear() redoRounds.clear()
} }
expectedRoundCount = it.count() expectedRoundCount = it.rounds.count()
} }
} }
} }
fun onNavigateClicked() {
topBarNavigationAction.action()
}
fun undoLastRound() { fun undoLastRound() {
viewModelScope.launch { viewModelScope.launch {
val round = gameRepository.getLastRound() val round = gameRepository.getLastRound()
@@ -83,10 +76,4 @@ class MainViewModel @Inject constructor(
gameRepository.newGame() gameRepository.newGame()
} }
} }
fun deleteAllInactiveGames() {
viewModelScope.launch {
gameRepository.deleteAllInactive()
}
}
} }

View File

@@ -2,7 +2,11 @@ package me.zobrist.tichucounter.ui
import android.os.Build import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.* import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext

View File

@@ -0,0 +1,109 @@
package me.zobrist.tichucounter.ui.about
import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Mail
import androidx.compose.material.icons.outlined.Shop
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ShapeDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment.Companion.Top
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.BuildConfig
import me.zobrist.tichucounter.R
import me.zobrist.tichucounter.ui.AppTheme
@Composable
fun AboutView() {
val uriHandler = LocalUriHandler.current
Column(
modifier = Modifier
.padding(
top = 20.dp,
start = 20.dp,
end = 20.dp,
bottom = 40.dp
),
) {
Row {
Image(
modifier = Modifier
.padding(end = 10.dp)
.align(Top)
.size(75.dp)
.background(
color = Color.Red,
shape = ShapeDefaults.Medium
),
painter = painterResource(R.drawable.tichu_logo),
contentDescription = null,
contentScale = ContentScale.None
)
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))
}
}
}
@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun AboutViewPreview() {
AppTheme {
Surface {
AboutView()
}
}
}

View File

@@ -0,0 +1,32 @@
package me.zobrist.tichucounter.ui.composables
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Check
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
@Composable
fun <T> DropDownMenu(map: Map<T, Int>, selected: T, expanded: Boolean, onSelected: (T?) -> Unit) {
DropdownMenu(
expanded = expanded,
onDismissRequest = { onSelected(null) }
) {
map.forEach {
DropdownMenuItem(
onClick = {
onSelected(it.key)
},
trailingIcon = {
if (it.key == selected) {
Icon(Icons.Outlined.Check, null)
}
},
text = { Text(stringResource(it.value)) },
)
}
}
}

View File

@@ -0,0 +1,100 @@
package me.zobrist.tichucounter.ui.composables
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun TypeaheadTextField(
value: String,
items: List<String>,
onValueChange: (String) -> Unit,
modifier: Modifier,
colors: TextFieldColors,
textStyle: TextStyle
) {
var isFocused by remember { mutableStateOf(false) }
val focusManager = LocalFocusManager.current
ExposedDropdownMenuBox(
expanded = isFocused,
modifier = modifier,
onExpandedChange = {}
) {
var dropDownWidth by remember { mutableStateOf(0) }
TextField(
value = value,
textStyle = textStyle,
onValueChange = {
onValueChange(it)
},
singleLine = true,
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Done,
),
keyboardActions = KeyboardActions(
onDone = {
focusManager.clearFocus()
}
),
modifier = Modifier
.menuAnchor()
.onFocusChanged {
isFocused = it.isFocused
}
.onSizeChanged { dropDownWidth = it.width }
.onKeyEvent { event ->
if (event.key == Key.Back || event.key == Key.Enter) {
focusManager.clearFocus()
true
} else {
false
}
},
colors = colors
)
ExposedDropdownMenu(
expanded = isFocused && items.isNotEmpty(),
modifier = Modifier
.width(with(LocalDensity.current) { dropDownWidth.toDp() }),
onDismissRequest = { }
) {
items.forEach {
DropdownMenuItem(
onClick = {
onValueChange(it)
focusManager.clearFocus()
},
text = { Text(it) },
)
}
}
}
}

View File

@@ -4,12 +4,16 @@ import android.content.res.Configuration
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.runtime.* import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
@@ -35,6 +39,8 @@ fun Landscape(viewModel: ICounterViewModel) {
TeamNamesView( TeamNamesView(
viewModel.teamNameA, viewModel.teamNameA,
viewModel.teamNameB, viewModel.teamNameB,
viewModel.teamNameSuggestionsA,
viewModel.teamNameSuggestionsB,
{ viewModel.updateNameA(it) }, { viewModel.updateNameA(it) },
{ viewModel.updateNameB(it) } { viewModel.updateNameB(it) }
) )
@@ -46,24 +52,14 @@ fun Landscape(viewModel: ICounterViewModel) {
RoundListView( RoundListView(
viewModel.roundScoreList, viewModel.roundScoreList,
viewModel.sumUpScores,
Modifier.weight(1f) Modifier.weight(1f)
) )
} }
if (!viewModel.keyboardHidden) {
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
KeyboardView( KeyBoardView(viewModel = viewModel)
viewModel.currentScoreA, }
viewModel.currentScoreB,
viewModel.requestFocusA,
viewModel.enableSubmit,
{ viewModel.updateFocusStateA(it) },
{ viewModel.updateFocusStateB(it) },
{ viewModel.digitClicked(it) },
{ viewModel.addSub100Clicked(it) },
{ viewModel.deleteClicked() },
{ viewModel.negateClicked() },
{ viewModel.submitClicked() },
{ viewModel.keyboardHidden = true })
} }
} }
} }
@@ -75,6 +71,8 @@ fun Portrait(viewModel: ICounterViewModel) {
TeamNamesView( TeamNamesView(
viewModel.teamNameA, viewModel.teamNameA,
viewModel.teamNameB, viewModel.teamNameB,
viewModel.teamNameSuggestionsA,
viewModel.teamNameSuggestionsB,
{ viewModel.updateNameA(it) }, { viewModel.updateNameA(it) },
{ viewModel.updateNameB(it) } { viewModel.updateNameB(it) }
) )
@@ -86,23 +84,12 @@ fun Portrait(viewModel: ICounterViewModel) {
RoundListView( RoundListView(
viewModel.roundScoreList, viewModel.roundScoreList,
viewModel.sumUpScores,
Modifier.weight(1f) Modifier.weight(1f)
) )
if (!viewModel.keyboardHidden) { if (!viewModel.keyboardHidden) {
KeyboardView( KeyBoardView(viewModel = viewModel)
viewModel.currentScoreA,
viewModel.currentScoreB,
viewModel.requestFocusA,
viewModel.enableSubmit,
{ viewModel.updateFocusStateA(it) },
{ viewModel.updateFocusStateB(it) },
{ viewModel.digitClicked(it) },
{ viewModel.addSub100Clicked(it) },
{ viewModel.deleteClicked() },
{ viewModel.negateClicked() },
{ viewModel.submitClicked() },
{ viewModel.keyboardHidden = true })
} }
} }
} }
@@ -124,17 +111,23 @@ internal class PreviewViewModel : ICounterViewModel {
override var totalScoreB: Int = 750 override var totalScoreB: Int = 750
override var teamNameA: String = "Team A" override var teamNameA: String = "Team A"
override var teamNameB: String = "Team B" override var teamNameB: String = "Team B"
override val sumUpScores: Boolean = false
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()
override var requestFocusB: FocusRequester = FocusRequester()
override var activeValue: String = currentScoreA override var activeValue: String = currentScoreA
override var inactiveValue: String = currentScoreB override var inactiveValue: String = currentScoreB
override var keyboardHidden: Boolean = false override var keyboardHidden: Boolean = false
override val teamNameSuggestionsA: List<String> =
listOf("TeamA", "asdffd", "TeamB", "really really long Team Name that is way too long")
override val teamNameSuggestionsB: List<String> =
listOf("TeamA", "asdffd", "TeamB", "really really long Team Name that is way too long")
override fun giveFocusToAIfNone() { override fun focusLastInput() {
} }
override fun updateOtherScore() { override fun updateOtherScore() {
@@ -159,9 +152,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) {
} }
@@ -174,4 +164,16 @@ internal class PreviewViewModel : ICounterViewModel {
override fun updateFocusStateB(state: Boolean) { override fun updateFocusStateB(state: Boolean) {
} }
override fun swapInputScores() {
}
override fun hideKeyboard() {
}
override fun showKeyboard() {
}
override fun deleteState(pressed: Boolean) {
}
} }

View File

@@ -7,31 +7,30 @@ 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.GameDao import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.domain.*
import me.zobrist.tichucounter.data.RoundDao
import me.zobrist.tichucounter.domain.Tichu
import me.zobrist.tichucounter.repository.GameRepository import me.zobrist.tichucounter.repository.GameRepository
import javax.inject.Inject import javax.inject.Inject
interface ICounterViewModel { private enum class Focused { TEAM_A, TEAM_B }
var roundScoreList: List<Round>
var totalScoreA: Int
var totalScoreB: Int
var teamNameA: String
var teamNameB: String
var currentScoreA: String
var currentScoreB: String
var enableSubmit: Boolean
var isAFocused: Boolean
var isBFocused: Boolean
var requestFocusA: FocusRequester
var activeValue: String
var inactiveValue: String
var keyboardHidden: Boolean
fun giveFocusToAIfNone() interface IKeyBoardViewModel {
val currentScoreA: String
val currentScoreB: String
val isValidRound: Boolean
val isAFocused: Boolean
val isBFocused: Boolean
val requestFocusA: FocusRequester
val requestFocusB: FocusRequester
val activeValue: String
val inactiveValue: String
val keyboardHidden: Boolean
fun focusLastInput()
fun updateOtherScore() fun updateOtherScore()
fun isValidTichuRound(): Boolean fun isValidTichuRound(): Boolean
fun updateSubmitButton() fun updateSubmitButton()
@@ -39,44 +38,83 @@ interface ICounterViewModel {
fun digitClicked(digit: String) fun digitClicked(digit: String)
fun negateClicked() fun negateClicked()
fun addSub100Clicked(toAdd: Int) fun addSub100Clicked(toAdd: Int)
fun deleteClicked()
fun updateNameA(value: String)
fun updateNameB(value: String)
fun updateFocusStateA(state: Boolean) fun updateFocusStateA(state: Boolean)
fun updateFocusStateB(state: Boolean) fun updateFocusStateB(state: Boolean)
fun swapInputScores()
fun hideKeyboard()
fun showKeyboard()
fun deleteState(pressed: Boolean)
}
interface ICounterViewModel : IKeyBoardViewModel {
val roundScoreList: List<Round>
val totalScoreA: Int
val totalScoreB: Int
val sumUpScores: Boolean
val teamNameA: String
val teamNameB: String
val teamNameSuggestionsA: List<String>
val teamNameSuggestionsB: List<String>
fun updateNameA(value: String)
fun updateNameB(value: String)
} }
@HiltViewModel @HiltViewModel
class CounterViewModel @Inject constructor( class CounterViewModel @Inject constructor(
private val gameRepository: GameRepository, private val gameRepository: GameRepository,
private val roundDao: RoundDao, private val settingsAdapter: SettingsAdapter
private val gameDao: GameDao
) : ) :
ViewModel(), ICounterViewModel { ViewModel(), ICounterViewModel, IDisplaySettingsChangeListener {
override var roundScoreList by mutableStateOf(emptyList<Round>()) override var roundScoreList by mutableStateOf(emptyList<Round>())
private set
override var totalScoreA by mutableStateOf(0) override var totalScoreA by mutableStateOf(0)
private set
override var totalScoreB by mutableStateOf(0) override var totalScoreB by mutableStateOf(0)
private set
override var teamNameA by mutableStateOf("") override var teamNameA by mutableStateOf("")
private set
override var sumUpScores by mutableStateOf(false)
private set
override var teamNameB by mutableStateOf("") override var teamNameB by mutableStateOf("")
private set
override var currentScoreA by mutableStateOf("") override var currentScoreA by mutableStateOf("")
private set
override var currentScoreB by mutableStateOf("") override var currentScoreB by mutableStateOf("")
private set
override var enableSubmit by mutableStateOf(false) override var isValidRound by mutableStateOf(false)
private set
override var isAFocused by mutableStateOf(false) override var isAFocused by mutableStateOf(false)
private set
override var isBFocused by mutableStateOf(false) override var isBFocused by mutableStateOf(false)
private set
override var requestFocusA by mutableStateOf(FocusRequester()) override var requestFocusA by mutableStateOf(FocusRequester())
private set
override var requestFocusB by mutableStateOf(FocusRequester())
private set
override var keyboardHidden by mutableStateOf(false) override var keyboardHidden by mutableStateOf(false)
private set
override var teamNameSuggestionsA by mutableStateOf(listOf<String>())
private set
override var teamNameSuggestionsB by mutableStateOf(listOf<String>())
private set
override var activeValue: String override var activeValue: String
get() { get() {
@@ -110,33 +148,54 @@ class CounterViewModel @Inject constructor(
} }
} }
private var lastFocused = Focused.TEAM_A
private var deletePressed = false
private var deleteJob: Job? = null
private var distinctTeamNames = listOf<String>()
init { init {
viewModelScope.launch { viewModelScope.launch {
roundDao.getForActiveGame().collect { gameRepository.getActiveGameFlow().collect {
roundScoreList = it
}
}
viewModelScope.launch {
gameDao.getActive().collect {
if (it != null) { if (it != null) {
teamNameA = it.nameA
teamNameB = it.nameB val score = it.getTotalPoints()
roundScoreList = it.rounds
totalScoreA = score.first
totalScoreB = score.second
teamNameA = it.game.nameA
teamNameB = it.game.nameB
buildTeamNameSuggestions()
} }
} }
} }
viewModelScope.launch { viewModelScope.launch {
roundDao.getRoundSumForActiveGame().collect { score -> gameRepository.getDistinctTeamNames().collect {
totalScoreA = score.scoreA distinctTeamNames = it
totalScoreB = score.scoreB
} buildTeamNameSuggestions()
} }
} }
override fun giveFocusToAIfNone() { settingsAdapter.registerOnChangeListener(this)
if (!isAFocused && !isBFocused) { }
requestFocusA.requestFocus()
override fun onCleared() {
settingsAdapter.unregisterOnChangeListener(this)
}
override fun focusLastInput() {
when (lastFocused) {
Focused.TEAM_A -> if (!isAFocused) requestFocusA.requestFocus()
Focused.TEAM_B -> if (!isBFocused) requestFocusB.requestFocus()
} }
} }
@@ -165,7 +224,7 @@ class CounterViewModel @Inject constructor(
} }
override fun updateSubmitButton() { override fun updateSubmitButton() {
enableSubmit = isValidTichuRound() isValidRound = isValidTichuRound()
} }
override fun submitClicked() { override fun submitClicked() {
@@ -174,19 +233,31 @@ class CounterViewModel @Inject constructor(
} }
currentScoreA = "" currentScoreA = ""
currentScoreB = "" currentScoreB = ""
enableSubmit = false isValidRound = false
} }
override fun digitClicked(digit: String) { override fun digitClicked(digit: String) {
giveFocusToAIfNone() focusLastInput()
if (activeValue.digitCount() >= 5) {
// 5 digits is enough
return
}
val newValue = activeValue + digit
try {
activeValue = newValue.toInt().toString()
} catch (_: NumberFormatException) {
}
activeValue += digit
updateOtherScore() updateOtherScore()
updateSubmitButton() updateSubmitButton()
} }
override fun negateClicked() { override fun negateClicked() {
giveFocusToAIfNone() focusLastInput()
activeValue = if (activeValue.contains("-")) { activeValue = if (activeValue.contains("-")) {
activeValue.replace("-", "") activeValue.replace("-", "")
@@ -198,7 +269,7 @@ class CounterViewModel @Inject constructor(
} }
override fun addSub100Clicked(toAdd: Int) { override fun addSub100Clicked(toAdd: Int) {
giveFocusToAIfNone() focusLastInput()
activeValue = try { activeValue = try {
val temp = activeValue.toInt() + toAdd val temp = activeValue.toInt() + toAdd
@@ -213,7 +284,61 @@ class CounterViewModel @Inject constructor(
updateSubmitButton() updateSubmitButton()
} }
override fun deleteClicked() { override fun updateNameA(value: String) {
teamNameA = value
viewModelScope.launch {
gameRepository.updateActiveTeamName(nameA = value)
}
}
override fun updateNameB(value: String) {
teamNameB = value
viewModelScope.launch {
gameRepository.updateActiveTeamName(nameB = value)
}
}
override fun updateFocusStateA(state: Boolean) {
isAFocused = state
if (state) {
lastFocused = Focused.TEAM_A
}
}
override fun updateFocusStateB(state: Boolean) {
isBFocused = state
if (state) {
lastFocused = Focused.TEAM_B
}
}
override fun swapInputScores() {
val swap = currentScoreA
currentScoreA = currentScoreB
currentScoreB = swap
}
override fun hideKeyboard() {
keyboardHidden = true
}
override fun showKeyboard() {
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 != "") { if (activeValue != "") {
activeValue = activeValue.dropLast(1) activeValue = activeValue.dropLast(1)
} }
@@ -221,27 +346,33 @@ class CounterViewModel @Inject constructor(
updateSubmitButton() updateSubmitButton()
} }
override fun updateNameA(value: String) { private fun deleteRepeatedlyUntilRelease(): Job {
viewModelScope.launch { return viewModelScope.launch {
val game = gameRepository.activeGame deleteLastDigitActive()
game.nameA = value delay(500)
gameRepository.updateGame(game) while (deletePressed) {
deleteLastDigitActive()
delay(100)
}
} }
} }
override fun updateNameB(value: String) { private fun buildTeamNameSuggestions() {
viewModelScope.launch { teamNameSuggestionsA = buildTypeaheadList(distinctTeamNames, teamNameA)
val game = gameRepository.activeGame teamNameSuggestionsB = buildTypeaheadList(distinctTeamNames, teamNameB)
game.nameB = value
gameRepository.updateGame(game)
}
} }
override fun updateFocusStateA(state: Boolean) { private fun buildTypeaheadList(rawList: List<String>, currentInput: String): List<String> {
isAFocused = state var filtered = rawList.filter { it.isNotEmpty() && it != currentInput }
if (currentInput.isNotEmpty()) {
filtered = filtered.filter { it.contains(currentInput, ignoreCase = true) }
} }
override fun updateFocusStateB(state: Boolean) { return filtered.sorted().sortedBy { it.length }.take(10)
isBFocused = state }
override fun onCounterListViewChanged(listView: CounterListView) {
sumUpScores = listView == CounterListView.CONTINUOUS
} }
} }

View File

@@ -1,74 +1,109 @@
package me.zobrist.tichucounter.ui.counter package me.zobrist.tichucounter.ui.counter
import android.content.res.Configuration import android.content.res.Configuration
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
import androidx.compose.material.icons.outlined.Check import androidx.compose.material.icons.outlined.Check
import androidx.compose.material.icons.outlined.KeyboardHide import androidx.compose.material.icons.outlined.KeyboardHide
import androidx.compose.material.icons.outlined.SwapHoriz
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.FocusState
import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
@Composable
fun KeyBoardView(viewModel: IKeyBoardViewModel) {
KeyboardView(
viewModel.currentScoreA,
viewModel.currentScoreB,
viewModel.requestFocusA,
viewModel.requestFocusB,
viewModel.isValidRound,
viewModel.isAFocused,
viewModel.isBFocused,
{ viewModel.updateFocusStateA(it) },
{ viewModel.updateFocusStateB(it) },
{ viewModel.digitClicked(it) },
{ viewModel.addSub100Clicked(it) },
{ viewModel.negateClicked() },
{ viewModel.submitClicked() },
{ viewModel.hideKeyboard() },
{ viewModel.swapInputScores() },
{ viewModel.deleteState(it) }
)
}
@OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
fun KeyboardView( fun KeyboardView(
scoreA: String, scoreA: String,
scoreB: String, scoreB: String,
requestFocus: FocusRequester, requestFocusA: FocusRequester,
enableSubmit: Boolean, requestFocusB: FocusRequester,
isValidScore: Boolean,
focusStateA: 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,
deleteButtonPressedState: (Boolean) -> Unit
) { ) {
val keyboardController = LocalSoftwareKeyboardController.current
Column { Column {
Row { Row(Modifier.height(IntrinsicSize.Max)) {
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
CenteredTextField( CenteredTextField(
scoreA, scoreA,
"0", "0",
Modifier focusStateA,
.focusRequester(requestFocus) requestFocusA
.onFocusChanged { ) { updateFocusStateA(it.isFocused) }
keyboardController?.hide()
updateFocusStateA(it.isFocused)
}
)
} }
Surface(
Modifier
.wrapContentWidth()
.fillMaxHeight(),
tonalElevation = 3.dp,
shape = MaterialTheme.shapes.extraSmall
) {
Column {
IconButton(onClick = onSwapClicked, enabled = isValidScore) {
Icon(Icons.Outlined.SwapHoriz, null)
}
}
}
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
CenteredTextField( CenteredTextField(
scoreB, scoreB,
"0", "0",
Modifier focusStateB,
.onFocusChanged { requestFocusB
keyboardController?.hide() ) {
updateFocusStateB(it.isFocused) updateFocusStateB(it.isFocused)
} }
)
} }
} }
Row { Row {
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
KeyboardTextButton("1") { KeyboardTextButton("1") {
@@ -131,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
) {}
} }
} }
@@ -155,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()
} }
} }
@@ -186,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() },
@@ -195,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,
@@ -203,28 +251,67 @@ fun KeyboardIconButton(icon: ImageVector, enabled: Boolean = true, onClicked: ()
} }
} }
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun CenteredTextField( fun CenteredTextField(
value: String, value: String,
placeholder: String, placeholder: String,
modifier: Modifier, focused: Boolean,
focusRequester: FocusRequester? = null,
onFocusStateChanged: (FocusState) -> Unit
) { ) {
val modifier = if (focusRequester != null) {
Modifier.focusRequester(focusRequester)
} else {
Modifier
}
Box(contentAlignment = Alignment.Center) {
TextField( TextField(
value = value, value = value,
onValueChange = { }, onValueChange = { },
placeholder = { placeholder = {
if (!focused) {
Text( Text(
placeholder, placeholder,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) )
}
}, },
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center), textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
singleLine = true, singleLine = true,
readOnly = true, readOnly = true,
modifier = modifier.fillMaxWidth() modifier = modifier
.fillMaxWidth()
.onFocusChanged {
onFocusStateChanged(it)
}
) )
if (focused) {
val cursorColor = MaterialTheme.colorScheme.onSurface
val infiniteTransition = rememberInfiniteTransition()
val alpha by infiniteTransition.animateFloat(
0f,
cursorColor.alpha,
animationSpec = infiniteRepeatable(
animation = tween(500),
repeatMode = RepeatMode.Reverse
)
)
Row {
Text(text = value, color = cursorColor.copy(alpha = 0f))
Divider(
modifier = Modifier
.padding(start = 3.dp, top = 15.dp, bottom = 15.dp)
.width(1.dp)
.fillMaxHeight(),
color = cursorColor.copy(alpha = alpha)
)
}
}
}
} }
@@ -235,18 +322,22 @@ fun KeyboardViewPreview() {
AppTheme { AppTheme {
Surface { Surface {
KeyboardView( KeyboardView(
"", "10",
"350", "190",
FocusRequester(), FocusRequester(),
false, FocusRequester(),
{}, isValidScore = false,
{}, focusStateA = true,
{}, focusStateB = false,
{}, updateFocusStateA = {},
{}, updateFocusStateB = {},
{}, digitClicked = {},
{}, addSub100Clicked = {},
{}) negateClicked = {},
submitClicked = {},
hideKeyboardClicked = {},
onSwapClicked = {},
deleteButtonPressedState = {})
} }
} }
} }

View File

@@ -17,17 +17,26 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.data.Round import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
@Composable @Composable
fun RoundListView(rounds: List<Round>, modifier: Modifier) { fun RoundListView(rounds: List<Round>, sumUpRounds: Boolean = false, modifier: Modifier) {
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
var sumA = 0
var sumB = 0
LazyColumn(state = lazyListState, modifier = modifier) { LazyColumn(state = lazyListState, modifier = modifier) {
itemsIndexed(rounds) { index, item -> itemsIndexed(rounds) { index, item ->
RoundListItem(item, index) if (sumUpRounds) {
sumA += item.scoreA
sumB += item.scoreB
RoundListItem(sumA, sumB, index)
} else {
RoundListItem(item.scoreA, item.scoreB, index)
}
} }
scope.launch { scope.launch {
@@ -37,14 +46,14 @@ fun RoundListView(rounds: List<Round>, modifier: Modifier) {
} }
@Composable @Composable
private fun RoundListItem(round: Round, index: Int) { private fun RoundListItem(scoreA: Int, scoreB: Int, index: Int) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(all = 4.dp) .padding(all = 4.dp)
) { ) {
Text( Text(
text = round.scoreA.toString(), text = scoreA.toString(),
style = MaterialTheme.typography.bodyMedium, style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.weight(5f), modifier = Modifier.weight(5f),
textAlign = TextAlign.Center textAlign = TextAlign.Center
@@ -56,7 +65,7 @@ private fun RoundListItem(round: Round, index: Int) {
textAlign = TextAlign.Center textAlign = TextAlign.Center
) )
Text( Text(
text = round.scoreB.toString(), text = scoreB.toString(),
style = MaterialTheme.typography.bodyMedium, style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.weight(5f), modifier = Modifier.weight(5f),
textAlign = TextAlign.Center textAlign = TextAlign.Center
@@ -78,7 +87,7 @@ fun RoundListViewPreview() {
AppTheme { AppTheme {
Surface { Surface {
RoundListView(rounds, Modifier) RoundListView(rounds, true, Modifier)
} }
} }
} }

View File

@@ -2,46 +2,53 @@ package me.zobrist.tichucounter.ui.counter
import android.content.res.Configuration import android.content.res.Configuration
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
import me.zobrist.tichucounter.ui.composables.TypeaheadTextField
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun TeamNamesView( fun TeamNamesView(
nameA: String, nameA: String,
nameB: String, nameB: String,
nameSuggestionsA: List<String>,
nameSuggestionsB: List<String>,
updateA: (String) -> Unit, updateA: (String) -> Unit,
updateB: (String) -> Unit updateB: (String) -> Unit
) { ) {
val color = TextFieldDefaults.textFieldColors( val containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(1.dp)
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(1.dp) val color = TextFieldDefaults.colors(
focusedContainerColor = containerColor,
unfocusedContainerColor = containerColor,
disabledContainerColor = containerColor,
) )
Row { Row {
TextField(
TypeaheadTextField(
value = nameA, value = nameA,
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center), items = nameSuggestionsA,
onValueChange = { updateA(it) }, onValueChange = { updateA(it) },
singleLine = true,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
colors = color colors = color,
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center)
) )
TextField( TypeaheadTextField(
value = nameB, value = nameB,
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center), items = nameSuggestionsB,
onValueChange = { updateB(it) }, onValueChange = { updateB(it) },
singleLine = true,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
colors = color colors = color,
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center)
) )
} }
} }
@@ -50,6 +57,6 @@ fun TeamNamesView(
@Composable @Composable
private fun TeamNamesViewPreview() { private fun TeamNamesViewPreview() {
AppTheme { AppTheme {
TeamNamesView("TeamA", "TeamB", {}, {}) TeamNamesView("TeamA", "TeamB", listOf("Test1", "Test3"), listOf("Test3", "Test5"), {}, {})
} }
} }

View File

@@ -4,7 +4,11 @@ import android.content.res.Configuration
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.* import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign

View File

@@ -1,41 +1,73 @@
package me.zobrist.tichucounter.ui.history package me.zobrist.tichucounter.ui.history
import androidx.compose.foundation.layout.* import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.DeleteForever
import androidx.compose.material.icons.outlined.OpenInFull import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material3.* import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.R import me.zobrist.tichucounter.R
import me.zobrist.tichucounter.data.GameAndScore import me.zobrist.tichucounter.data.GameWithScores
import me.zobrist.tichucounter.data.entity.Game
import me.zobrist.tichucounter.data.entity.Round
import me.zobrist.tichucounter.domain.getTotalPoints
import me.zobrist.tichucounter.ui.composables.DropDownMenu
import java.text.DateFormat import java.text.DateFormat
import java.util.* import java.util.Date
import java.util.Locale
@Composable @Composable
fun HistoryList( fun HistoryList(
viewModel: HistoryViewModel, viewModel: HistoryViewModel,
showDeleteDialog: Boolean,
onDialogExecuted: (Boolean) -> Unit,
navigateToCalculator: () -> Unit navigateToCalculator: () -> Unit
) { ) {
DeleteConfirmDialog(showDeleteDialog, onDialogExecuted) var showDeleteDialog by remember { mutableStateOf(false) }
HistoryList(viewModel.gameAndHistory, DeleteConfirmDialog(showDeleteDialog) {
showDeleteDialog = false
if (it) {
viewModel.deleteAllInactiveGames()
}
}
HistoryList(
viewModel.gameAndHistory,
{ {
viewModel.activateGame(it) viewModel.activateGame(it)
navigateToCalculator() navigateToCalculator()
}, },
{ viewModel.deleteGame(it) }) { viewModel.deleteGame(it) },
{ showDeleteDialog = true },
)
} }
@Preview @Preview
@@ -65,22 +97,58 @@ fun DeleteConfirmDialog(show: Boolean = true, onExecuted: (Boolean) -> Unit = {}
@Composable @Composable
fun HistoryList( fun HistoryList(
games: List<GameAndScore>, games: List<GameWithScores>,
onOpenClicked: (GameId: Long) -> Unit, onOpenClicked: (GameId: Long) -> Unit,
onDeleteClicked: (GameId: Long) -> Unit onDeleteClicked: (GameId: Long) -> Unit,
onDeleteAllClicked: () -> Unit
) { ) {
Row { Row {
LazyColumn { LazyColumn {
items(games) { item {
Text(
modifier = Modifier.padding(start = 10.dp, end = 10.dp),
text = stringResource(R.string.active),
style = MaterialTheme.typography.headlineSmall
)
}
items(games.filter { it.game.active }) {
HistoryListItem(it, onOpenClicked, onDeleteClicked) HistoryListItem(it, onOpenClicked, onDeleteClicked)
} }
if (games.count() > 1) {
item {
Text(
modifier = Modifier.padding(start = 10.dp, end = 10.dp, top = 10.dp),
text = stringResource(R.string.inactive),
style = MaterialTheme.typography.headlineSmall
)
}
items(games.filter { !it.game.active }) {
HistoryListItem(it, onOpenClicked, onDeleteClicked)
}
item {
Button(
enabled = games.count() > 1,
modifier = Modifier
.padding(start = 4.dp, end = 4.dp, top = 10.dp)
.align(CenterVertically)
.fillMaxWidth(),
onClick = { onDeleteAllClicked() }) {
Icon(imageVector = Icons.Outlined.DeleteForever, contentDescription = null)
Text(text = stringResource(id = R.string.deleteAll))
}
}
}
} }
} }
} }
@Composable @Composable
fun HistoryListItem( fun HistoryListItem(
game: GameAndScore, game: GameWithScores,
onOpenClicked: (GameId: Long) -> Unit, onOpenClicked: (GameId: Long) -> Unit,
onDeleteClicked: (GameId: Long) -> Unit onDeleteClicked: (GameId: Long) -> Unit
) { ) {
@@ -88,17 +156,20 @@ fun HistoryListItem(
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault()) DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault())
val cardColor = if (game.active) { val cardColor = if (game.game.active) {
CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer) CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer)
} else { } else {
CardDefaults.cardColors() CardDefaults.cardColors()
} }
val totalScores = game.getTotalPoints()
Card( Card(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(all = 4.dp), .padding(all = 4.dp)
.clickable { onOpenClicked(game.game.uid) },
colors = cardColor colors = cardColor
) { ) {
Row( Row(
@@ -107,34 +178,51 @@ fun HistoryListItem(
) { ) {
Column(Modifier.weight(4f)) { Column(Modifier.weight(4f)) {
Text( Text(
text = game.nameA + " vs " + game.nameB, text = game.game.nameA + " vs " + game.game.nameB,
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.headlineSmall style = MaterialTheme.typography.headlineSmall
) )
Text( Text(
text = game.scoreA.toString() + " : " + game.scoreB.toString(), text = totalScores.first.toString() + " : " + totalScores.second.toString(),
style = MaterialTheme.typography.bodyLarge style = MaterialTheme.typography.bodyLarge
) )
Spacer(modifier = Modifier.padding(5.dp)) Spacer(modifier = Modifier.padding(5.dp))
Text( Text(
text = format.format(game.modified), text = format.format(game.game.modified),
style = MaterialTheme.typography.labelSmall style = MaterialTheme.typography.labelSmall
) )
} }
Column( Column(
Modifier Modifier
.wrapContentSize() .wrapContentSize()
.width(70.dp) .width(40.dp)
) { ) {
ElevatedButton(onClick = { onOpenClicked(game.gameId) }, enabled = true) { if (!game.game.active) {
Icon(Icons.Outlined.OpenInFull, null) var expanded by remember { mutableStateOf(false) }
}
ElevatedButton( Icon(
onClick = { onDeleteClicked(game.gameId) }, enabled = !game.active modifier = Modifier
.padding(start = 20.dp, bottom = 20.dp)
.clickable { expanded = true },
imageVector = Icons.Outlined.MoreVert,
contentDescription = null
)
DropDownMenu(
mapOf("delete" to R.string.delete),
"",
expanded,
) { ) {
Icon(Icons.Outlined.Delete, null) expanded = false
it?.let {
when (it) {
"delete" -> onDeleteClicked(game.game.uid)
}
}
}
} }
} }
} }
@@ -145,11 +233,26 @@ fun HistoryListItem(
@Composable @Composable
private fun HistoryListPreview() { private fun HistoryListPreview() {
val tempData = listOf( val tempData = listOf(
GameAndScore(true, "abc", "def", Date(), Date(), 1, 10, 50), GameWithScores(
GameAndScore(false, "ADTH", "dogfg", Date(), Date(), 2, 20, 60), Game(true, "abc", "def", Date(), Date()),
GameAndScore(false, "TeamA3 langer Name", "TeamB3", Date(), Date(), 3, 30, 70), listOf(Round(1, 550, 500))
GameAndScore(false, "TeamA4", "TeamB4", Date(), Date(), 4, 40, 80), ),
GameAndScore(false, "TeamA5", "TeamB5", Date(), Date(), 5, 50, 90) GameWithScores(
Game(false, "ADTH", "dogfg", Date(), Date()),
listOf(Round(2, 20, 60))
),
GameWithScores(
Game(false, "TeamA3 langer Name", "TeamB3", Date(), Date()),
listOf(Round(3, 30, 70))
),
GameWithScores(
Game(false, "TeamA4", "TeamB4", Date(), Date()),
listOf(Round(4, 40, 80))
),
GameWithScores(
Game(false, "TeamA5", "TeamB5", Date(), Date()),
listOf(Round(5, 50, 90))
) )
HistoryList(tempData, {}) {} )
HistoryList(tempData, {}, {}) {}
} }

View File

@@ -7,25 +7,25 @@ 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.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.data.GameAndScore import me.zobrist.tichucounter.data.GameWithScores
import me.zobrist.tichucounter.data.GameDao
import me.zobrist.tichucounter.repository.GameRepository import me.zobrist.tichucounter.repository.GameRepository
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
class HistoryViewModel @Inject constructor( class HistoryViewModel @Inject constructor(
private val gameDao: GameDao,
private val gameRepository: GameRepository private val gameRepository: GameRepository
) : ViewModel() { ) : ViewModel() {
var gameAndHistory by mutableStateOf(emptyList<GameAndScore>()) var gameAndHistory by mutableStateOf(emptyList<GameWithScores>())
private set private set
init { init {
viewModelScope.launch { viewModelScope.launch {
gameDao.getAllWithPoints().collect { games ->
gameAndHistory = games gameRepository.getAllWithRoundFlow().collect { games ->
gameAndHistory =
games.sortedBy { it.game.modified }.sortedBy { it.game.active }.reversed()
} }
} }
} }
@@ -42,4 +42,10 @@ class HistoryViewModel @Inject constructor(
} }
} }
fun deleteAllInactiveGames() {
viewModelScope.launch {
gameRepository.deleteAllInactive()
}
}
} }

View File

@@ -0,0 +1,63 @@
package me.zobrist.tichucounter.ui.layout
import android.content.res.Configuration
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.outlined.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.R
import me.zobrist.tichucounter.domain.*
import me.zobrist.tichucounter.ui.AppTheme
import me.zobrist.tichucounter.ui.counter.*
@Composable
fun DrawerContent(
drawerItems: List<DrawerItem>,
selectedDrawerItem: DrawerItem,
onElementClicked: (Route) -> Unit
) {
ModalDrawerSheet {
Text(
modifier = Modifier.padding(start = 10.dp, top = 10.dp),
text = stringResource(R.string.app_name),
style = MaterialTheme.typography.headlineSmall
)
Divider(modifier = Modifier.padding(10.dp))
drawerItems.forEach { screen ->
NavigationDrawerItem(
icon = { Icon(screen.menuIcon, contentDescription = null) },
label = { Text(screen.menuName) },
selected = screen == selectedDrawerItem,
onClick = { onElementClicked(screen.route) },
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
}
}
}
@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun DrawerContentPreview() {
val counter = DrawerItem(Route.COUNTER, Icons.Outlined.Calculate, "Counter")
val history = DrawerItem(Route.HISTORY, Icons.Outlined.List, "History")
val settings = DrawerItem(Route.SETTINGS, Icons.Outlined.Settings, "Settings")
AppTheme {
Surface {
DrawerContent(
listOf(counter, history, settings),
counter
) {}
}
}
}

View File

@@ -0,0 +1,56 @@
package me.zobrist.tichucounter.ui.layout
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextOverflow
import me.zobrist.tichucounter.domain.TopBarAction
import me.zobrist.tichucounter.domain.TopBarState
@Composable
fun TopBar(topBarState: TopBarState) {
TopBar(
topBarState.title,
topBarState.icon,
topBarState.onNavigate,
topBarState.actions
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar(
title: String,
icon: ImageVector,
navigateAction: () -> Unit,
actions: List<TopBarAction>
) {
TopAppBar(
title = {
Text(
title,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
navigationIcon = {
IconButton(onClick = { navigateAction() }) {
Icon(
imageVector = icon,
contentDescription = "Localized description"
)
}
},
actions = {
actions.forEach {
IconButton(onClick = { it.action() }, enabled = it.isActive) {
Icon(
imageVector = it.imageVector,
contentDescription = null,
)
it.composeCode()
}
}
}
)
}

View File

@@ -8,9 +8,16 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowDropDown import androidx.compose.material.icons.outlined.ArrowDropDown
import androidx.compose.material.icons.outlined.Check import androidx.compose.material3.Icon
import androidx.compose.material3.* import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.* import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.End import androidx.compose.ui.Alignment.Companion.End
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@@ -18,12 +25,16 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.zobrist.tichucounter.R import me.zobrist.tichucounter.R
import me.zobrist.tichucounter.domain.CounterListView
import me.zobrist.tichucounter.domain.KeepScreenOn
import me.zobrist.tichucounter.domain.Language import me.zobrist.tichucounter.domain.Language
import me.zobrist.tichucounter.domain.Theme import me.zobrist.tichucounter.domain.Theme
import me.zobrist.tichucounter.ui.AppTheme import me.zobrist.tichucounter.ui.AppTheme
import me.zobrist.tichucounter.ui.composables.DropDownMenu
val languageMap = mapOf( val languageMap = mapOf(
Language.DEFAULT to R.string.android_default_text,
Language.ENGLISH to R.string.english, Language.ENGLISH to R.string.english,
Language.GERMAN to R.string.german Language.GERMAN to R.string.german
) )
@@ -34,16 +45,23 @@ val themeMap = mapOf(
Theme.LIGHT to R.string.light Theme.LIGHT to R.string.light
) )
val counterListViewMap = mapOf(
CounterListView.BY_ROUND to R.string.by_round,
CounterListView.CONTINUOUS to R.string.continuous,
)
@Composable @Composable
fun SettingsView(viewModel: SettingsViewModel) { fun SettingsView(viewModel: SettingsViewModel) {
SettingsView( SettingsView(
viewModel.screenOn, viewModel.screenOn.value,
viewModel.language, viewModel.language,
viewModel.theme, viewModel.theme,
viewModel.counterListView,
{ viewModel.updateScreenOn(it) }, { viewModel.updateScreenOn(it) },
{ viewModel.updateLanguage(it) }, { viewModel.updateLanguage(it) },
{ viewModel.updateTheme(it) }) { viewModel.updateTheme(it) },
{ viewModel.updateCounterListView(it) })
} }
@Composable @Composable
@@ -51,15 +69,17 @@ fun SettingsView(
valueScreenOn: Boolean = true, valueScreenOn: Boolean = true,
valueLanguage: Language = Language.ENGLISH, valueLanguage: Language = Language.ENGLISH,
valueTheme: Theme = Theme.DARK, valueTheme: Theme = Theme.DARK,
updateScreenOn: (Boolean) -> Unit = {}, valueCounterListView: CounterListView = CounterListView.BY_ROUND,
updateScreenOn: (KeepScreenOn) -> Unit = {},
updateLanguage: (Language) -> Unit = {}, updateLanguage: (Language) -> Unit = {},
updateTheme: (Theme) -> Unit = {} updateTheme: (Theme) -> Unit = {},
updateCounterListView: (CounterListView) -> Unit = {}
) { ) {
Column { Column {
BooleanSetting( BooleanSetting(
stringResource(R.string.keep_screen_on), stringResource(R.string.keep_screen_on),
valueScreenOn valueScreenOn
) { updateScreenOn(it) } ) { updateScreenOn(if (it) KeepScreenOn.ON else KeepScreenOn.OFF) }
StringSetting( StringSetting(
stringResource(R.string.choose_language_text), stringResource(R.string.choose_language_text),
@@ -72,6 +92,14 @@ fun SettingsView(
themeMap, themeMap,
valueTheme, valueTheme,
) { updateTheme(it) } ) { updateTheme(it) }
StringSetting(
stringResource(R.string.counter_mode),
counterListViewMap,
valueCounterListView,
) { updateCounterListView(it) }
} }
} }
@@ -118,7 +146,12 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
.clickable { expanded = true }) { .clickable { expanded = true }) {
Column(Modifier.weight(5f)) { Column(Modifier.weight(5f)) {
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis) Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge) map[selected]?.let {
Text(
stringResource(it),
style = MaterialTheme.typography.labelLarge
)
}
} }
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
@@ -129,27 +162,18 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
) )
} }
DropdownMenu( DropDownMenu(
expanded = expanded, map,
onDismissRequest = { expanded = false } selected,
expanded,
) { ) {
map.forEach {
DropdownMenuItem(
onClick = {
onSelected(it.key)
expanded = false expanded = false
}, it?.let { onSelected(it) }
text = { Text(stringResource(it.value)) },
trailingIcon = {
if (it.key == selected) {
Icon(Icons.Outlined.Check, contentDescription = null)
}
})
}
} }
} }
} }
@Preview(name = "Light Mode") @Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true) @Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable @Composable
@@ -161,3 +185,20 @@ fun SettingsViewPreview() {
} }
} }
} }
@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun StringSettingPreview() {
AppTheme {
Surface {
DropDownMenu(
themeMap,
Theme.LIGHT,
true,
) {}
}
}
}

View File

@@ -5,9 +5,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import me.zobrist.tichucounter.domain.Language import me.zobrist.tichucounter.domain.*
import me.zobrist.tichucounter.domain.SettingsAdapter
import me.zobrist.tichucounter.domain.Theme
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@@ -20,7 +18,10 @@ class SettingsViewModel @Inject constructor(private val settings: SettingsAdapte
var theme by mutableStateOf(settings.theme) var theme by mutableStateOf(settings.theme)
private set private set
var screenOn by mutableStateOf(false) var screenOn by mutableStateOf(settings.keepScreenOn)
private set
var counterListView by mutableStateOf(settings.counterListView)
private set private set
fun updateLanguage(language: Language) { fun updateLanguage(language: Language) {
@@ -33,9 +34,14 @@ class SettingsViewModel @Inject constructor(private val settings: SettingsAdapte
this.theme = settings.theme this.theme = settings.theme
} }
fun updateScreenOn(value: Boolean) { fun updateScreenOn(value: KeepScreenOn) {
settings.setKeepScreenOn(value) settings.setKeepScreenOn(value)
screenOn = settings.keepScreenOn screenOn = settings.keepScreenOn
} }
fun updateCounterListView(value: CounterListView) {
settings.setCounterViewList(value)
counterListView = settings.counterListView
}
} }

View File

@@ -0,0 +1,54 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="50.684"
android:viewportHeight="39.77">
<group
android:scaleX="0.46444446"
android:scaleY="0.36443365"
android:translateX="13.572049"
android:translateY="12.638237">
<path
android:fillColor="#ffff00"
android:pathData="m25.885,0.371 l-25.752,0.047 0.094,9.167 8.174,0.094 -0.094,29.957 9.592,-0.094 -0.047,-29.957h8.08z"
android:strokeWidth="0.26458300000000001"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#ffff00"
android:pathData="m50.503,39.494 l-0.047,-9.214h-10.773c-0.738,-0.009 -2.147,-0.64 -2.139,-1.95 0.009,-1.415 0.012,-16.903 0.012,-16.903 -0.001,-1.41 1.227,-2.055 1.89,-2.079l11.009,-0.142 0.094,-9.072 -11.86,0.189c-2.609,0.059 -10.536,2.804 -10.537,13.23 -0,10.079 0.218,15.921 0.218,15.921 0.223,4.386 5.741,9.979 10.125,9.99l12.006,0.03z"
android:strokeWidth="0.26458300000000001"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m8.403,10.083c0.049,-0.713 -0.112,-0.88 -0.029,-2.095 0.366,-1.278 9.609,-5.62 16.985,-2.65 0.403,0.162 0.5,-0.375 0.299,-0.467 -2.964,-2.692 -13.318,-8.128 -25.314,0.286l8.059,4.521"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m13.09,39.45 l-4.651,-7.072 1.436,-0.155c1.716,-3.55 8.698,-15.304 2.46,-24.75 -0.307,-0.496 0.802,-2.297 1.512,-0.563 5.772,10.547 5.161,23.028 -0.757,32.54z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m42.812,9.194 l7.51,-4.144c-4.938,-5.973 -14.39,-7.27 -20.302,4.385 -0.2,0.447 0.901,0.888 1.106,0.649 5.387,-7.535 11.325,-3.968 11.686,-2.476 0.158,0.461 0.006,1.054 0.001,1.585z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m42.429,30.41 l7.935,4.743c-13.982,13.838 -26.059,-8.724 -21.01,-24.194 0.143,-0.438 1.306,-0.12 1.201,0.408 -1.411,6.97 1.621,22.234 11.726,20.975z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
</group>
</vector>

View File

@@ -0,0 +1,48 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="50.684dp"
android:height="39.77dp"
android:viewportWidth="50.684"
android:viewportHeight="39.77">
<path
android:fillColor="#ffff00"
android:pathData="m25.885,0.371 l-25.752,0.047 0.094,9.167 8.174,0.094 -0.094,29.957 9.592,-0.094 -0.047,-29.957h8.08z"
android:strokeWidth="0.26458300000000001"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#ffff00"
android:pathData="m50.503,39.494 l-0.047,-9.214h-10.773c-0.738,-0.009 -2.147,-0.64 -2.139,-1.95 0.009,-1.415 0.012,-16.903 0.012,-16.903 -0.001,-1.41 1.227,-2.055 1.89,-2.079l11.009,-0.142 0.094,-9.072 -11.86,0.189c-2.609,0.059 -10.536,2.804 -10.537,13.23 -0,10.079 0.218,15.921 0.218,15.921 0.223,4.386 5.741,9.979 10.125,9.99l12.006,0.03z"
android:strokeWidth="0.26458300000000001"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m8.403,10.083c0.049,-0.713 -0.112,-0.88 -0.029,-2.095 0.366,-1.278 9.609,-5.62 16.985,-2.65 0.403,0.162 0.5,-0.375 0.299,-0.467 -2.964,-2.692 -13.318,-8.128 -25.314,0.286l8.059,4.521"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m13.09,39.45 l-4.651,-7.072 1.436,-0.155c1.716,-3.55 8.698,-15.304 2.46,-24.75 -0.307,-0.496 0.802,-2.297 1.512,-0.563 5.772,10.547 5.161,23.028 -0.757,32.54z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m42.812,9.194 l7.51,-4.144c-4.938,-5.973 -14.39,-7.27 -20.302,4.385 -0.2,0.447 0.901,0.888 1.106,0.649 5.387,-7.535 11.325,-3.968 11.686,-2.476 0.158,0.461 0.006,1.054 0.001,1.585z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
<path
android:fillColor="#000000"
android:pathData="m42.429,30.41 l7.935,4.743c-13.982,13.838 -26.059,-8.724 -21.01,-24.194 0.143,-0.438 1.306,-0.12 1.201,0.408 -1.411,6.97 1.621,22.234 11.726,20.975z"
android:strokeWidth="0.264583"
android:strokeColor="#000000"
android:strokeLineCap="butt"
android:strokeLineJoin="miter" />
</vector>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background" /> <background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" /> <foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon> </adaptive-icon>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background" /> <background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" /> <foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon> </adaptive-icon>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -13,11 +13,16 @@
<string name="on">Ein</string> <string name="on">Ein</string>
<string name="off">Aus</string> <string name="off">Aus</string>
<string name="newGame">Neues Spiel</string> <string name="newGame">Neues Spiel</string>
<string name="created">"Erstellt: %s "</string>
<string name="modified">Bearbeitet: %s</string>
<string name="delete_inactive_title">Verlauf löschen</string> <string name="delete_inactive_title">Verlauf löschen</string>
<string name="delete_inactive_text">Wirklich den gesamten Verlauf löschen? Diese Aktion kann nicht rückgängig gemacht werden.</string> <string name="delete_inactive_text">Wirklich den gesamten Verlauf löschen? Diese Aktion kann nicht rückgängig gemacht werden.</string>
<string name="cancel">Abbrechen</string> <string name="cancel">Abbrechen</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="delete">Löschen</string>
<string name="deleteAll">Alle löschen</string>
<string name="active">Aktives Spiel</string>
<string name="inactive">Vergangene Spiele</string>
<string name="menu_counter">Counter</string>
<string name="menu_about">About</string>
<string name="contact_us">Schreib uns</string>
</resources> </resources>

View File

@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#d50000</color>
</resources>

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="ic_launcher_background">@color/primaryColor</color> <color name="ic_launcher_background">#DC0E00</color>
</resources> </resources>

View File

@@ -16,10 +16,19 @@
<string name="on">On</string> <string name="on">On</string>
<string name="off">Off</string> <string name="off">Off</string>
<string name="newGame">New Game</string> <string name="newGame">New Game</string>
<string name="created">Created: %s</string>
<string name="modified">Modified: %s</string>
<string name="delete_inactive_title">Delete history</string> <string name="delete_inactive_title">Delete history</string>
<string name="delete_inactive_text">You really want to delete the the history? This action can\'t be undone.</string> <string name="delete_inactive_text">You really want to delete the the history? This action can\'t be undone.</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="ok">Ok</string> <string name="ok">OK</string>
<string name="delete">Delete</string>
<string name="deleteAll">Delete all</string>
<string name="active">Current Game</string>
<string name="inactive">Old Games</string>
<string name="menu_counter">Counter</string>
<string name="menu_about">About</string>
<string name="by_round">Display Single Round</string>
<string name="continuous">Sum up rounds</string>
<string name="counter_mode">Counter Mode</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

@@ -1,7 +1,9 @@
package me.zobrist.tichucounter package me.zobrist.tichucounter
import me.zobrist.tichucounter.domain.Tichu import me.zobrist.tichucounter.domain.Tichu
import org.junit.Assert.* import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
/** /**

View File

@@ -1,15 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext { ext.kotlin_version = "1.8.21"
compose_version = '1.1.1'
}
ext.kotlin_version = "1.7.20"
repositories { repositories {
google() google()
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.4.0' classpath 'com.android.tools.build:gradle:8.0.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

View File

@@ -19,3 +19,6 @@ android.useAndroidX=true
android.enableJetifier=false android.enableJetifier=false
# Kotlin code style for this project: "official" or "obsolete": # Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official kotlin.code.style=official
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=true
android.nonFinalResIds=false

View File

@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip