Skip to content

Commit

Permalink
Merge pull request #423 from bounswe/feature/mobile-370
Browse files Browse the repository at this point in the history
Feature/mobile 370
  • Loading branch information
HarunErgen authored Nov 24, 2023
2 parents 784dd13 + 7f90b6c commit 59356fa
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import android.content.Context
import android.content.SharedPreferences

class UserSessionManager(context: Context) {
companion object {
private const val PREF_NAME = "UserSessionPref"
private const val KEY_IS_LOGGED_IN = "isLoggedIn"
private const val KEY_USER_TOKEN = "userToken"
// Add other user-related keys
}

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

fun createLoginSession(token: String) {
editor.putBoolean(KEY_IS_LOGGED_IN, true)
editor.putString(KEY_USER_TOKEN, token)
// Store other user info if needed
editor.apply()
}

fun getUserToken(): String? = pref.getString(KEY_USER_TOKEN, null)

fun isLoggedIn(): Boolean = pref.getBoolean(KEY_IS_LOGGED_IN, false)

// Implement logout or other session management functions
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.cmpe451.resq.data.models


data class User(
val id: Integer,
val name: String,
val surname: String,
val email: String,
val password: String
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.cmpe451.resq.domain

import android.content.Context
import android.provider.Settings.Global.putInt
import android.provider.Settings.Global.putString
import androidx.compose.ui.platform.LocalContext
import com.cmpe451.resq.data.remote.AuthApi
import com.cmpe451.resq.data.remote.LoginRequest
import com.cmpe451.resq.data.remote.LoginResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)

val DeepBlue = Color(0xff224957)
val LightGreen = Color(0xff20df7f)
val LightGreen = Color(0xff20df7f)

val ResourceColor = Color(0xFF397FE7)
val RequestColor = Color(0xFFB356AF)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmpe451.resq.ui.views.screens

import android.content.Context
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
Expand All @@ -25,6 +26,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
Expand Down Expand Up @@ -157,7 +159,7 @@ fun LoginScreen(navController: NavController) {

// Login button
Button(
onClick = { viewModel.login(email, password, navController) },
onClick = { viewModel.login(email, password, navController, context) },
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors(
containerColor = LightGreen
Expand Down Expand Up @@ -214,8 +216,6 @@ fun LoginScreen(navController: NavController) {
)
}
}

SnackbarHost(hostState = snackbarHostState)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
Expand All @@ -20,16 +19,20 @@ import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.cmpe451.resq.R
import com.cmpe451.resq.ui.theme.RequestColor
import com.cmpe451.resq.ui.theme.ResourceColor
import com.cmpe451.resq.utils.NavigationItem
import com.cmpe451.resq.viewmodels.MapViewModel

Expand All @@ -43,8 +46,16 @@ fun MapScreen(navController: NavController) {
.fillMaxSize()
.padding(16.dp)
) {
AddRequestButton {
navController.navigate(NavigationItem.Request.route)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
AddRequestButton {
navController.navigate(NavigationItem.Request.route)
}
AddResourceButton {
navController.navigate(NavigationItem.Resource.route)
}
}
Spacer(modifier = Modifier.height(16.dp))
SearchBar(viewModel)
Expand All @@ -53,7 +64,6 @@ fun MapScreen(navController: NavController) {
contentDescription = "Mock Map",
modifier = Modifier.fillMaxSize()
)

}
}
}
Expand All @@ -74,20 +84,49 @@ fun AddRequestButton(onClick: () -> Unit) {
onClick = onClick,
shape = RoundedCornerShape(20.dp),
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
.width(160.dp)
.padding(8.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(0xFFB356AF)
backgroundColor = RequestColor
)
) {
Row(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
Text("Add Request", color = Color.White)
Spacer(modifier = Modifier.width(8.dp))
Icon(Icons.Default.Add, contentDescription = "Add Request", tint = Color.White)
}
Icon(
Icons.Default.Add,
contentDescription = "Add Request",
tint = Color.White
)
Spacer(modifier = Modifier.width(8.dp))
Text(
"Add Request",
color = Color.White,
textAlign = TextAlign.Center
)
}
}

@Composable
fun AddResourceButton(onClick: () -> Unit) {
Button(
onClick = onClick,
shape = RoundedCornerShape(20.dp),
modifier = Modifier
.width(160.dp)
.padding(8.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = ResourceColor
)
) {
Icon(
Icons.Default.AddCircle,
contentDescription = "Add Resource",
tint = Color.White
)
Spacer(modifier = Modifier.width(8.dp))
Text(
"Add Resource",
color = Color.White,
textAlign = TextAlign.Center

)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import androidx.navigation.NavController
import com.cmpe451.resq.viewmodels.ProfileViewModel
import com.cmpe451.resq.data.models.ProfileData
import androidx.compose.runtime.LaunchedEffect
import com.cmpe451.resq.ui.theme.ResourceColor
import com.cmpe451.resq.utils.NavigationItem

@Composable
fun ProfileScreen(userId: Int, navController: NavController) {
Expand Down Expand Up @@ -237,9 +239,9 @@ fun Profile(profileData:ProfileData, navController: NavController) {
// "My Resources" button
Button(
onClick = {
// Add Resources action here
navController.navigate(NavigationItem.Resource.route)
},
colors = ButtonDefaults.buttonColors(Color(0xFF397FE7)),
colors = ButtonDefaults.buttonColors(ResourceColor),
modifier = Modifier
.fillMaxWidth()
.width(200.dp)
Expand All @@ -249,7 +251,7 @@ fun Profile(profileData:ProfileData, navController: NavController) {
}
Button(
onClick = {
// Add Tasks action here
navController.navigate(NavigationItem.Task.route)
},
colors = ButtonDefaults.buttonColors(Color(0xFFE7A139)),
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package com.cmpe451.resq.utils

enum class NavigationItem(val route: String) {
Login (route = "login"),

Register (route = "register"),

Profile(route = "profile"),

Map(route = "map"),

Settings(route = "settings"),

Notifications(route = "notifications"),

Request(route = "request"),

Resource(route = "resource");

Resource(route = "resource"),
Task(route = "task");
OngoingTasks(route = "ongoingTasks");

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cmpe451.resq.viewmodels

import UserSessionManager
import android.content.Context
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
Expand All @@ -20,13 +22,18 @@ class LoginViewModel() : ViewModel() {
private val _errorMessage = mutableStateOf<String?>(null)
val errorMessage: State<String?> = _errorMessage

fun login(email: String, password: String, navController: NavController) {
fun login(email: String, password: String, navController: NavController, context: Context) {
if (validateLoginInputs(email, password)) {
viewModelScope.launch {
val result = loginUseCase.execute(email, password)
if (result.isSuccess) {
_loginResponse.value = result.getOrNull()
_errorMessage.value = null

// Create session
val sessionManager = UserSessionManager(context)
_loginResponse.value?.jwt?.let { sessionManager.createLoginSession(it) }

navController.navigate(NavigationItem.Map.route)
} else {
_errorMessage.value = result.exceptionOrNull()?.message
Expand All @@ -35,6 +42,7 @@ class LoginViewModel() : ViewModel() {
}
}


fun validateLoginInputs(email: String, password: String): Boolean {
when {
email.isBlank() -> {
Expand Down

0 comments on commit 59356fa

Please sign in to comment.