-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
284 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
sample/src/main/java/dev/arkbuilders/sample/ScoreActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package dev.arkbuilders.sample | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.button.MaterialButton | ||
import dev.arkbuilders.arklib.data.folders.FoldersRepo | ||
import dev.arkbuilders.arklib.data.index.ResourceIndex | ||
import dev.arkbuilders.arklib.data.index.ResourceIndexRepo | ||
import dev.arkbuilders.arklib.user.score.ScoreStorage | ||
import dev.arkbuilders.arklib.user.score.ScoreStorageRepo | ||
import dev.arkbuilders.components.filepicker.ArkFilePickerConfig | ||
import dev.arkbuilders.components.filepicker.ArkFilePickerFragment | ||
import dev.arkbuilders.components.filepicker.ArkFilePickerMode | ||
import dev.arkbuilders.components.filepicker.onArkPathPicked | ||
import dev.arkbuilders.components.scorewidget.HorizontalScoreWidgetComposable | ||
import dev.arkbuilders.components.scorewidget.ScoreWidgetController | ||
import dev.arkbuilders.components.scorewidget.VerticalScoreWidgetComposable | ||
import kotlinx.coroutines.launch | ||
import java.nio.file.Path | ||
import kotlin.io.path.name | ||
|
||
class ScoreActivity : AppCompatActivity() { | ||
private var rootFolder: Path? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_score) | ||
|
||
val btnPickRoot = findViewById<MaterialButton>(R.id.btn_pick_root) | ||
val btnPickResource = findViewById<MaterialButton>(R.id.btn_pick_resource) | ||
|
||
supportFragmentManager.onArkPathPicked( | ||
this, | ||
customRequestKey = PICK_ROOT_KEY | ||
) { root -> | ||
rootFolder = root | ||
btnPickRoot.text = root.name | ||
} | ||
|
||
supportFragmentManager.onArkPathPicked( | ||
this, | ||
customRequestKey = PICK_RESOURCE_KEY | ||
) { resourcePath -> | ||
btnPickResource.text = resourcePath.name | ||
rootFolder?.let { | ||
onResourcePicked(root = it, resourcePath) | ||
} | ||
} | ||
|
||
btnPickRoot.setOnClickListener { | ||
ArkFilePickerFragment | ||
.newInstance(ArkFilePickerConfig(pathPickedRequestKey = PICK_ROOT_KEY)) | ||
.show(supportFragmentManager, null) | ||
} | ||
|
||
btnPickResource.setOnClickListener { | ||
ArkFilePickerFragment | ||
.newInstance( | ||
ArkFilePickerConfig( | ||
pathPickedRequestKey = PICK_RESOURCE_KEY, | ||
mode = ArkFilePickerMode.FILE | ||
) | ||
).show(supportFragmentManager, null) | ||
} | ||
} | ||
|
||
private fun onResourcePicked( | ||
root: Path, | ||
resourcePath: Path | ||
) = lifecycleScope.launch { | ||
val (index, scoreStorage) = setupIndexAndScoreStorage(root) | ||
val id = index.allPaths().toList() | ||
.find { it.second == resourcePath }!!.first | ||
val scoreWidgetController = ScoreWidgetController( | ||
lifecycleScope, | ||
getCurrentId = { id }, | ||
onScoreChanged = {} | ||
) | ||
scoreWidgetController.init(scoreStorage) | ||
|
||
findViewById<ComposeView>(R.id.score_widget_horizontal).setContent { | ||
HorizontalScoreWidgetComposable( | ||
size = DpSize(200.dp, 80.dp), | ||
controller = scoreWidgetController | ||
) | ||
} | ||
|
||
findViewById<ComposeView>(R.id.score_widget_vertical).setContent { | ||
VerticalScoreWidgetComposable( | ||
modifier = Modifier.padding(40.dp), | ||
size = DpSize(50.dp, 120.dp), | ||
controller = scoreWidgetController | ||
) | ||
} | ||
|
||
scoreWidgetController.setVisible(true) | ||
scoreWidgetController.displayScore() | ||
} | ||
|
||
private suspend fun setupIndexAndScoreStorage( | ||
root: Path | ||
): Pair<ResourceIndex, ScoreStorage> { | ||
val foldersRepo = FoldersRepo(applicationContext) | ||
val index = ResourceIndexRepo(foldersRepo).provide(root) | ||
val scoreStorage = ScoreStorageRepo(lifecycleScope).provide(index) | ||
return index to scoreStorage | ||
} | ||
|
||
companion object { | ||
private val PICK_ROOT_KEY = "pickRootKey" | ||
private val PICK_RESOURCE_KEY = "pickResourceKey" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.material.button.MaterialButton | ||
android:id="@+id/btn_pick_root" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="12dp" | ||
android:text="Pick root" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<com.google.android.material.button.MaterialButton | ||
android:id="@+id/btn_pick_resource" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="12dp" | ||
android:text="Pick resource" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/btn_pick_root" /> | ||
|
||
<androidx.compose.ui.platform.ComposeView | ||
android:id="@+id/score_widget_horizontal" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="50dp" | ||
android:padding="50dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/btn_pick_resource" /> | ||
|
||
<androidx.compose.ui.platform.ComposeView | ||
android:id="@+id/score_widget_vertical" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/score_widget_horizontal" /> | ||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
scorewidget/src/main/java/dev/arkbuilders/components/scorewidget/ScoreWidget.kt
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
scorewidget/src/main/java/dev/arkbuilders/components/scorewidget/ScoreWidgetComposable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dev.arkbuilders.components.scorewidget | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.dp | ||
import com.mdrlzy.counterslider.HorizontalCounterSlider | ||
import com.mdrlzy.counterslider.VerticalCounterSlider | ||
import org.orbitmvi.orbit.compose.collectAsState | ||
|
||
|
||
@Composable | ||
fun HorizontalScoreWidgetComposable( | ||
modifier: Modifier = Modifier, | ||
size: DpSize = DpSize(200.dp, 80.dp), | ||
allowTopToReset: Boolean = true, | ||
allowBottomToReset: Boolean = true, | ||
controller: ScoreWidgetController, | ||
) { | ||
val state by controller.collectAsState() | ||
if (state.visible.not()) | ||
return | ||
HorizontalCounterSlider( | ||
modifier = modifier, | ||
size = size, | ||
value = state.score.toString(), | ||
allowTopToReset = allowTopToReset, | ||
allowBottomToReset = allowBottomToReset, | ||
onValueIncreaseClick = { controller.onIncrease() }, | ||
onValueDecreaseClick = { controller.onDecrease() }, | ||
onValueClearClick = { controller.onReset() } | ||
) | ||
} | ||
|
||
@Composable | ||
fun VerticalScoreWidgetComposable( | ||
modifier: Modifier = Modifier, | ||
size: DpSize = DpSize(80.dp, 200.dp), | ||
allowLeftToReset: Boolean = true, | ||
allowRightToReset: Boolean = true, | ||
controller: ScoreWidgetController, | ||
) { | ||
val state by controller.collectAsState() | ||
if (state.visible.not()) | ||
return | ||
VerticalCounterSlider( | ||
modifier = modifier, | ||
size = size, | ||
value = state.score.toString(), | ||
allowLeftToReset = allowLeftToReset, | ||
allowRightToReset = allowRightToReset, | ||
onValueIncreaseClick = { controller.onIncrease() }, | ||
onValueDecreaseClick = { controller.onDecrease() }, | ||
onValueClearClick = { controller.onReset() } | ||
) | ||
} |
Oops, something went wrong.