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

Refactoring remove duplication #368

Merged
merged 22 commits into from
Jun 7, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ch.sdp.vibester.model

import android.os.Environment
import androidx.test.platform.app.InstrumentationRegistry
import ch.sdp.vibester.api.LastfmMethod
import org.junit.Assert
import org.junit.Test
import java.io.File
Expand All @@ -24,7 +23,7 @@ class OfflineSongListTest {
val totalPages = "20"
val totalSongs = "2000"

Assert.assertEquals(inputSongsList, mySongsList.getSongList())
Assert.assertEquals(inputSongsList, mySongsList.songList)
Assert.assertEquals(page, mySongsList.getPage())
Assert.assertEquals(songsPerPage, mySongsList.getSongsPerPage())
Assert.assertEquals(totalPages, mySongsList.getTotalPages())
Expand All @@ -35,7 +34,7 @@ class OfflineSongListTest {
@Test
fun addSpecificSongToList() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
val records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("bones - imagine dragons\n")

Expand All @@ -53,7 +52,7 @@ class OfflineSongListTest {
val totalPages = "20"
val totalSongs = "2000"

Assert.assertEquals(inputSongsList, mySongsList.getSongList())
Assert.assertEquals(inputSongsList, mySongsList.songList)
Assert.assertEquals(page, mySongsList.getPage())
Assert.assertEquals(songsPerPage, mySongsList.getSongsPerPage())
Assert.assertEquals(totalPages, mySongsList.getTotalPages())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SongListTest {
assertEquals(songName, mySongsList.getShuffledSongList().get(0).first)


assertEquals(inputSongsList, mySongsList.getSongList())
assertEquals(inputSongsList, mySongsList.songList)
assertEquals(page, mySongsList.getPage())
assertEquals(songsPerPage, mySongsList.getSongsPerPage())
assertEquals(totalPages, mySongsList.getTotalPages())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class BuzzerScreenActivity : GameActivity() {
gameIsOn = true
toggleBtnVisibility(R.id.skip_buzzer, true)
findViewById<LinearLayout>(R.id.answer).visibility=View.INVISIBLE
val trackName = gameManager.getCurrentSong().getTrackName()
val artist = gameManager.getCurrentSong().getArtistName()
findViewById<TextView>(R.id.songTitle).text = "$trackName - $artist"
//val trackName = gameManager.getCurrentSong().getTrackName()
//val artist = gameManager.getCurrentSong().getArtistName()
findViewById<TextView>(R.id.songTitle).text =
"${gameManager.getCurrentSong().getTrackName()} - ${gameManager.getCurrentSong().getArtistName()}"
//Checks if internet is available. If not, skip the loading of the artwork from url.
if (gameManager.getInternet()) {
Glide.with(ctx).load(gameManager.getCurrentSong().getArtworkUrl()).override(artworkDim, artworkDim).into(findViewById(R.id.songArtwork))
Expand Down Expand Up @@ -293,7 +294,7 @@ class BuzzerScreenActivity : GameActivity() {
/**
* Fires an intent from the Gamescreen to the Ending Screen
*/
fun switchToEnding(gameManager: GameManager) {
private fun switchToEnding(gameManager: GameManager) {
checkAndStopPlayer(gameManager)
val intent = Intent(this, GameEndingActivity::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class GameEndingActivity : DownloadFunctionalityActivity(), OnItemClickListener

setUpRecyclerView()

Helper().setReturnToMainListener(findViewById<FloatingActionButton>(R.id.end_returnToMain), this)
val returnBtn = findViewById<FloatingActionButton>(R.id.end_returnToMain)
Helper().setReturnToMainListener(returnBtn, this)

createDownloadReceiver(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class LyricsBelongGameActivity : GameActivity() {
if (getIntent != null) {
gameManager = getIntent.getSerializable("gameManager") as GameManager
// put in one line to increase coverage
findViewById<Button>(R.id.nextSongLyrics).setOnClickListener { startRound(ctx, gameManager) }
val nextSong = findViewById<Button>(R.id.nextSongLyrics)
nextSong.setOnClickListener { startRoundLyrics(ctx, gameManager) }
findViewById<Button>(R.id.lyricMatchButton).setOnClickListener { getAndCheckLyrics(ctx, song, speechInput, gameManager) }

gameManager.setNextSong()
startRound(ctx, gameManager)
startRoundLyrics(ctx, gameManager)
super.setMax(intent)
}

Expand Down Expand Up @@ -87,7 +88,7 @@ class LyricsBelongGameActivity : GameActivity() {
* @param ctx: Context on which the round is happening.
* @param gameManager: The gameManager instance that is managing the current game.
*/
private fun startRound(ctx: Context, gameManager: GameManager) {
private fun startRoundLyrics(ctx: Context, gameManager: GameManager) {
toggleBtnVisibility(R.id.lyricMatchButton, false)
toggleBtnVisibility(R.id.nextSongLyrics, false)
song = gameManager.getCurrentSong()//Song.songBuilder("", "", gameManager.currentSong.getTrackName(), gameManager.currentSong.getArtistName())
Expand Down Expand Up @@ -227,7 +228,7 @@ class LyricsBelongGameActivity : GameActivity() {
}

fun testStartRound(ctx: Context, gameManager: GameManager) {
startRound(ctx, gameManager)
startRoundLyrics(ctx, gameManager)
}

}
10 changes: 4 additions & 6 deletions app/src/main/java/ch/sdp/vibester/activity/QrScanningActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.sdp.vibester.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
Expand All @@ -13,8 +12,6 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import ch.sdp.vibester.R
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
Expand All @@ -26,7 +23,6 @@ import com.google.android.gms.vision.barcode.Barcode
import com.google.android.gms.vision.barcode.BarcodeDetector
import dagger.hilt.android.AndroidEntryPoint
import java.io.IOException
import java.io.Serializable
import javax.inject.Inject


Expand Down Expand Up @@ -134,7 +130,8 @@ class QrScanningActivity : AppCompatActivity() {
private fun setupBarcodeDetector(barcodeDetector: BarcodeDetector) {
barcodeDetector.setProcessor(object : Detector.Processor<Barcode> {
override fun release() {
Toast.makeText(applicationContext, getString(R.string.qrScanning_scannerClosed), Toast.LENGTH_SHORT).show()
val text = getString(R.string.qrScanning_scannerClosed)
Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
}

override fun receiveDetections(detections: Detector.Detections<Barcode>) {
Expand Down Expand Up @@ -186,7 +183,8 @@ class QrScanningActivity : AppCompatActivity() {
finish()
} else {
// Camera permission not granted, come back to previous activity
Toast.makeText(applicationContext, getString(R.string.qrScanning_cameraError), Toast.LENGTH_LONG).show()
val text = getString(R.string.qrScanning_cameraError)
Toast.makeText(applicationContext, text, Toast.LENGTH_LONG).show()
finishActivity()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class TypingGameActivity : GameActivity() {
val inputTxt = findViewById<EditText>(R.id.yourGuessET)
setGuessLayoutListener(inputTxt, guessLayout)

findViewById<Button>(R.id.skip_typing).setOnClickListener {
checkAnswer(ctx, null, gameManager)
}
val skipBtn = findViewById<Button>(R.id.skip_typing)
skipBtn.setOnClickListener { checkAnswer(ctx, null, gameManager) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ open class DownloadFunctionalityActivity : AppCompatActivity() {
override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (id == downloadId) {
alert(getString(R.string.download_download_complete), getString(R.string.download_try_another), songNameView)
alert(R.string.download_download_complete, R.string.download_try_another, songNameView)
}
}
}
Expand All @@ -90,12 +90,12 @@ open class DownloadFunctionalityActivity : AppCompatActivity() {
* @param hint : String to be set as the hint of the textView.
* @param view : The textView that will be updated.
*/
fun alert(toast: String, hint: String?, view: TextView?) {
fun alert(toast: Int, hint: Int?, view: TextView?) {
downloadComplete = true
downloadStarted = false
Toast.makeText(applicationContext, toast, Toast.LENGTH_LONG).show()
Toast.makeText(applicationContext, getString(toast), Toast.LENGTH_LONG).show()
if (hint != null && view != null) {
editTextView(hint, view)
editTextView(getString(hint), view)
}
}

Expand All @@ -120,7 +120,7 @@ open class DownloadFunctionalityActivity : AppCompatActivity() {
}

if (checkExistingSong()) {
alert(getString(R.string.download_already_done), getString(R.string.download_try_different), songView)
alert(R.string.download_already_done, R.string.download_try_different, songView)
} else { getAndDownload(songView) }
}
}
Expand All @@ -144,7 +144,7 @@ open class DownloadFunctionalityActivity : AppCompatActivity() {
songName = song.getTrackName().lowercase() + " - " + song.getArtistName().lowercase()
checkPermissionsAndDownload()
} catch (e: IllegalArgumentException) {
alert(getString(R.string.download_unable_to_find), getString(R.string.download_retry), songView)
alert(R.string.download_unable_to_find, R.string.download_retry, songView)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class DownloadManagerActivity : AppCompatActivity(), OnItemClickListener {

setContentView(R.layout.activity_download_manager)

Helper().setReturnToMainListener(
findViewById<FloatingActionButton>(R.id.downloadManager_returnToMain),
this
)
val returnBtn = findViewById<FloatingActionButton>(R.id.downloadManager_returnToMain)
Helper().setReturnToMainListener(returnBtn, this)

setUpRecyclerView()
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ class DataGetter @Inject constructor() {
dbUserRef.child(userId).get().addOnSuccessListener {
it.getValue<User>()?.let { it1 -> callback(it1) }
}.addOnFailureListener{
Log.d("DataGetter", "getUserData:onCancelled", it)
val msg = "getUserData:onCancelled"
Log.d("DataGetter", msg, it)
}
}

Expand Down Expand Up @@ -284,7 +285,9 @@ class DataGetter @Inject constructor() {
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException())
val msg = "loadPost:onCancelled"
val exception = databaseError.toException()
Log.w(TAG, msg, exception)
}
}
queryRooms.addValueEventListener(startGameListener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)

private lateinit var auth: FirebaseAuth

private lateinit var authentication_status: TextView
private lateinit var authenticationStatus: TextView
private lateinit var username: EditText
private lateinit var password: EditText
private var vmAuth = ViewModel()
Expand All @@ -55,30 +55,27 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)
super.onViewCreated(view, savedInstanceState)
vmAuth.view = view
vmAuth.ctx = view.context
val googleSignInToken =
"7687769601-qiqrp6kt48v89ub76k9lkpefh9ls36ha.apps.googleusercontent.com"
val googleSignInToken = "7687769601-qiqrp6kt48v89ub76k9lkpefh9ls36ha.apps.googleusercontent.com"

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(googleSignInToken)
.requestEmail()
.build()
.requestIdToken(googleSignInToken).requestEmail().build()

googleSignInClient = GoogleSignIn.getClient(vmAuth.ctx, gso)

auth = Firebase.auth

username = vmAuth.view.findViewById(R.id.username)
password = vmAuth.view.findViewById(R.id.password)
authentication_status = vmAuth.view.findViewById(R.id.authentication_status)

vmAuth.view.findViewById<Button>(R.id.createAcc)
.setOnClickListener { createAccountListener() }
vmAuth.view.findViewById<Button>(R.id.logIn)
.setOnClickListener { logInListener() }
vmAuth.view.findViewById<Button>(R.id.googleBtn)
.setOnClickListener { googleSignInListener() }
vmAuth.view.findViewById<Button>(R.id.forgotPassword)
.setOnClickListener { resetPasswordListener() }
authenticationStatus = vmAuth.view.findViewById(R.id.authentication_status)

val createAccount = vmAuth.view.findViewById<Button>(R.id.createAcc)
createAccount.setOnClickListener { createAccountListener() }
val logIn = vmAuth.view.findViewById<Button>(R.id.logIn)
logIn.setOnClickListener { logInListener() }
val google = vmAuth.view.findViewById<Button>(R.id.googleBtn)
google.setOnClickListener { googleSignInListener() }
val forgetPass = vmAuth.view.findViewById<Button>(R.id.forgotPassword)
forgetPass.setOnClickListener { resetPasswordListener() }
}

/**
Expand Down Expand Up @@ -106,7 +103,7 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)
/**
* Listener bound to the "Reset Password" button in the Authentication activity.
*/
fun resetPasswordListener() {
private fun resetPasswordListener() {
resetPassword(username.text.toString())
}

Expand Down Expand Up @@ -150,8 +147,8 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(getString(R.string.log_tag), "signInWithCredential:success")
if (task.getResult().additionalUserInfo != null) {
createAcc = task.getResult().additionalUserInfo!!.isNewUser
if (task.result.additionalUserInfo != null) {
createAcc = task.result.additionalUserInfo!!.isNewUser
if (createAcc) {
createAccount()
}
Expand All @@ -170,23 +167,23 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)

/**
* A function validates email and password
* @param email email
* @param password passwprd
* @param username username
* @param password password
* @return validity of email and password
*/
private fun credentialsValidation(username: String, password: String): Boolean {
if (username.isEmpty() || password.isEmpty()) {
authentication_status.setText(R.string.authentication_emptyField)
authenticationStatus.setText(R.string.authentication_emptyField)
return false
}

if (!username.contains('@')) {
authentication_status.setText(R.string.authentication_notAnEmail)
authenticationStatus.setText(R.string.authentication_notAnEmail)
return false
}

if (password.length < 6) {
authentication_status.setText(R.string.authentication_shortPassword)
authenticationStatus.setText(R.string.authentication_shortPassword)
return false
}
return true
Expand Down Expand Up @@ -254,12 +251,11 @@ class AuthenticationFragment : Fragment(R.layout.fragment_layout_authentication)

/**
* Reset Password function
* @param email email adress for the password reset
* @param email email address for the password reset
*/
private fun resetPassword(email: String) {
auth.sendPasswordResetEmail(email).addOnFailureListener {
Toast.makeText(vmAuth.ctx, R.string.authentication_forgotPassword, Toast.LENGTH_SHORT)
.show()
Toast.makeText(vmAuth.ctx, R.string.authentication_forgotPassword, Toast.LENGTH_SHORT).show()
}
}
}
Loading