Skip to content

Commit

Permalink
#4: Selecting StartScore/ Sets & Legs
Browse files Browse the repository at this point in the history
  • Loading branch information
Entreco committed Dec 28, 2017
1 parent 4cf2662 commit 69fb4ca
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package nl.entreco.dartsscorecard.setup

import android.content.Context
import android.databinding.ObservableInt
import android.widget.AdapterView
import android.widget.SeekBar
import nl.entreco.dartsscorecard.base.BaseViewModel
import nl.entreco.dartsscorecard.play.Play01Activity
import nl.entreco.domain.Logger
import nl.entreco.domain.launch.TeamNamesString
import nl.entreco.domain.launch.usecase.CreateGameUsecase
import nl.entreco.domain.launch.usecase.ExtractTeamsUsecase
Expand All @@ -16,11 +18,13 @@ import javax.inject.Inject
/**
* Created by Entreco on 20/12/2017.
*/
class Setup01ViewModel @Inject constructor(private val createGameUsecase: CreateGameUsecase, private val extractTeamsUsecase: ExtractTeamsUsecase) : BaseViewModel() {
class Setup01ViewModel @Inject constructor(private val createGameUsecase: CreateGameUsecase, private val extractTeamsUsecase: ExtractTeamsUsecase, private val logger: Logger) : BaseViewModel() {

private val min = 0
val max = 20
val startScore = ObservableInt(501)
val numSets = ObservableInt(1)
val numLegs = ObservableInt(3)
val numSets = ObservableInt(0)
val numLegs = ObservableInt(0)

fun onStartPressed(context: Context) {
val setup = CreateGameRequest(startScore.get(), 0, numLegs.get(), numSets.get())
Expand All @@ -31,14 +35,37 @@ class Setup01ViewModel @Inject constructor(private val createGameUsecase: Create
}, onGameCreatedFailed())
}

fun onStartScoreSelected(adapter: AdapterView<*>, index: Int) {
val selectedString = adapter.adapter.getItem(index) as? String
startScore.set(selectedString?.toInt()!!)
}

fun onSetsChanged(seekBar: SeekBar, delta: Int) {
seekBar.progress += delta
}

fun onLegsChanged(seekBar: SeekBar, delta: Int) {
seekBar.progress += delta
}

fun onSetsProgressChanged(sets: Int) {
if (sets in min..max) {
numSets.set(sets + 1)
}
}

fun onLegsProgressChanged(legs: Int) {
if (legs in min..max) {
numLegs.set(legs + 1)
}
}

private fun onGameCreated(context: Context): (RetrieveGameRequest) -> Unit =
{ Play01Activity.startGame(context, it) }
{ req -> Play01Activity.startGame(context, req) }

private fun onGameCreatedFailed(): (Throwable) -> Unit = {}
private fun onGameCreatedFailed(): (Throwable) -> Unit = { err ->
logger.w("Unable to create game $err")
}

private fun ensureTeamPlayersExist(teamNamesInput: TeamNamesString, done: (TeamIdsString) -> Unit, fail: (Throwable) -> Unit) {
extractTeamsUsecase.exec(teamNamesInput, done, fail)
Expand Down
106 changes: 55 additions & 51 deletions android/DartsScorecard/app/src/main/res/layout/activity_setup_01.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/def">
android:paddingBottom="@dimen/def"
android:paddingTop="@dimen/def">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -29,36 +30,37 @@
android:padding="@dimen/xsmall"
android:text="@string/select_x01_score" />


<LinearLayout
<RelativeLayout
style="@style/Setup.Entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/xsmall">
android:layout_height="wrap_content">

<TextView
style="@style/Setup.Entry.Txt"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="start score:" />

<TextView
style="@style/Setup.Entry.Txt"
style="@style/Setup.Entry.Value"
android:layout_width="@dimen/setup_width"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="@{@string/number(viewModel.startScore)}" />
</LinearLayout>

<android.support.v7.widget.AppCompatSpinner
style="@style/Setup.Entry.Spinner"
android:layout_width="@dimen/setup_width"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:alpha="0"
android:entries="@array/x01"
android:onItemSelected="@{(adapter,i,index,l) -> viewModel.onStartScoreSelected(adapter,index)}" />
</RelativeLayout>

<!-- Footer -->
<TextView
style="@style/Setup.Ftr"
android:layout_width="match_parent"
android:layout_height="@dimen/def"
android:background="@drawable/score_footer"
android:padding="@dimen/xsmall" />
<TextView style="@style/Setup.Ftr" />

<TextView
style="@style/Setup.Hdr"
Expand All @@ -69,21 +71,27 @@
android:padding="@dimen/xsmall"
android:text="@string/sponsored_by" />

<com.google.android.gms.ads.AdView
style="@style/Setup.Entry"
<!-- Ad -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/demo_ad_unit_id"
app:loadAdd="@{true}" />
android:layout_height="wrap_content">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

<com.google.android.gms.ads.AdView
style="@style/Setup.Ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/real_ad_unit_id"
app:loadAdd="@{true}" />
</FrameLayout>

<!-- Footer -->
<TextView
style="@style/Setup.Ftr"
android:layout_width="match_parent"
android:layout_height="@dimen/large"
android:background="@drawable/score_footer"
android:padding="@dimen/xsmall" />
<TextView style="@style/Setup.Ftr" />

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -101,7 +109,7 @@
android:text="@string/number_of_sets" />

<TextView
style="@style/Score.Hdr"
style="@style/Setup.Hdr.End"
android:layout_width="@dimen/setup_width"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -113,8 +121,7 @@
style="@style/Setup.Entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/xsmall">
android:orientation="horizontal">

<TextView
style="@style/Setup.Entry.Txt"
Expand All @@ -124,14 +131,14 @@
android:text="@string/minus" />

<SeekBar
style="@style/Setup.Progress"
android:id="@+id/setup_sets"
style="@style/Setup.Progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="21"
android:min="1"
android:progress="1" />
android:max="@{viewModel.max}"
android:onProgressChanged="@{(view, sets, user) -> viewModel.onSetsProgressChanged(sets)}"
android:progress="0" />

<TextView
style="@style/Setup.Entry.Txt"
Expand All @@ -141,16 +148,17 @@
android:text="@string/plus" />

<TextView
style="@style/Setup.Entry.Txt"
style="@style/Setup.Entry.Value"
android:layout_width="@dimen/setup_width"
android:layout_height="wrap_content"
android:background="@drawable/score_pts"
android:text="@{@string/number(viewModel.numSets)}" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/score_pts"
android:background="@drawable/score_header"
android:orientation="horizontal">

<TextView
Expand All @@ -163,7 +171,7 @@
android:text="@string/number_of_legs" />

<TextView
style="@style/Score.Hdr"
style="@style/Setup.Hdr.End"
android:layout_width="@dimen/setup_width"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -175,45 +183,42 @@
style="@style/Setup.Entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/xsmall">
android:orientation="horizontal">

<TextView
style="@style/Setup.Entry.Txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.onLegsChanged(setupLegs, -1)}"
android:text="@string/minus" />


<SeekBar
android:id="@+id/setup_legs"
style="@style/Setup.Progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="21"
android:min="1"
android:progress="3" />
android:max="@{viewModel.max}"
android:onProgressChanged="@{(view, legs, user) -> viewModel.onLegsProgressChanged(legs)}"
android:progress="2" />

<TextView
style="@style/Setup.Entry.Txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.onLegsChanged(setupLegs, +1)}"
android:text="@string/plus" />

<TextView
style="@style/Setup.Entry.Txt"
style="@style/Setup.Entry.Value"
android:layout_width="@dimen/setup_width"
android:layout_height="wrap_content"
android:text="@{@string/number(viewModel.numLegs)}" />
</LinearLayout>

<!-- Footer -->
<TextView
style="@style/Setup.Ftr"
android:layout_width="match_parent"
android:layout_height="@dimen/def"
android:background="@drawable/score_footer"
android:padding="@dimen/xsmall" />
<TextView style="@style/Setup.Ftr" />

<Button
style="@style/Setup.Btn"
Expand All @@ -223,6 +228,5 @@
android:text="@string/cont" />

</LinearLayout>

</ScrollView>
</layout>
13 changes: 13 additions & 0 deletions android/DartsScorecard/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
<string name="undo">Undo</string>
<string name="score_180">180</string>

<string-array name="x01">
<item>101</item>
<item>201</item>
<item>301</item>
<item>401</item>
<item>501</item>
<item>601</item>
<item>701</item>
<item>801</item>
<item>901</item>
<item>1001</item>
</string-array>

<!-- Setup -->
<string name="select_x01_score">Select x01 score</string>
<string name="sponsored_by">Sponsored by</string>
Expand Down
Loading

0 comments on commit 69fb4ca

Please sign in to comment.