Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/AndroidProjectSystem.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/deviceManager.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/src/main/java/edu/temple/dicethrow/BaseBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.temple.dicethrow;

public class BaseBundle {
}
35 changes: 25 additions & 10 deletions app/src/main/java/edu/temple/dicethrow/DieFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import kotlin.random.Random

class DieFragment : Fragment() {

val DIESIDE = "sidenumber"

lateinit var dieTextView: TextView

var currentRoll = 1
var dieSides: Int = 6

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
it.getInt(DIESIDE).run {
dieSides = this
}
dieSides = it.getInt(DIESIDE, 6)
}

savedInstanceState?.getInt(ROLL_KEY)?.run {
currentRoll = this
}
}

Expand All @@ -37,13 +38,27 @@ class DieFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
throwDie()
view.setOnClickListener{
throwDie()
}
dieTextView.text = currentRoll.toString()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(ROLL_KEY, currentRoll)
}

fun throwDie() {
dieTextView.text = Random.nextInt(dieSides).toString()
currentRoll = Random.nextInt(dieSides) + 1
dieTextView.text = currentRoll.toString()
}

companion object {
private const val DIESIDE = "sidenumber"
private const val ROLL_KEY = "current_role"

fun newInstance(sides: Int) = DieFragment().apply {
arguments = Bundle().apply {
putInt(DIESIDE, sides)
}
}
}
}
20 changes: 17 additions & 3 deletions app/src/main/java/edu/temple/dicethrow/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package edu.temple.dicethrow

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Add fragments only the first time the activity is created
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainerView, DieFragment.newInstance(6))
.replace(R.id.fragmentContainerView2, DieFragment.newInstance(10))
.commit()
}

val rollButton = findViewById<Button>(R.id.rollDiceButton)
// Set a click listener for the button
rollButton.setOnClickListener {
(supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as? DieFragment)?.throwDie()
(supportFragmentManager.findFragmentById(R.id.fragmentContainerView2) as? DieFragment)?.throwDie()
}
}
}
24 changes: 24 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,28 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<FrameLayout
android:id="@+id/fragmentContainerView1"
android:layout_width="200dp"
android:layout_height="200dp" />

<FrameLayout
android:id="@+id/fragmentContainerView2"
android:layout_width="200dp"
android:layout_height="200dp" />

<Button
android:id="@+id/rollDiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Dice" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>