Skip to content

Commit

Permalink
Add WheelActivity and the the wheel to playing room
Browse files Browse the repository at this point in the history
  • Loading branch information
tungnk123 committed Apr 8, 2024
1 parent 9dde362 commit bf0bbaa
Show file tree
Hide file tree
Showing 18 changed files with 371 additions and 49 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

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

5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugins {
alias(libs.plugins.jetbrainsKotlinAndroid)
id("com.google.gms.google-services")
}

android {
namespace = "com.example.chiecnonkydieu"
compileSdk = 34
Expand Down Expand Up @@ -54,4 +53,8 @@ dependencies {

//coroutine
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")

//wheel
implementation("androidx.navigation:navigation-dynamic-features-fragment:2.7.7")
implementation ("com.github.thanhniencung:LuckyWheel:a6110f5128")
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
android:theme="@style/Theme.ChiecNonKyDieu"
tools:targetApi="31">
<activity
android:name=".ui.WheelActivity"
android:name=".ui.wheel.WheelActivity"
android:exported="false" />
<activity
android:name=".ui.PlayingRoomActivity"
android:launchMode="singleTop"
android:exported="false" />
<activity
android:name=".ui.SearchRoomActivity"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import com.example.chiecnonkydieu.databinding.ActivityMainBinding
import com.example.chiecnonkydieu.ui.CreateRoomActivity
import com.example.chiecnonkydieu.ui.PlayingRoomActivity
import com.example.chiecnonkydieu.ui.SearchRoomActivity
import com.example.chiecnonkydieu.ui.WaitingRoomActivity
import com.google.firebase.Firebase
import com.google.firebase.database.database
import com.example.chiecnonkydieu.ui.wheel.WheelActivity

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
Expand All @@ -37,7 +35,9 @@ class MainActivity : AppCompatActivity() {
// val intent = Intent(this, WaitingRoomActivity::class.java)
// startActivity(intent)
binding.btnBatDau.setOnClickListener {
goToPlayingRoom()
// goToPlayingRoom()
val intent = Intent(this, WheelActivity::class.java)
startActivity(intent)
}

binding.btnTaoPhong.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.chiecnonkydieu.data

import rubikstudio.library.model.LuckyItem


Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.example.chiecnonkydieu.ui

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
Expand All @@ -21,7 +24,10 @@ import com.example.chiecnonkydieu.data.GameData
import com.example.chiecnonkydieu.data.GameModel
import com.example.chiecnonkydieu.data.GameStatus
import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding
import com.example.chiecnonkydieu.ui.wheel.WheelActivity
import com.example.chiecnonkydieu.ui.wheel.WheelViewModel
import kotlinx.coroutines.launch
import rubikstudio.library.LuckyWheelView


class PlayingRoomActivity : AppCompatActivity() {
Expand All @@ -39,6 +45,14 @@ class PlayingRoomActivity : AppCompatActivity() {
insets
}


val toolbar: Toolbar = binding.toolbar
setSupportActionBar(toolbar)

supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)


lifecycleScope.launch {
GameData.fetchGameModel(intent.getStringExtra("room_id").toString().toInt())

Expand All @@ -63,6 +77,31 @@ class PlayingRoomActivity : AppCompatActivity() {
binding.iconButton.setOnClickListener {
Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show()
}

// Wheel
val wheelViewModel: WheelViewModel by viewModels<WheelViewModel>()
val luckyWheelView: LuckyWheelView = binding.luckyWheel

wheelViewModel.initLuckyItemList(this)
luckyWheelView.setData(wheelViewModel.luckyItemsList)
luckyWheelView.setLuckyRoundItemSelectedListener {
Toast.makeText(this, "on click", Toast.LENGTH_LONG).show()
goToWheelActivity()
}
luckyWheelView.isTouchEnabled = false
binding.llWheel.setOnClickListener {
goToWheelActivity()
}


}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
private fun goToWheelActivity() {
val intent = Intent(this, WheelActivity::class.java)
startActivity(intent)
}

private fun updateUi(gameModel: GameModel?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class WaitingRoomActivity : AppCompatActivity() {
updateUi(gameModel)
if (gameModel.gameStatus == GameStatus.INPROGRESS) {
goToPlayingRoom()
finish()
}
})

Expand Down Expand Up @@ -148,8 +149,10 @@ class WaitingRoomActivity : AppCompatActivity() {
}

private fun goToPlayingRoom() {
val intent = Intent(this, PlayingRoomActivity::class.java)
intent.putExtra("room_id", binding.tvMaPhong.text.toString())
val intent = Intent(this, PlayingRoomActivity::class.java).apply {
putExtra("room_id", binding.tvMaPhong.text.toString())
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
}
startActivity(intent)
}
}
36 changes: 0 additions & 36 deletions app/src/main/java/com/example/chiecnonkydieu/ui/WheelActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.example.chiecnonkydieu.ui.wheel

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.get
import com.example.chiecnonkydieu.R
import com.example.chiecnonkydieu.databinding.ActivityWheelBinding
import rubikstudio.library.LuckyWheelView
import rubikstudio.library.model.LuckyItem

class WheelActivity : AppCompatActivity() {
private lateinit var binding: ActivityWheelBinding
private lateinit var countDownTimer: CountDownTimer

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityWheelBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val toolbar: Toolbar = binding.toolbar
setSupportActionBar(toolbar)

supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)

val wheelViewModel: WheelViewModel by viewModels<WheelViewModel>()
val luckyWheelView: LuckyWheelView = binding.luckyWheel

wheelViewModel.initLuckyItemList(this)
luckyWheelView.setData(wheelViewModel.luckyItemsList)

luckyWheelView.isTouchEnabled = false
luckyWheelView.setOnTouchListener { v, event ->
val indexAns = wheelViewModel.getIndexAfterRotate()
luckyWheelView.setRound(3)
luckyWheelView.startLuckyWheelWithTargetIndex(indexAns)
Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show()
return@setOnTouchListener true
}


binding.btnQuay.setOnClickListener {
val indexAns = wheelViewModel.getIndexAfterRotate()
luckyWheelView.setRound(3)
luckyWheelView.startLuckyWheelWithTargetIndex(indexAns)
Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show()
// Toast.makeText(this, wheelViewModel.luckyItemsList.get(3).secondaryText, Toast.LENGTH_LONG).show()
}

// count down timer
countDownTimer = object : CountDownTimer(7000, 1000) {
override fun onTick(millisUntilFinished: Long) {
val secondsLeft = millisUntilFinished / 1000
binding.tvConLaiXGiay.text = String.format(getString(R.string.con_lai_x_giay), secondsLeft.toString())
}

override fun onFinish() {
binding.tvConLaiXGiay.text = String.format(getString(R.string.con_lai_x_giay), 0)
binding.btnQuay.performClick()

}
}

countDownTimer.start()

}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

override fun onDestroy() {
super.onDestroy()
countDownTimer.cancel()
}
}
Loading

0 comments on commit bf0bbaa

Please sign in to comment.