Skip to content

Commit

Permalink
Add source
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Feb 20, 2024
1 parent c75697c commit ad99240
Show file tree
Hide file tree
Showing 27 changed files with 586 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "net.leodesouza.blitz"
compileSdk = 34

defaultConfig {
applicationId = "net.leodesouza.blitz"
minSdk = 23
targetSdk = 34
versionCode = 100
versionName = "1.0.0"
}

buildTypes {
release {
isMinifyEnabled = true
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
debugImplementation("androidx.compose.ui:ui-tooling")
}
19 changes: 19 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:icon="@mipmap/ic_launcher"
android:label="Blitz"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/Theme.Blitz"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
148 changes: 148 additions & 0 deletions app/src/main/java/net/leodesouza/blitz/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package net.leodesouza.blitz

import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.os.SystemClock.elapsedRealtime
import android.text.format.DateUtils.formatElapsedTime
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
super.onCreate(savedInstanceState)
setContent {
CountDown(303_000L, 3_000L) { whiteTime, blackTime, onClick ->
ChessClock(whiteTime, blackTime, onClick)
}
}
}
}

@Composable
fun CountDown(
duration: Long,
increment: Long,
content: @Composable (whiteTime: Long, blackTime: Long, onClick: () -> Unit) -> Unit
) {
var whiteTime by remember { mutableLongStateOf(duration) }
var blackTime by remember { mutableLongStateOf(duration) }
var endTime by remember { mutableLongStateOf(elapsedRealtime() + duration) }
var timesPressed by remember { mutableIntStateOf(0) }
var isPressed by remember { mutableStateOf(false) }
var isRunning by remember { mutableStateOf(false) }
val onClick = {
timesPressed++
isPressed = true
}
content.invoke(whiteTime, blackTime, onClick)
BackHandler(isRunning) {
timesPressed--
isRunning = false
}
LaunchedEffect(whiteTime, blackTime, isPressed) {
val isBlackTurn = (timesPressed % 2 == 0)
if (isPressed) {
if (isRunning) {
if (isBlackTurn) {
whiteTime += increment
} else {
blackTime += increment
}
} else {
isRunning = true
}
endTime = elapsedRealtime() + if (isBlackTurn) blackTime else whiteTime
isPressed = false
}
if (isRunning) {
val remainingTime = if (elapsedRealtime() < endTime) {
delay((endTime - elapsedRealtime()) % 100L)
endTime - elapsedRealtime()
} else {
0L
}
if (isBlackTurn) blackTime = remainingTime
else whiteTime = remainingTime
}
}
}

@Composable
fun ChessClock(whiteTime: Long, blackTime: Long, onClick: () -> Unit) {
Surface(onClick) {
Row {
Time(
whiteTime,
color = if (whiteTime == 0L) Color.Red else Color.Black,
modifier = Modifier
.background(Color.White)
.weight(1F)
.fillMaxSize()
.wrapContentSize()
)
Time(
blackTime,
color = if (blackTime == 0L) Color.Red else Color.White,
modifier = Modifier
.background(Color.Black)
.weight(1F)
.fillMaxSize()
.wrapContentSize()
)
}
}
}

@Composable
fun Time(timeMillis: Long, color: Color, modifier: Modifier = Modifier) {
val roundedTime = (timeMillis + 50L) / 100L
val integerPart = formatElapsedTime(roundedTime / 10L)
val decimalPart = roundedTime % 10L
Text(
text = "$integerPart.$decimalPart",
modifier = modifier,
color = color,
fontSize = with(LocalDensity.current) {
(LocalConfiguration.current.screenWidthDp / 8).dp.toSp()
},
)
}

@Preview
@Composable
fun ChessClockPreview() {
CountDown(13_000L, 3_000L) { whiteTime, blackTime, onClick ->
ChessClock(whiteTime, blackTime, onClick)
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="#000000">
<group android:scaleX="0.58"
android:scaleY="0.58"
android:translateX="201.6"
android:translateY="201.6">
<path
android:fillColor="@android:color/white"
android:pathData="M480,840Q406,840 340.5,811.5Q275,783 226,734Q177,685 148.5,619.5Q120,554 120,480Q120,402 150,334Q180,266 234,217Q246,206 262.5,206.5Q279,207 290,218L508,436Q519,447 519,464Q519,481 508,492Q497,503 480,503Q463,503 452,492L264,304Q234,340 217,384.5Q200,429 200,480Q200,596 282,678Q364,760 480,760Q596,760 678,678Q760,596 760,480Q760,373 691.5,295.5Q623,218 520,204L520,240Q520,257 508.5,268.5Q497,280 480,280Q463,280 451.5,268.5Q440,257 440,240L440,160Q440,143 451.5,131.5Q463,120 480,120Q554,120 619.5,148.5Q685,177 734,226Q783,275 811.5,340.5Q840,406 840,480Q840,554 811.5,619.5Q783,685 734,734Q685,783 619.5,811.5Q554,840 480,840ZM280,520Q263,520 251.5,508.5Q240,497 240,480Q240,463 251.5,451.5Q263,440 280,440Q297,440 308.5,451.5Q320,463 320,480Q320,497 308.5,508.5Q297,520 280,520ZM480,720Q463,720 451.5,708.5Q440,697 440,680Q440,663 451.5,651.5Q463,640 480,640Q497,640 508.5,651.5Q520,663 520,680Q520,697 508.5,708.5Q497,720 480,720ZM680,520Q663,520 651.5,508.5Q640,497 640,480Q640,463 651.5,451.5Q663,440 680,440Q697,440 708.5,451.5Q720,463 720,480Q720,497 708.5,508.5Q697,520 680,520Z"/>
</group>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>
Binary file added app/src/main/res/mipmap-hdpi/ic_launcher.webp
Binary file not shown.
Binary file not shown.
Binary file added app/src/main/res/mipmap-mdpi/ic_launcher.webp
Binary file not shown.
Binary file not shown.
Binary file added app/src/main/res/mipmap-xhdpi/ic_launcher.webp
Binary file not shown.
Binary file not shown.
Binary file added app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
Binary file not shown.
Binary file not shown.
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions app/src/main/res/values/ic_launcher_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#FFFFFF</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Blitz" parent="android:Theme.Material.Light.NoActionBar" />
</resources>
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
}
23 changes: 23 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Feb 20 11:40:55 CET 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit ad99240

Please sign in to comment.