Skip to content

Commit

Permalink
Preparation delete user fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxcucus committed Apr 13, 2024
1 parent 81932a5 commit d9ac94e
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.planes.android

enum class ApplicationScreens(val value: Int) {
Preferences(0), Game(1), Videos(2), About(3),
Login(4), Logout(5), Register(6), NoRobot(7), CreateGame(8), GameStats(9);
Login(4), Logout(5), Register(6), NoRobot(7),
CreateGame(8), GameStats(9), DeleteUser(10);

companion object {
private val map = ApplicationScreens.values().associateBy(ApplicationScreens::value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface MultiplayerRoundInterface: PlanesRoundInterface {
fun login(username: String, password: String): Observable<Response<LoginResponse>>
fun logout(username: String, userid: String): Observable<Response<LogoutResponse>>

fun deactivateUser(username: String, userid: String): Observable<Response<DeleteUserResponse>>
fun setUserData(username: String, password: String, authToken: String)

fun authTokenExpired(): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.planes.android.deleteuser

import android.content.Context
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.planes.android.ApplicationScreens
import com.planes.android.MainActivity
import com.planes.android.R
import com.planes.android.creategame.CreateGameSettingsGlobal
import com.planes.android.creategame.CreateGameStates
import com.planes.android.databinding.FragmentDeleteUserBinding
import com.planes.multiplayer_engine.MultiplayerRoundJava
import com.planes.multiplayer_engine.commobj.SimpleRequestNotConnectedToGameCommObj
import com.planes.multiplayer_engine.responses.DeleteUserResponse
import io.reactivex.Observable
import retrofit2.Response

class DeleteUserFragment: Fragment() {
public lateinit var binding: FragmentDeleteUserBinding
private lateinit var m_LogoutCommObj: SimpleRequestNotConnectedToGameCommObj<DeleteUserResponse>
public var m_MultiplayerRound = MultiplayerRoundJava()
public var m_CreateGameSettingsService = CreateGameSettingsGlobal()
public lateinit var m_Context: Context

override fun onAttach(context: Context) {
super.onAttach(context)
m_Context = context
m_MultiplayerRound.createPlanesRound()
m_CreateGameSettingsService.createPreferencesService()
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentDeleteUserBinding.inflate(inflater, container, false)
val username = m_MultiplayerRound.getUsername()

binding.settingsData = DeleteUserViewModel(
username,
m_Context
)

if (activity is MainActivity) {
(activity as MainActivity).setActionBarTitle(getString(R.string.delete_user))
(activity as MainActivity).setCurrentFragmentId(ApplicationScreens.DeleteUser)
(activity as MainActivity).updateOptionsMenu()
}

val deleteUserButton = binding.deleteuser

deleteUserButton.setOnClickListener { performDeleteUser() }
if (username.isEmpty())
deleteUserButton.isEnabled = false

return binding.root
}

override fun onGetLayoutInflater(savedInstanceState: Bundle?): LayoutInflater {
if (activity is MainActivity)
return super.onGetLayoutInflater(savedInstanceState)

val inflater = super.onGetLayoutInflater(savedInstanceState)
val contextThemeWrapper: Context = ContextThemeWrapper(requireContext(), R.style.MyAppTheme)
return inflater.cloneInContext(contextThemeWrapper)
}

private fun createObservable() : Observable<Response<DeleteUserResponse>> {
return m_MultiplayerRound.deactivateUser(m_MultiplayerRound.getUsername(), m_MultiplayerRound.getUserId().toString())
}
public fun performDeleteUser() {

//TODO: update instrumented tests
m_LogoutCommObj = SimpleRequestNotConnectedToGameCommObj(::createObservable,
getString(R.string.error_deleteuser), getString(R.string.unknownerror), getString(R.string.validation_user_not_loggedin),
::receiveDeleteUserStatus, ::finalizeDeleteUserSuccessful, requireActivity())

m_LogoutCommObj.makeRequest()
}

fun receiveDeleteUserStatus(response: DeleteUserResponse): String {
var errorString = ""
if (!response.m_Deactivated)
errorString = getString(R.string.error_deleteuser)
return errorString
}

fun finalizeDeleteUserSuccessful() {
m_MultiplayerRound.setUserData("", "", "")
m_MultiplayerRound.resetGameData()
m_MultiplayerRound.initRound()

m_CreateGameSettingsService.createGameState = CreateGameStates.NotSubmitted
m_CreateGameSettingsService.gameName = ""

if (!this::binding.isInitialized)
return

binding.deleteuser.isEnabled = false
binding.settingsData!!.m_LoginStatus = m_Context.resources.getString(R.string.nouser)
binding.settingsData!!.m_Username = ""
binding.invalidateAll()

if (activity is MainActivity)
(activity as MainActivity).setUsernameDrawerMenuMultiplayer()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.planes.android.deleteuser

import android.content.Context
import androidx.lifecycle.ViewModel
import com.planes.android.R
import java.lang.ref.WeakReference

class DeleteUserViewModel(var username: String, context: Context) : ViewModel() {

var m_Username: String
var m_LoginStatus: String
var m_Context: WeakReference<Context>

init {
m_Context = WeakReference(context)
m_Username = username
m_LoginStatus = if (m_Username.isEmpty())
m_Context.get()!!.resources.getString(R.string.nouser)
else
m_Context.get()!!.resources.getString(R.string.userloggedin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import com.planes.android.creategame.CreateGameSettingsGlobal
import com.planes.android.creategame.CreateGameStates
import com.planes.android.databinding.FragmentLogoutBinding
import com.planes.multiplayer_engine.MultiplayerRoundJava
import com.planes.multiplayer_engine.commobj.SimpleRequestCommObj
import com.planes.multiplayer_engine.commobj.SimpleRequestNotConnectedToGameCommObj
import com.planes.multiplayer_engine.responses.LoginResponse
import com.planes.multiplayer_engine.responses.LogoutResponse
import io.reactivex.Observable
import retrofit2.Response
Expand Down Expand Up @@ -77,7 +75,6 @@ class LogoutFragment: Fragment() {
}
public fun performLogout() {

//TODO: update instrumented tests
m_LogoutCommObj = SimpleRequestNotConnectedToGameCommObj(::createObservable,
getString(R.string.error_logout), getString(R.string.unknownerror), getString(R.string.validation_user_not_loggedin),
::receiveLogoutStatus, ::finalizeLogoutSuccessful, requireActivity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface MultiplayerCommApi {
@Headers("Content-Type: application/json")
fun logout(@Header("Authorization") authorization: String, @Body user: LogoutRequest): Observable<retrofit2.Response<LogoutResponse>>

@POST("users/deactivate_user")
@Headers("Content-Type: application/json")
fun deactivateUser(@Header("Authorization") authorization: String, @Body user: DeleteUserRequest): Observable<retrofit2.Response<DeleteUserResponse>>

@POST("users/registration_request")
@Headers("Content-Type: application/json")
fun register(@Body user: RegistrationRequest): Observable<retrofit2.Response<RegistrationResponse>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class MultiplayerRound(rowNo: Int, colNo: Int, planeNo: Int) {
return m_Service.logout(m_UserData.authToken, LogoutRequest(userid, username))
}

fun deactivateUser(username: String, userid: String): Observable<Response<DeleteUserResponse>> {
return m_Service.deactivateUser(m_UserData.authToken, DeleteUserRequest(userid, username))
}
fun setUserData(username: String, password: String, authToken: String) {
if (m_UserData.userName != username || m_UserData.password != password) {
//login with new credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class MultiplayerRoundJava : MultiplayerRoundInterface {
return global_Round!!.logout(username, userid)
}

override fun deactivateUser(username: String, userid: String): Observable<Response<DeleteUserResponse>> {
return global_Round!!.deactivateUser(username, userid)
}

override fun setUserData(username: String, password: String, authToken: String) {
global_Round!!.setUserData(username, password, authToken)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.planes.multiplayer_engine.requests

import com.google.gson.annotations.SerializedName

class DeleteUserRequest (
@SerializedName("userId")
override var m_UserId: String,

@SerializedName("userName")
override var m_UserName: String
): BasisRequest(m_UserName, m_UserId);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.planes.multiplayer_engine.responses

import com.google.gson.annotations.SerializedName

class DeleteUserResponse (
@SerializedName("deactivated")
var m_Deactivated: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="settingsData" type="com.planes.android.deleteuser.DeleteUserViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<RelativeLayout
android:id="@+id/deleteuser_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<TextView
android:id="@+id/deleteuser_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="20dp"
style="@style/CustomTextViewStyle"
android:text="@={settingsData.m_LoginStatus}"/>
<TextView
android:id="@+id/deleteuser_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/deleteuser_state"
android:textSize="20dp"
android:layout_marginLeft="10dp"
style="@style/CustomTextViewStyle"
android:text="@={settingsData.m_Username}" />
<Button
android:id="@+id/deleteuser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/deleteuser_username"
android:layout_alignRight="@+id/deleteuser_username"
style="@style/ButtonColor"
android:text="@string/logout"/>
</RelativeLayout>
</RelativeLayout>
</layout>
2 changes: 2 additions & 0 deletions kotlin/PlanesAndroid/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@
<string name="helptext_game_1_opponent">Touch on the opponent\'s board to guess where the opponent\'s planes are.</string>

<string name="error_logout"> Error when logging out.</string>
<string name="delete_user"> Delete User</string>
<string name="error_deleteuser"> Error when deleting user.</string>

<!-- The class name to the ScrollingChildBehavior required for AppBarLayout -->
<string name="appbar_scrolling_view_behavior" translatable="false">
Expand Down

0 comments on commit d9ac94e

Please sign in to comment.