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

Implement UserSessionManager into Profile #435

Merged
merged 1 commit into from
Nov 24, 2023
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,6 +2,8 @@ package com.cmpe451.resq.data.manager

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class UserSessionManager(appContext: Context) {
companion object {
Expand All @@ -12,19 +14,26 @@ class UserSessionManager(appContext: Context) {
private const val USER_SURNAME = "USER_SURNAME"
private const val USER_EMAIL = "USER_EMAIL"
private const val USER_ROLES = "USER_ROLES"
private const val SELECTED_ROLE = "SELECTED_ROLE"
private const val KEY_IS_LOGGED_IN = "IS_LOGGED_IN"
}

private var pref: SharedPreferences = appContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
private var editor: SharedPreferences.Editor = pref.edit()

fun createLoginSession(token: String, userId: Int, userName: String, userSurname: String, userEmail: String, userRoles: String) {
fun createLoginSession(token: String, userId: Int, userName: String, userSurname: String, userEmail: String, userRoles: List<String>) {
val gson = Gson()
val rolesJson = gson.toJson(userRoles)

editor.putString(JWT_TOKEN, token)
editor.putInt(USER_ID, userId)
editor.putString(USER_NAME, userName)
editor.putString(USER_SURNAME, userSurname)
editor.putString(USER_EMAIL, userEmail)
editor.putString(USER_ROLES, userRoles)
editor.putString(USER_ROLES, rolesJson)
if (userRoles.isNotEmpty()) {
editor.putString(SELECTED_ROLE, userRoles.last())
}
editor.putBoolean(KEY_IS_LOGGED_IN, true)
editor.apply()
}
Expand All @@ -39,8 +48,19 @@ class UserSessionManager(appContext: Context) {

fun getUserEmail(): String? = pref.getString(USER_EMAIL, null)

fun getUserRoles(): String? = pref.getString(USER_ROLES, null)
fun getUserRoles(): List<String> {
val gson = Gson()
val rolesJson = pref.getString(USER_ROLES, null) ?: return emptyList()
val type = object : TypeToken<List<String>>() {}.type
return gson.fromJson(rolesJson, type)
}

fun getSelectedRole(): String? = pref.getString(SELECTED_ROLE, null)

fun setSelectedRole(role: String) {
editor.putString(SELECTED_ROLE, role)
editor.apply()
}
fun isLoggedIn(): Boolean = pref.getBoolean(KEY_IS_LOGGED_IN, false)

fun logout() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.cmpe451.resq.data.models

open class ProfileData(
data class ProfileData(
var name: String?,
var surname: String?,
var dateOfBirth: String?,
var role: String,
var address: String?
){}
var email: String?,
var roles: List<String>?,
var selectedRole: String?
)


Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.cmpe451.resq.data.remote

import androidx.compose.ui.res.stringResource
import android.content.Context
import com.cmpe451.resq.data.Constants
import com.cmpe451.resq.data.manager.UserSessionManager
import com.cmpe451.resq.data.models.ProfileData
import retrofit2.Response
import retrofit2.Retrofit
Expand All @@ -22,9 +23,10 @@ data class UserInfoResponse(
val roles: List<String>
)

class ProfileRepository {
class ProfileRepository(appContext: Context) {

private val profileService: ProfileService
private val userSessionManager: UserSessionManager = UserSessionManager(appContext)

init {
val retrofit = Retrofit.Builder()
Expand All @@ -35,40 +37,19 @@ class ProfileRepository {
profileService = retrofit.create(ProfileService::class.java)
}

suspend fun getUserData(): ProfileData{
val response = profileService.getUserInfo(
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhbGl0cGMyNTI1QGdtYWlsLmNvbSIsImlhdCI6MTY5ODcwMzU5MiwiZXhwIjoxNjk4Nzg5OTkyfQ.FLyFCITkyCEYOuMBdaSHWCl5V1MrLSl5Yt5Y2L2WAlc",
"Victim"
)
suspend fun getUserData(): ProfileData {
val token = userSessionManager.getUserToken() ?: ""
val selectedRole = userSessionManager.getSelectedRole() ?: ""

val response = profileService.getUserInfo("Bearer $token", selectedRole)

return ProfileData(
name = response.body()?.name,
surname = response.body()?.surname,
dateOfBirth = "29/05/1993",
role = "Victim",
address = ""
//address = "123 Main Street, apt 4B San Diego CA, 91911"
email = response.body()?.email,
roles = response.body()?.roles,
selectedRole = selectedRole
)
}

// This is a placeholder response
/*
return ProfileData(
name = "Responder",
surname = "Responderoğlu",
dateOfBirth = "01/01/1990",
role = "Responder" ,
address = "123 Main Street, apt 4B San Diego CA, 91911"
//address = null
)
*/

/*
return ProfileData(
name = "Harun",
surname = "Ergen",
dateOfBirth = "18/10/2000",
role = "Victim" ,
//address = "123 Main Street, apt 4B San Diego CA, 91911"
address = null)
*/
}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ class LoginUseCase() {
}
private fun saveLoginResponse(loginResponse: LoginResponse, appContext: Context) {
val userSessionManager = UserSessionManager(appContext)
val gson = Gson()
val rolesJson = gson.toJson(loginResponse.roles)

userSessionManager.createLoginSession(
token = loginResponse.jwt,
userId = loginResponse.id,
userName = loginResponse.name,
userSurname = loginResponse.surname,
userEmail = loginResponse.email,
userRoles = rolesJson
userRoles = loginResponse.roles
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ import com.cmpe451.resq.ui.theme.ResourceColor
import com.cmpe451.resq.utils.NavigationItem

@Composable
fun ProfileScreen(userId: Int, navController: NavController) {
fun ProfileScreen(navController: NavController, appContext: Context) {
val viewModel: ProfileViewModel = viewModel()
LaunchedEffect(userId) {

viewModel.getUserData(userId)
}
viewModel.getUserData(appContext)

val profileData by viewModel.profile
when (profileData) {
Expand All @@ -55,11 +53,14 @@ fun ProfileScreen(userId: Int, navController: NavController) {
Text("Loading...")
}
else -> {
if (profileData!!.role == "Victim" || profileData!!.role == "Responder") {
Profile(profileData = profileData!!, navController = navController)
} else {
// Handle other roles or unknown roles
Text("Unknown Role")
val userRoles = profileData!!.roles
if (userRoles != null) {
if (userRoles.contains("VICTIM") || userRoles.contains("RESPONDER")) {
Profile(profileData = profileData!!, navController = navController)
} else {
// Handle other roles or unknown roles
Text("Unknown Role")
}
}
}
}
Expand All @@ -70,9 +71,9 @@ fun ProfileScreen(userId: Int, navController: NavController) {
fun Profile(profileData:ProfileData, navController: NavController) {
val name = profileData.name
val surname = profileData.surname
val dateOfBirth = profileData.dateOfBirth
val role = profileData.role
val address = profileData.address
val email = profileData.email
val selectedRole = profileData.selectedRole

Column {
Surface(
modifier = Modifier.fillMaxWidth()
Expand All @@ -85,7 +86,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
) {
Column {
Text(
text = "User Profile",
text = "Account",
style = TextStyle(
fontSize = 25.sp,
color = Color(0xFF224957),
Expand Down Expand Up @@ -139,7 +140,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
// .clip(CircleShape) // Clip to a circular shape
// )

Spacer(modifier = Modifier.width(16.dp)) // Add space between photo and info
Spacer(modifier = Modifier.width(16.dp))

Column(
modifier = Modifier
Expand All @@ -155,7 +156,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
},
style = TextStyle(
fontSize = 20.sp,
color = Color(0xFF224957) // Set the font color to #224957
color = Color(0xFF224957)
)
)
Text(
Expand All @@ -167,7 +168,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
},
style = TextStyle(
fontSize = 20.sp,
color = Color(0xFF224957) // Set the font color to #224957
color = Color(0xFF224957)
)
)
Text(
Expand All @@ -179,7 +180,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
},
style = TextStyle(
fontSize = 20.sp,
color = Color(0xFF224957) // Set the font color to #224957
color = Color(0xFF224957)
)
)
Text(
Expand All @@ -191,7 +192,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
},
style = TextStyle(
fontSize = 20.sp,
color = Color(0xFF224957) // Set the font color to #224957
color = Color(0xFF224957)
)
)
if (role == "Responder") {
Expand Down Expand Up @@ -236,7 +237,8 @@ fun Profile(profileData:ProfileData, navController: NavController) {
) {
Text(text = "My Requests")
}
} else if (role == "Responder") {
}
else if (role == "Responder") {
// "My Resources" button
Button(
onClick = {
Expand All @@ -263,6 +265,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
Text(text = "My Tasks")
}
}

// "Edit Profile" button
Button(
onClick = {
Expand All @@ -281,7 +284,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
"✏️",
fontSize = 20.sp
)
Spacer(modifier = Modifier.width(8.dp)) // Add space between the icon and the button text
Spacer(modifier = Modifier.width(8.dp))
Text(text = "Edit Profile")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmpe451.resq.viewmodels

import android.content.Context
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
Expand All @@ -10,16 +11,17 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class ProfileViewModel() : ViewModel() {
private val profileRepository = ProfileRepository()
private var _profileData: MutableState<ProfileData?> = mutableStateOf(null)
val profile get() = _profileData
private val errorMessage = MutableStateFlow<String?>(null)

fun getUserData(userId: Int) {
fun getUserData(appContext: Context) {
val profileRepository = ProfileRepository(appContext)

viewModelScope.launch {
try {
val data = profileRepository.getUserData()
_profileData.value = data // Update the mutable state
_profileData.value = data
} catch (e: Exception) {
errorMessage.value = e.message
}
Expand Down