Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

implement SharedPref #150

Merged
merged 25 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch.sdp.vibester.model

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import ch.sdp.vibester.profile.UserProfile
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test

class UserSharedPrefTest {
@Test
fun sharedPrefTest(){
val handle: String = "myHandle"
val username: String = "myUsername"
val image: String = "myImage"
val email: String = "myEmail"
val totalGames: Int = 10
val bestScore: Int = 27
val correctSongs: Int = 16
val ranking: Int = 6

val ctx = ApplicationProvider.getApplicationContext() as Context

val pro = UserProfile(handle, username, image, email, totalGames, bestScore, correctSongs, ranking)
UserSharedPref.setUser(ctx, pro)
UserSharedPref.updateScore(ctx, 1, -1, 0, 1)

val updated = UserSharedPref.getUser(ctx)

assertEquals(pro.handle, updated.handle)
assertEquals(pro.username, updated.username)
assertEquals(pro.image, updated.image)
assertEquals(pro.email, updated.email)
assertEquals(pro.totalGames+1, updated.totalGames)
assertEquals(pro.bestScore-1, updated.bestScore)
assertEquals(pro.correctSongs, updated.correctSongs)
assertEquals(pro.ranking+1, updated.ranking)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.Window.FEATURE_NO_TITLE
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.model.Song
Expand All @@ -14,8 +15,11 @@ class WelcomeActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
requestWindowFeature(FEATURE_NO_TITLE)
supportActionBar?.hide()

setContentView(R.layout.activity_welcome_screen)

val tv = findViewById<TextView>(R.id.WelcomingText)
tv.isSelected = true

}

private fun sendDirectIntent(arg: Class<*>?) {
Expand Down
85 changes: 85 additions & 0 deletions app/src/main/java/ch/sdp/vibester/model/UserSharedPref.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ch.sdp.vibester.model

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import ch.sdp.vibester.profile.UserProfile


class UserSharedPref private constructor() {
companion object{
val HANDLE = "handle"
val USERNAME = "username"
val IMAGE = "image"
val EMAIL = "email"
val TOTAL_GAMES = "totalGames"
val BEST_SCORE = "bestScore"
val CORRECT_SONGS = "correctSongs"
val RANKING = "ranking"
/*
https://stackoverflow.com/questions/12744337/how-to-keep-android-applications-always-be-logged-in-state#:~:text=When%20users%20log%20in%20to,direct%20to%20the%20login%20page.
*/

fun getSharedPreferences(ctx: Context): SharedPreferences? {
return PreferenceManager.getDefaultSharedPreferences(ctx)
}

fun setUser(ctx: Context, user: UserProfile){
val edit = getSharedPreferences(ctx)?.edit()
if (edit != null) {
edit.putString(HANDLE, user.handle)
edit.putString(USERNAME, user.username)
edit.putString(IMAGE, user.image)
edit.putString(EMAIL, user.email)
edit.putInt(TOTAL_GAMES, user.totalGames)
edit.putInt(BEST_SCORE, user.bestScore)
edit.putInt(CORRECT_SONGS, user.correctSongs)
edit.putInt(RANKING, user.ranking)
edit.commit()
}
}

fun updateScore(ctx: Context, deltaTotal: Int = 0, deltaBest: Int = 0, deltaCorrect: Int = 0, deltaRanking: Int = 0){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method updateScore has 5 arguments (exceeds 4 allowed). Consider refactoring.

val sharedPref = getSharedPreferences(ctx)
if(sharedPref != null){
val edit = sharedPref.edit()
val current_total_games = sharedPref.getInt(TOTAL_GAMES, 0) + deltaTotal
val current_best_score = sharedPref.getInt(BEST_SCORE, 0) + deltaBest
val current_correct_song = sharedPref.getInt(CORRECT_SONGS, 0) + deltaCorrect
val current_ranking = sharedPref.getInt(RANKING, 0) + deltaRanking
edit.putInt(TOTAL_GAMES, current_total_games)
edit.putInt(BEST_SCORE, current_best_score)
edit.putInt(CORRECT_SONGS, current_correct_song)
edit.putInt(RANKING, current_ranking)
edit.commit()
}
}

fun getUser(ctx: Context): UserProfile{
val sharedPref = getSharedPreferences(ctx)

var handle = ""
var username = ""
var image = ""
var email = ""
var totalGames = -1
var bestScore = -1
var correctSongs = -1
var ranking = -1

if(sharedPref != null) {
handle = sharedPref.getString(HANDLE, "").toString()
username = sharedPref.getString(USERNAME, "").toString()
image = sharedPref.getString(IMAGE, "").toString()
email = sharedPref.getString(EMAIL, "").toString()
totalGames = sharedPref.getInt(TOTAL_GAMES, 0)
bestScore = sharedPref.getInt(BEST_SCORE, 0)
correctSongs = sharedPref.getInt(CORRECT_SONGS, 0)
ranking = sharedPref.getInt(RANKING, 0)
}
return UserProfile(handle, username, image, email, totalGames, bestScore, correctSongs, ranking)
}


}
}
31 changes: 26 additions & 5 deletions app/src/main/res/layout/activity_welcome_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
<ScrollView
android:layout_width="468dp"
android:layout_height="0dp"
android:layout_marginTop="27dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vibester_welcome_logo"
app:layout_constraintVertical_bias="0.0">
app:layout_constraintTop_toBottomOf="@+id/WelcomingText">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -41,10 +40,10 @@
android:layout_marginTop="10dp"
android:backgroundTint="@color/cg_blue"
android:elevation="10dp"
android:stateListAnimator="@null"
android:translationZ="10dp"
android:onClick="switchToPlay"
android:stateListAnimator="@null"
android:text="@string/play_a_game"
android:translationZ="10dp"
app:icon="@android:drawable/ic_media_play" />

<Button
Expand Down Expand Up @@ -106,4 +105,26 @@
</LinearLayout>
</ScrollView>

<TextView
android:id="@+id/WelcomingText"
android:layout_width="407dp"
android:layout_height="28dp"
android:layout_marginStart="5dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="5dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:gravity="center"
android:text="Welcome New Player, create an account in the profile section"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vibester_welcome_logo" />

</androidx.constraintlayout.widget.ConstraintLayout>