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

2 changes: 1 addition & 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.

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

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button

class ButtonFragment : Fragment() {

interface ButtonOnclickListener {
fun onClick()
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_button, container, false).apply {
if(context is ButtonOnclickListener)
this.findViewById<Button>(R.id.rollDiceButton).setOnClickListener {
(context as ButtonOnclickListener).onClick()
}
}
}
}
34 changes: 27 additions & 7 deletions app/src/main/java/edu/temple/dicethrow/DieFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import kotlin.random.Random
class DieFragment : Fragment() {

val DIESIDE = "sidenumber"
val DIEVALUE = "dievalue"

lateinit var dieTextView: TextView

var dieSides: Int = 6
var dieValue: Int = 0

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

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

if(savedInstanceState != null)
dieValue = savedInstanceState.getInt(DIEVALUE)
}

override fun onCreateView(
Expand All @@ -35,15 +39,31 @@ class DieFragment : Fragment() {
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(DIEVALUE, dieValue)
}

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

fun throwDie() {
dieTextView.text = Random.nextInt(dieSides).toString()
dieValue = Random.nextInt(dieSides).plus(1)
dieTextView.text = dieValue.toString()
}

companion object {
@JvmStatic
fun newInstance(dieSides: Int) =
DieFragment().apply {
arguments = Bundle().apply {
putInt(DIESIDE, dieSides)
}
}
}
}
17 changes: 13 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,23 @@
package edu.temple.dicethrow

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

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

if(supportFragmentManager.findFragmentById(R.id.dieContainer) !is DieFragment)
supportFragmentManager.beginTransaction()
.replace(R.id.dieContainer, DieFragment.newInstance(10))
.commit()

}

override fun onClick() {
(supportFragmentManager.findFragmentById(R.id.dieContainer) as DieFragment).throwDie()
}
}
17 changes: 9 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="edu.temple.dicethrow.DieFragment"
android:id="@+id/dieContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/rollDiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLL"
<androidx.fragment.app.FragmentContainerView
android:id="@+id/buttonContainer"
android:name="edu.temple.dicethrow.ButtonFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/dieContainer" />

</androidx.constraintlayout.widget.ConstraintLayout>
20 changes: 20 additions & 0 deletions app/src/main/res/layout/fragment_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonFragment">

<Button
android:id="@+id/rollDiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.6.1"
material = "1.10.0"
activity = "1.9.2"
appcompat = "1.7.0"
material = "1.12.0"
activity = "1.9.3"
constraintlayout = "2.1.4"

[libraries]
Expand Down