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
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.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.fragment)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
41 changes: 29 additions & 12 deletions app/src/main/java/edu/temple/dicethrow/DieFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,57 @@ import kotlin.random.Random

class DieFragment : Fragment() {

val DIESIDE = "sidenumber"
private val ROLL_KEY = "current_roll"
private lateinit var dieTextView: TextView

lateinit var dieTextView: TextView
private var currentRoll = 1
private var dieSides: Int = 6

var dieSides: Int = 6
companion object {
const val DIESIDE = "sidenumber"

fun newInstance(sides: Int = 6): DieFragment {
return DieFragment().apply {
arguments = Bundle().apply {
putInt(DIESIDE, sides)
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

arguments?.let {
it.getInt(DIESIDE).run {
dieSides = this
}
dieSides = it.getInt(DIESIDE)
}

savedInstanceState?.let {
currentRoll = it.getInt(ROLL_KEY, 1)
}
}

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)
}
}

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(1, dieSides + 1)
dieTextView.text = currentRoll.toString()
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/edu/temple/dicethrow/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.temple.dicethrow

import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
Expand All @@ -10,5 +11,25 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

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)
rollbutton.setOnClickListener {
val dieFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView)
as? DieFragment
dieFragment?.throwDie()

val dieFragment2 = supportFragmentManager.findFragmentById(R.id.fragmentContainerView2)
as? DieFragment
dieFragment2?.throwDie()
}
}
}
13 changes: 13 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,17 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="edu.temple.dicethrow.DieFragment"
android:layout_width="411dp"
android:layout_height="303dp"
android:layout_marginTop="35dp"
android:layout_marginBottom="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rollDiceButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ appcompat = "1.6.1"
material = "1.10.0"
activity = "1.9.2"
constraintlayout = "2.1.4"
fragment = "1.8.9"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand All @@ -19,6 +20,7 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
androidx-fragment = { group = "androidx.fragment", name = "fragment", version.ref = "fragment" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down