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
1 change: 1 addition & 0 deletions .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.

42 changes: 30 additions & 12 deletions app/src/main/java/edu/temple/dicethrow/DieFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,57 @@ import kotlin.random.Random
class DieFragment : Fragment() {

val DIESIDE = "sidenumber"

val PREV = "previousroll"
var current = 0
lateinit var dieTextView: TextView

var dieSides: Int = 6

// Factory method to create an instance of DieFragment with a specific number of sides
companion object {
fun newInstance(dieSides: Int): DieFragment {
val fragment = DieFragment()
val args = Bundle()
args.putInt(fragment.DIESIDE, dieSides) // Use the constant DIESIDE correctly
fragment.arguments = args
return fragment
}
}

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

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_die, container, false).apply {
dieTextView = findViewById(R.id.dieTextView)
}
val view = inflater.inflate(R.layout.fragment_die, container, false)
dieTextView = view.findViewById(R.id.dieTextView)
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
throwDie()
view.setOnClickListener{
savedInstanceState?.getInt(PREV)?.let {
current = it
dieTextView.text = current.toString()
}
view.setOnClickListener {
throwDie()
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(PREV, current)
}

fun throwDie() {
dieTextView.text = Random.nextInt(dieSides).toString()
current = Random.nextInt(dieSides) + 1
dieTextView.text = current.toString()
}
}
}
28 changes: 24 additions & 4 deletions app/src/main/java/edu/temple/dicethrow/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package edu.temple.dicethrow

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

class MainActivity : AppCompatActivity() {

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

// Create the first die with 6 sides
val dieFragment1 = DieFragment.newInstance(20)
val fragmentTransaction1: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction1.replace(R.id.dieFragmentContainer1, dieFragment1)
fragmentTransaction1.commit()

// Create the second die with 6 sides
val dieFragment2 = DieFragment.newInstance(20)
val fragmentTransaction2: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction2.replace(R.id.dieFragmentContainer2, dieFragment2)
fragmentTransaction2.commit()

// Set up the roll button to roll both dice
val rollButton = findViewById<Button>(R.id.rollButton)
rollButton.setOnClickListener {
dieFragment1.throwDie() // Roll the first die
dieFragment2.throwDie() // Roll the second die
}
}
}
}

34 changes: 23 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- First Die FragmentContainerView -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:id="@+id/dieFragmentContainer1"
android:name="edu.temple.dicethrow.DieFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<!-- Second Die FragmentContainerView -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/dieFragmentContainer2"
android:name="edu.temple.dicethrow.DieFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"/>

<!-- Roll Button -->
<Button
android:id="@+id/rollDiceButton"
android:id="@+id/rollButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLL"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Roll"
app:layout_constraintTop_toBottomOf="@id/dieFragmentContainer2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_marginTop="32dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
18 changes: 9 additions & 9 deletions app/src/main/res/layout/fragment_die.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DieFragment">
android:orientation="vertical"
android:gravity="center">

<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/dieTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="96sp" />
android:textSize="48sp"
android:textAlignment="center"
android:layout_marginTop="32dp"/>

</FrameLayout>
</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="dieFragmentContainer1" type="id" />
<item name="rollButton" type="id" />
<item name="dieFragmentContainer2" type="id" />
</resources>