Skip to content

Commit

Permalink
first time normal works
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirxomitov committed Feb 14, 2024
1 parent d3cb26a commit dfacfb2
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package uz.gita.memorygame.presentation.screens

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -34,36 +38,76 @@ class GameScreen : Fragment(R.layout.screen_game) {
private var secondImageIndex = -1
private var findCardsCount = 0

@SuppressLint("FragmentLiveDataObserve")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) = binding.myApply {
super.onViewCreated(view, savedInstanceState)

viewModel.reloadGameLiveData.observe(this@GameScreen, reloadGameObserver)
viewModel.countLiveData.observe(this@GameScreen, countObserver)

countY = navArgs.verticalCardCount
countX = navArgs.horizontalCardCount

binding.container.post {
cardHeight = container.height.toFloat() / countY
cardWidth = container.width.toFloat() / countX

val ls = viewModel.getCardsByLevel(countX, countY)

loadImages(ls)
lifecycleScope.launch {
delay(1000) // 1 soniya kartalarni ochiq ushlab turadi,
hideCards()
delay(1000) // animatsiya bilan yopilishi uchun 1 soniya ketadi
setCardsClick()
}
val cards = viewModel.getCardsByLevel(countX, countY)
loadImages(cards)
setCardsClick()
}

reload.setOnClickListener {

viewModel.reloadGame()
}
menu.setOnClickListener {
findNavController().popBackStack()
}
menu.setOnClickListener { }
}

private fun hideCards() {
views.forEach {
it.hideImage()
private val countObserver = Observer<Int> {
binding.attempt.text = it.toString()
}

private val reloadGameObserver = Observer<Unit> {
binding.reload.isClickable = false
firstImageIndex = -1
secondImageIndex = -1

val cards = viewModel.getCardsByLevel(countX, countY)
binding.container.children.forEachIndexed { index, view ->
val img = view as ImageView
img.tag = cards[index]

img.isClickable = true

img.layoutParams.apply {
height = cardHeight.toInt()
width = cardWidth.toInt()
}

img.animate()
.setDuration(500)
.rotationBy(180f)
.scaleX(0f)
.scaleY(0f)
.withEndAction {
img.setImageResource(R.drawable.image_back)
img.rotation = 0f
img.animate()
.setDuration(500)
.scaleX(1f)
.scaleY(1f)
.rotationBy(360f)
.start()

lifecycleScope.launch {
delay(500)
binding.reload.isClickable = true
}
}
.start()
views[index] = img
}
}

Expand All @@ -72,7 +116,7 @@ class GameScreen : Fragment(R.layout.screen_game) {
for (j in 0 until countX) {
val img = ImageView(requireContext())
img.isClickable = false
img.setImageResource(images[i * countX + j])
img.setImageResource(R.drawable.image_back)

img.x = j * cardWidth
img.y = i * cardHeight
Expand All @@ -95,6 +139,7 @@ class GameScreen : Fragment(R.layout.screen_game) {
imageView.setOnClickListener {
if (firstImageIndex != -1 && secondImageIndex != -1) return@setOnClickListener
imageView.openImage()
viewModel.increaseCount()

if (firstImageIndex == -1) {
firstImageIndex = index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ class LevelScreen : Fragment(R.layout.screen_level) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.safariCar
.animate()
.scaleX(1f)
.scaleY(1f)
.setDuration(1000)
.start()

binding.tableSafari.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.setDuration(500)
.start()

binding.mapSafari.animate()
.scaleXBy(0.3f)
.scaleYBy(0.3f)
.setDuration(500)
.start()

binding.root.post {
val width = binding.root.width
val treeWidth = binding.treeSafari.width

val treeX = (width - treeWidth).toFloat() / 2

binding.treeSafari.animate()
.scaleX(1f)
.scaleY(1f)
.rotation(1000f)
.setDuration(1000)
.x(treeX)
.start()
}

binding.apply {
easy.setOnClickListener {
startGame(2, 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package uz.gita.memorygame.presentation.viewmodels

import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow


interface GameViewModel {
val reloadGameLiveData : LiveData<Unit>
val countLiveData : LiveData<Int>

fun getCardsByLevel(countX: Int, countY: Int) : List<Int>
fun userWin()

fun reloadGame()
fun increaseCount()
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package uz.gita.memorygame.presentation.viewmodels.impl

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import uz.gita.memorygame.domain.AppRepository
import uz.gita.memorygame.presentation.viewmodels.GameViewModel
import javax.inject.Inject

@HiltViewModel
class GameViewModelImpl
@Inject constructor(
class GameViewModelImpl @Inject constructor(
private val repository: AppRepository
) : GameViewModel, ViewModel() {
override val reloadGameLiveData = MutableLiveData<Unit>()
override val countLiveData = MutableLiveData<Int>(0)

override fun getCardsByLevel(countX: Int, countY: Int) =
repository.getCardsByLevel(countX, countY)

override fun userWin() {
countLiveData.value = 0
}

override fun reloadGame() {
reloadGameLiveData.value = Unit
countLiveData.value = 0
}

override fun increaseCount() {
countLiveData.value = countLiveData.value?.plus(1)
}
}
Binary file added app/src/main/res/drawable/bg_jungle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/car_safari.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/car_safari_better.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/map_safari.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tree_safari.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 105 additions & 42 deletions app/src/main/res/layout/screen_level.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_jungle"
tools:context=".presentation.screens.LevelScreen">


<ImageView
android:id="@+id/safariCar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0"
android:scaleY="0"
android:src="@drawable/car_safari"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/lineHor30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/lineHor60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />

<ImageView
android:id="@+id/tableSafari"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/bg5"
tools:context=".presentation.screens.LevelScreen">


<ImageView
android:layout_width="150dp"
android:layout_height="60dp"
android:src="@drawable/easy"
android:id="@+id/easy"
android:layout_marginTop="30dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="60dp"
android:src="@drawable/medium"
android:id="@+id/medium"
android:layout_margin="8dp"
app:layout_constraintTop_toBottomOf="@id/easy"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="60dp"
android:src="@drawable/hard"
android:id="@+id/hard"
android:layout_margin="8dp"
app:layout_constraintTop_toBottomOf="@id/medium"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="0dp"
android:layout_marginHorizontal="12dp"
android:src="@drawable/table_safari_better"
app:layout_constraintBottom_toTopOf="@id/lineHor30"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/mapSafari"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:src="@drawable/map_safari"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/lineHor60" />

<ImageView
android:id="@+id/treeSafari"
android:layout_width="0dp"
android:src="@drawable/tree_safari"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="@id/lineHor60"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />


<ImageView
android:id="@+id/easy"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:src="@drawable/easy"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />


<ImageView
android:id="@+id/medium"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_margin="8dp"
android:src="@drawable/medium"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/easy" />

<ImageView
android:id="@+id/hard"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_margin="8dp"
android:src="@drawable/hard"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/medium" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit dfacfb2

Please sign in to comment.