Skip to content
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

Update Score and Word Count Display #10 #20

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation 'androidx.fragment:fragment-ktx:1.6.1'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.activity:activity:1.8.0'
}
16 changes: 14 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
Expand All @@ -9,10 +8,23 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Unscramble">
<activity android:name=".MainActivity"
<activity
android:name=".SplashScreen"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/example/android/unscramble/SplashScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.android.unscramble

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity

@Suppress("DEPRECATION")
class SplashScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)

// This is used to hide the status bar and make
// the splash screen as a full screen activity.
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)

// we used the postDelayed(Runnable, time) method
// to send a message with a delayed time.
//Normal Handler is deprecated , so we have to change the code little bit

// Handler().postDelayed({
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}, 3000) // 3000 is the delayed time in milliseconds.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GameViewModel : ViewModel() {

private var wordsList: MutableList<String> = mutableListOf()
private lateinit var currentWord: String

var i = 0
private var _score = MutableLiveData(0)
val score: LiveData<Int>
get() = _score
Expand All @@ -21,15 +21,24 @@ class GameViewModel : ViewModel() {
private val _currentScrambledWord = MutableLiveData<String>()
val currentScrambledWord: LiveData<String>
get() = _currentScrambledWord

public fun shuffle(str: String): String {
val charArray = str.toCharArray()
for (i in charArray.indices) {
val randomIndex = charArray.indices.random()
val temp = charArray[i]
charArray[i] = charArray[randomIndex]
charArray[randomIndex] = temp
}
return String(charArray)
}

private fun getNextWord() {
currentWord = allWordsList.random()

if (wordsList.contains(currentWord)) {
getNextWord()
} else {
_currentScrambledWord.value = currentWord
_currentScrambledWord.value = shuffle(currentWord)
_currentWordCount.value = (_currentWordCount.value)?.inc()
wordsList.add(currentWord)
}
Expand All @@ -49,18 +58,20 @@ class GameViewModel : ViewModel() {
}

private fun increaseScore() {
_score.value = 0
_score.value = 20 * i
}

fun isUserWordCorrect(playerWord: String): Boolean {
if (playerWord.equals(currentWord, true)) {
i = i + 1
increaseScore()
return true
}
return false
}

fun reinitializeData() {
i = 0
_score.value = 0
_currentWordCount.value = 0
wordsList.clear()
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_splash_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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="#fff"
app:layoutDescription="@xml/activity_splash_screen_scene"
tools:context=".SplashScreen">

<ImageView
android:id="@+id/SplashScreenImage"
android:layout_width="300dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_foreground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.motion.widget.MotionLayout>
44 changes: 44 additions & 0 deletions app/src/main/res/xml/activity_splash_screen_scene.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">

<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:autoTransition="animateToEnd"
motion:duration="1000">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="0"
android:scaleX="1.2" />
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="0"
android:scaleY="1.2" />
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="100"
android:scaleX="100" />
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="100"
android:scaleY="100" />
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="76"
android:scaleX="0.7" />
<KeyAttribute
motion:motionTarget="@+id/SplashScreenImage"
motion:framePosition="76"
android:scaleY="0.7" />
</KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/start">
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
</ConstraintSet>
</MotionScene>