Skip to content

Commit

Permalink
game refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VolgoAK committed Aug 20, 2019
1 parent 59bfa30 commit ed1ba70
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,40 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.sergey.shlypa2.OnSwipeTouchListener
import com.example.sergey.shlypa2.R
import com.example.sergey.shlypa2.beans.Word
import com.example.sergey.shlypa2.extensions.dpToPx
import com.example.sergey.shlypa2.extensions.hide
import com.example.sergey.shlypa2.extensions.onTransitionCompletedOnce
import com.example.sergey.shlypa2.extensions.show
import com.example.sergey.shlypa2.utils.Functions
import kotlinx.android.synthetic.main.fragment_game.*
import timber.log.Timber
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import kotlin.math.abs


/**
* A simple [Fragment] subclass.
*/
class GameFragment : androidx.fragment.app.Fragment() {
class GameFragment : Fragment() {


lateinit var viewModel: RoundViewModel
private val viewModel: RoundViewModel by sharedViewModel()

private var cardYPath: Int? = null

private val onSwipeTouchListener = object : OnSwipeTouchListener() {
override fun onActionUp() {
cv_word.translationY = 0F
cv_word.translationX = 0F
frameWord.translationY = 0F
frameWord.translationX = 0F
}

override fun onSwipeTop(): Boolean {
viewModel.answerWord(true)
cv_word.hide()
frameWord.hide()
return true
}

override fun onSwipeBottom(): Boolean {
viewModel.answerWord(false)
cv_word.hide()
frameWord.hide()
return true
}
}
Expand All @@ -56,8 +53,6 @@ class GameFragment : androidx.fragment.app.Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
viewModel = ViewModelProviders.of(activity!!).get(RoundViewModel::class.java)

return inflater.inflate(R.layout.fragment_game, container, false)
}

Expand All @@ -71,7 +66,7 @@ class GameFragment : androidx.fragment.app.Fragment() {
})

onSwipeTouchListener.scrollListener = { x, y ->
cv_word.translationY = cv_word.translationY - y
frameWord.translationY = frameWord.translationY - y
}

