Skip to content

Feature/my guess it app #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: starter-code
Choose a base branch
from
35 changes: 22 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: "androidx.navigation.safeargs.kotlin"

android {
Expand All @@ -39,25 +38,35 @@ android {
buildFeatures {
dataBinding true
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

// KTX
implementation 'androidx.core:core-ktx:1.3.1'
// https://developer.android.com/jetpack/androidx/releases/core
implementation "androidx.core:core-ktx:$core_version"

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-rc02"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-rc02"
// https://developer.android.com/jetpack/androidx/releases/navigation
implementation "androidx.navigation:navigation-fragment-ktx:$rootProject.nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$rootProject.nav_version"

// Lifecycles
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
// https://developer.android.com/jetpack/androidx/releases/lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.guesstheword">

<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_guess_it"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.android.guesstheword.screens.game

private val CORRECT_BUZZ_PATTERN = longArrayOf(100, 100, 100, 100, 100, 100)
private val PANIC_BUZZ_PATTERN = longArrayOf(0, 200)
private val GAME_FINISHED_BUZZ_PATTERN = longArrayOf(0, 2000)
private val NO_BUZZ_PATTERN = longArrayOf(0)

enum class BuzzType(val pattern: LongArray) {
CORRECT(CORRECT_BUZZ_PATTERN),
GAME_FINISHED(GAME_FINISHED_BUZZ_PATTERN),
COUNTDOWN_PANIC(PANIC_BUZZ_PATTERN),
NO_BUZZ(NO_BUZZ_PATTERN)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,126 +16,67 @@

package com.example.android.guesstheword.screens.game

import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment.findNavController
import com.example.android.guesstheword.R
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.example.android.guesstheword.databinding.GameFragmentBinding
import com.example.android.guesstheword.screens.game.GameViewModel.Companion.INITIAL_SCORE

/**
* Fragment where the game is played
*/
class GameFragment : Fragment() {

// The current word
private var word = ""

// The current score
private var score = 0

// The list of words - the front of the list is the next word to guess
private lateinit var wordList: MutableList<String>

private lateinit var binding: GameFragmentBinding

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

// Inflate view and obtain an instance of the binding class
binding = DataBindingUtil.inflate(
inflater,
R.layout.game_fragment,
container,
false
)

resetList()
nextWord()

binding.correctButton.setOnClickListener { onCorrect() }
binding.skipButton.setOnClickListener { onSkip() }
updateScoreText()
updateWordText()
return binding.root

}

/**
* Resets the list of words and randomizes the order
*/
private fun resetList() {
wordList = mutableListOf(
"queen",
"hospital",
"basketball",
"cat",
"change",
"snail",
"soup",
"calendar",
"sad",
"desk",
"guitar",
"home",
"railway",
"zebra",
"jelly",
"car",
"crow",
"trade",
"bag",
"roll",
"bubble"
)
wordList.shuffle()
private val viewModel: GameViewModel by viewModels()

private val navController: NavController by lazy { findNavController() }

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = GameFragmentBinding.inflate(inflater, container, false)
.apply {
viewModel = this@GameFragment.viewModel
lifecycleOwner = viewLifecycleOwner
}
.root

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(viewModel) {
gameFinishedEvent.observe(viewLifecycleOwner) { hasFinished ->
if (hasFinished) navigateToScore()
}
buzzGameEvent.observe(viewLifecycleOwner) { buzzType -> vibrate(buzzType.pattern) }
}
}

/**
* Called when the game is finished
*/
private fun gameFinished() {
val action = GameFragmentDirections.actionGameToScore(score)
findNavController(this).navigate(action)
}

/**
* Moves to the next word in the list
*/
private fun nextWord() {
//Select and remove a word from the list
if (wordList.isEmpty()) {
gameFinished()
} else {
word = wordList.removeAt(0)
}
updateWordText()
updateScoreText()
}

/** Methods for buttons presses **/

private fun onSkip() {
score--
nextWord()
}

private fun onCorrect() {
score++
nextWord()
}

/** Methods for updating the UI **/

private fun updateWordText() {
binding.wordText.text = word

}

private fun updateScoreText() {
binding.scoreText.text = score.toString()
}
private fun navigateToScore() =
GameFragmentDirections.actionGameToScore(viewModel.score.value ?: INITIAL_SCORE)
.run { navController.navigate(this) }
.also { viewModel.onGameFinishedNavigated() }

private fun vibrate(pattern: LongArray) =
activity?.getSystemService<Vibrator>()?.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrate(VibrationEffect.createWaveform(pattern, -1))
} else {
//deprecated in API 26
@Suppress("DEPRECATION")
vibrate(pattern, -1)
}
}
}
Loading