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

Jwen/remove code smells #359

Merged
merged 8 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -75,7 +75,7 @@ class BuzzerSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListe
* id: the id of the field to update
*/
fun updatePlayerNameVisibility(n: Int, id: Int) {
var i = when (id) {
val i = when (id) {
R.id.namePlayer2 -> 2
R.id.namePlayer3 -> 3
R.id.namePlayer4 -> 4
Expand All @@ -91,27 +91,30 @@ class BuzzerSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListe

fun switchToGame(view: View) {
val intent = Intent(this, BuzzerScreenActivity::class.java)
val players =
findViewById<LinearLayout>(R.id.playerNames).children.filter { child: View -> child.visibility == View.VISIBLE }
val players = findViewById<LinearLayout>(R.id.playerNames).children.filter { child: View -> child.visibility == View.VISIBLE }
val pNameArray = arrayOfNulls<String>(players.count())

if (players.count() > 0) {
intent.putExtra("Number of players", players.count())
} else {
intent.putExtra("Number of players", 1)
}

intent.putExtra("gameManager", gameManager)
intent.putExtra("Difficulty", difficulty)
val editTextIdArray =
arrayListOf(R.id.namePlayer1, R.id.namePlayer2, R.id.namePlayer3, R.id.namePlayer4)

val editTextIdArray = arrayListOf(R.id.namePlayer1, R.id.namePlayer2, R.id.namePlayer3, R.id.namePlayer4)
var i = 0
for (playerView in players) {
val name = findViewById<EditText>(editTextIdArray[i]).text.toString()
if (name.isNotEmpty()) { pNameArray[i] = name } else {
findViewById<LinearLayout>(R.id.missingNameAlert).visibility = View.VISIBLE
findViewById<Button>(R.id.nb_players_selected).visibility = View.INVISIBLE
return }
return
}
i += 1
}

intent.putExtra("Player Names", pNameArray)
startActivity(intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ class QrScanningActivity : AppCompatActivity() {
.setAutoFocusEnabled(true)
.build()

binding.cameraSurfaceView.getHolder().addCallback(object : SurfaceHolder.Callback {
setupCamera(camera)

setupBarcodeDetector(barcodeDetector)
}

private fun setupCamera(camera: CameraSource) {
binding.cameraSurfaceView.holder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
surfaceHandler(holder)
}
Expand All @@ -123,7 +129,9 @@ class QrScanningActivity : AppCompatActivity() {
camera.stop()
}
})
}

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,17 @@ class MyProfileActivity : ProfileActivity() {
* A function that displays the dialog
* @param title title of the dialog
* @param hint hint of the text in the dialog
* @param id id of the dialog
* @param textId id of the text in the dialog
* @param name of the dialog
*/
private fun showTextDialog(title: String, hint: String, id: Int, textId: Int, name: String) {
private fun showTextDialog(title: String, hint: String, textId: Int, name: String) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(title)

val input = EditText(this)
input.hint = hint
input.inputType = InputType.TYPE_CLASS_TEXT
input.id = id
input.id = 0

builder.setView(input)
builder.setPositiveButton("OK") { _, _ ->
Expand Down Expand Up @@ -189,7 +188,7 @@ class MyProfileActivity : ProfileActivity() {
val title = "Create $name"
val hint = "Enter new $name"

showTextDialog(title, hint, 0, R.id.username, name)
showTextDialog(title, hint, R.id.username, name)
}
else {
showImageChangeDialog(name)
Expand Down
82 changes: 35 additions & 47 deletions app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ch.sdp.vibester.user.User
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.Query
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.ktx.getValue
import java.util.*
Expand Down Expand Up @@ -90,7 +91,7 @@ class DataGetter @Inject constructor() {
fun updateSubFieldInt(userID: String, newVal: Int, fieldName: String, subFieldName: String, method: String) {
dbUserRef.child(userID).child(fieldName).child(subFieldName)
.get().addOnSuccessListener { t ->
var finalVal = checkValue(t, method, newVal) //newVal: TO DELETE IF TESTS PASS, OTHERWISE ROLL BACK!
val finalVal = checkValue(t, method, newVal) //newVal: TO DELETE IF TESTS PASS, OTHERWISE ROLL BACK!
setSubFieldValue(userID, fieldName, subFieldName, finalVal)
}
}
Expand All @@ -104,7 +105,7 @@ class DataGetter @Inject constructor() {
*/
private fun checkValue(t: DataSnapshot, method: String, newVal: Int): Int {
var finalVal = newVal
if(t.value != null) {
if (t.value != null) {
val previousVal = (t.value as Long?)!!.toInt()
when (method) {
"sum" -> finalVal += previousVal
Expand All @@ -128,7 +129,6 @@ class DataGetter @Inject constructor() {

/**
* This function creates a new room in the database
* @param roomName the name of the new room
* @param callback function to be called when the room has been created
*/
fun createRoom(callback: (PartyRoom, String) -> Unit) {
Expand Down Expand Up @@ -182,7 +182,7 @@ class DataGetter @Inject constructor() {
queryUsers.removeEventListener(this)
}
override fun onCancelled(error: DatabaseError) {
Log.w(ContentValues.TAG, "searchByField:onCancelled", error.toException())
Log.w(TAG, "searchByField:onCancelled", error.toException())
}
})
}
Expand Down Expand Up @@ -212,7 +212,8 @@ class DataGetter @Inject constructor() {
return authenticator.getCurrUser()
}

/**

/**
* This functions fetches the data of the given user from the database
* @param roomID the name of the room to retrieve data from
* @param partyRoomCallback the function to be called when the data of the appropriate room data is available
Expand All @@ -222,49 +223,41 @@ class DataGetter @Inject constructor() {
partyRoomCallback: (PartyRoom, String) -> Unit,
songListCallback: (MutableList<Pair<String, String>>) -> Unit) {

val queryRooms = dbRoomRef
.orderByChild("roomID")
.equalTo(roomID)
dbRoomRef.orderByChild("roomID").equalTo(roomID)
.addValueEventListener(object : ValueEventListener {

queryRooms.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snapshot in dataSnapshot.children) {
val partyRoom: PartyRoom? = snapshot.getValue(PartyRoom::class.java)
if(partyRoom != null) {
val currUserEmail = getCurrentUser()?.email!!
if(!partyRoom.getEmailList().contains(currUserEmail)) {
partyRoom.addUserEmail(currUserEmail)
updateRoomUserList(partyRoom)
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snapshot in dataSnapshot.children) {
val partyRoom: PartyRoom? = snapshot.getValue(PartyRoom::class.java)
if (partyRoom != null) {
val currUserEmail = getCurrentUser()?.email!!
if (!partyRoom.getEmailList().contains(currUserEmail)) {
partyRoom.addUserEmail(currUserEmail)
updateRoomUserList(partyRoom)
}
partyRoomCallback(partyRoom, partyRoom.getRoomID())
}
partyRoomCallback(partyRoom, partyRoom.getRoomID())
}
val snapshotMap = snapshot.value as Map<String, Object>
val songList = snapshotMap["songList"] as List<*>
var gameSongList: MutableList<Pair<String, String>> = mutableListOf()
for (song in songList) {
val tempPair: Map<String, String> = song as Map<String, String>
gameSongList.add(
Pair(
tempPair.getOrDefault("first", ""),
tempPair.getOrDefault("second", "")

val gameSongList: MutableList<Pair<String, String>> = mutableListOf()
for (song in (snapshot.value as Map<String, Object>) ["songList"] as List<*>) {
val tempPair: Map<String, String> = song as Map<String, String>
gameSongList.add(
Pair(
tempPair.getOrDefault("first", ""),
tempPair.getOrDefault("second", "")
)
)
)
}
songListCallback(gameSongList)
}
songListCallback(gameSongList)
}
}
override fun onCancelled(error: DatabaseError) {
Log.w(ContentValues.TAG, "getRoomData:onCancelled", error.toException())
}

override fun onCancelled(error: DatabaseError) {
Copy link

Choose a reason for hiding this comment

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

Similar blocks of code found in 3 locations. Consider refactoring.

Log.w(TAG, "getRoomData:onCancelled", error.toException())
}
})
}

/**
* This functions that updates the field of a room entry
* @param roomID ID of the room
* @param fieldName name of the field to update
* @param value new value to write to the database
*/
/**
* This functions that updates the field of a room entry
* @param roomID ID of the room
Expand All @@ -274,12 +267,7 @@ class DataGetter @Inject constructor() {
fun <T> updateRoomField(roomID: String, fieldName: String, value: T) {
dbRoomRef.child("${roomID}/${fieldName}").setValue(value)
}

/**
* This functions reads the start of the game field and calls the appropriate functions
* @param roomID ID of the room
* @param callback callback to be called when the read value is available
*/

/**
* This functions reads the start of the game field and calls the appropriate functions
* @param roomID ID of the room
Expand All @@ -300,5 +288,5 @@ class DataGetter @Inject constructor() {
}
}
queryRooms.addValueEventListener(startGameListener)
}
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/fragment_layout_game_setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="41dp"
android:layout_marginTop="60dp"
android:layout_marginTop="122dp"
android:layout_marginEnd="45dp"
android:layout_marginBottom="40dp"
android:layout_marginBottom="54dp"
android:fontFeatureSettings="smcp"
android:text="@string/holder_setup_game_type"
android:textAlignment="center"
android:textSize="30sp"
android:textSize="30dp"
android:textStyle="bold" />

<ScrollView
Expand Down Expand Up @@ -208,7 +208,7 @@
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/Multiplayer"
android:textSize="20sp" />
android:textSize="20dp" />

<LinearLayout
android:id="@+id/horilayer_multi"
Expand Down