viewModel.timerLiveData.observe(this, Observer { time -> time?.let { onTimer(it) } })
Expand All @@ -83,7 +78,7 @@ class GameFragment : androidx.fragment.app.Fragment() {
containerGame.setOnClickListener { }
containerGame.setOnTouchListener(onSwipeTouchListener)

tv_PlayGame.hide()

timerLinear.setOnClickListener {
if (timerStop) {
resumeTimer()
Expand All @@ -92,7 +87,8 @@ class GameFragment : androidx.fragment.app.Fragment() {
}
}

tv_PlayGame.setOnClickListener {
tvResumeGame.hide()
tvResumeGame.setOnClickListener {
resumeTimer()
}

Expand Down Expand Up @@ -122,18 +118,18 @@ class GameFragment : androidx.fragment.app.Fragment() {
}

private fun pauseTimer() {
tv_PlayGame.show()
tvResumeGame.show()
containerGame.setOnTouchListener(null)
viewModel.pauseTimer()
tv_word.hide()
tvWord.hide()
ibStopTime.setImageResource(R.drawable.ic_play_circle_outline_black_24dp)
timerStop = true
}

private fun resumeTimer() {
viewModel.startTimer()
tv_PlayGame.hide()
tv_word.show()
tvResumeGame.hide()
tvWord.show()
containerGame.setOnTouchListener(onSwipeTouchListener)
ibStopTime.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp)
timerStop = false
Expand All @@ -147,22 +143,21 @@ class GameFragment : androidx.fragment.app.Fragment() {
tvTime.text = "%02d:%02d".format(minutes, seconds)
}

fun onNextWord(word: Word) {
Timber.d("on Next word")
tv_word.text = word.word
private fun onNextWord(word: Word) {
tvWord.text = word.word

Handler().postDelayed({
if (cv_word != null) cv_word.show()
if (frameWord != null) frameWord.show()
}, 100)

runWordAppearAnimation()
}

fun onAnsweredCount(answered: Pair<Int, Int>) {
private fun onAnsweredCount(answered: Pair<Int, Int>) {
tvAnsweredCount.text = answered.first.toString()
}

fun runWordAppearAnimation() {
private fun runWordAppearAnimation() {
val animator = ValueAnimator.ofFloat(0F, 1F)

if (cardYPath == null) {
Expand All @@ -173,9 +168,11 @@ class GameFragment : androidx.fragment.app.Fragment() {

animator.addUpdateListener { animation ->
val animated = animation.animatedValue as Float
cv_word?.scaleX = animated
cv_word?.scaleY = animated
cv_word?.translationY = yPath * (1F - animated)
with(frameWord) {
scaleX = animated
scaleY = animated
translationY = yPath * (1F - animated)
}
}

animator.duration = 300
Expand All @@ -185,13 +182,11 @@ class GameFragment : androidx.fragment.app.Fragment() {

//Calculate path for a word card from top of a hat to center of the screen
private fun calculatePath(): Int? {
val hatTop = ivHat.top
val cardTop = cv_word.top

if (hatTop == 0 || cardTop == 0) return null
val hatTop = ivHat.top.takeIf { it != 0 } ?: return null
val cardTop = frameWord.top.takeIf { it != 0 } ?: return null

val additionalMargin: Float = context?.let { Functions.dpToPx(it, 32F) } ?: 0F
val additionalMargin: Float = 32.dpToPx.toFloat()

return Math.abs(hatTop - cardTop) + additionalMargin.toInt()
return abs(hatTop - cardTop) + additionalMargin.toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RoundViewModel(
private lateinit var round: Round
val roundLiveData = MutableLiveData<Round>()
val rounResultLiveData = MutableLiveData<List<TeamWithScores>>()
val scoresSheetLiveData = MutableLiveData<List<TeamWithScores>>()

val wordLiveData = MutableLiveData<Word>()
//First value - answered, second - skipped
Expand Down Expand Up @@ -223,7 +224,6 @@ class RoundViewModel(
}

fun startTimer() {
Timber.d("TESTING startTimer - ticker is $ticker")
launch {
ticker?.cancel()
ticker = ticker(1000, 1000)
Expand All @@ -248,6 +248,27 @@ class RoundViewModel(
}
}

fun updateCurrentScoresSheet() {
launch {
scoresSheetLiveData.value = emptyList()
val results = withContext(dispatchers.ioDispatcher) {
val currentResults = Game.getGameResults()
val roundScores = round.results

currentResults.forEach {
it.team.players.forEach { player ->
val scores = (roundScores[player.id] ?: 0) + (it.scoresMap[player.id] ?: 0)
it.scoresMap[player.id] = scores
}
}

currentResults
}

scoresSheetLiveData.value = results
}
}

private fun saveGameState() {
dataProvider.insertState(Game.state)
}
Expand All @@ -262,22 +283,6 @@ class RoundViewModel(
SHOW_HINT_TEAM_TABLE,
SHOW_INTERSTITIAL_ADS,
EXIT
}

fun loadCurrrentBal(): List<TeamWithScores> {
val g = Game.getGameResults()
val f = round.results

g.forEach {
val map = it.scoresMap
it.team.players.forEach {
var scores: Int = f[it.id] ?: 0
scores += map[it.id] ?: 0
map[it.id] = scores
}

}

return g
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
package com.example.sergey.shlypa2.screens.game

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.sergey.shlypa2.R
import com.example.sergey.shlypa2.RvAdapter
import com.example.sergey.shlypa2.extensions.observeSafe
import com.example.sergey.shlypa2.screens.game.adapter.ItemTeamWithScores
import eu.davidea.flexibleadapter.FlexibleAdapter
import kotlinx.android.synthetic.main.fragment_team_hint.*
import org.koin.androidx.viewmodel.ext.android.sharedViewModel


class TeamHintFragment : androidx.fragment.app.Fragment() {
class TeamHintFragment : Fragment() {


val adapter = RvAdapter()
private val adapter = FlexibleAdapter(emptyList())
private val viewModel: RoundViewModel by sharedViewModel()
private val playersViewPool = RecyclerView.RecycledViewPool()

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

lateinit var viewModel: RoundViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_team_hint, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(activity!!).get(RoundViewModel::class.java)

adapter.setData(viewModel.loadCurrrentBal())
rvHintTeam_and_Result.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context)
rvHintTeam_and_Result.adapter = adapter
rvScoresSheet.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context)
rvScoresSheet.adapter = adapter

viewModel.updateCurrentScoresSheet()
viewModel.scoresSheetLiveData.observeSafe(this) { teams ->
adapter.updateDataSet(teams.map { ItemTeamWithScores(it, playersViewPool) })
}
}

}
Loading

0 comments on commit ed1ba70

Please sign in to comment.