Skip to content

Commit 649807f

Browse files
committed
added log out options and integrated profile with firebase
1 parent ec46b10 commit 649807f

File tree

9 files changed

+168
-27
lines changed

9 files changed

+168
-27
lines changed

app/src/main/java/com/example/eventtracker/ui/navigation/EventTrackerApp.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fun EventTrackerApp(
2424
navController: NavHostController,
2525
onBottomBarVisibilityChanged: (Boolean) -> Unit,
2626
homeScreenViewModel: HomeScreenViewModel,
27-
signInViewModel: SignInViewModel
27+
signInViewModel: SignInViewModel,
2828
) {
2929
val scope = rememberCoroutineScope()
3030
NavHost(
@@ -36,7 +36,13 @@ fun EventTrackerApp(
3636
onBottomBarVisibilityChanged(false)
3737
SignInScreen(
3838
viewModel = signInViewModel,
39-
navigateToHome = { navController.navigate(HomeScreen) })
39+
navigateToHome = {
40+
navController.navigate(HomeScreen) {
41+
popUpTo(LogInScreen) {
42+
inclusive = true
43+
}
44+
}
45+
})
4046
}
4147

4248
composable<HomeScreen> {
@@ -77,7 +83,10 @@ fun EventTrackerApp(
7783
}
7884
composable<ProfileScreen> {
7985
onBottomBarVisibilityChanged(true)
80-
com.example.eventtracker.ui.profile.ProfileScreen(name = "Yash Aggarwal")
86+
com.example.eventtracker.ui.profile.ProfileScreen(navigateToLogin = {
87+
signInViewModel.signOut()
88+
navController.navigate(LogInScreen)
89+
})
8190
}
8291
composable<PostEventScreen> {
8392
onBottomBarVisibilityChanged(true)

app/src/main/java/com/example/eventtracker/ui/postNewEvent/PostNewEventScreen.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ fun PostNewEventBody(
246246

247247
}
248248

249+
fun getMonthName(month: Int): String {
250+
return when (month) {
251+
1 -> "Jan"
252+
2 -> "Feb"
253+
3 -> "Mar"
254+
4 -> "Apr"
255+
5 -> "May"
256+
6 -> "Jun"
257+
7 -> "Jul"
258+
8 -> "Aug"
259+
9 -> "Sep"
260+
10 -> "Oct"
261+
11 -> "Nov"
262+
12 -> "Dec"
263+
else -> ""
264+
}
265+
}
249266

250267
@OptIn(ExperimentalMaterial3Api::class)
251268
@Composable
@@ -282,7 +299,7 @@ fun pickDate(): String {
282299
{ _: DatePicker, mYear: Int, mMonth: Int, mDayOfMonth: Int ->
283300
// mDate.value = "$mDayOfMonth/${mMonth+1}/$mYear"
284301
// year-month-day
285-
mDate.value = "$mYear-${String.format("%02d", mMonth + 1)}-$mDayOfMonth"
302+
mDate.value = "$mDayOfMonth ${getMonthName(mMonth + 1)} $mYear"
286303
}, mYear, mMonth, mDay
287304
)
288305
// correct code

app/src/main/java/com/example/eventtracker/ui/profile/ProfileScreen.kt

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ package com.example.eventtracker.ui.profile
33
import androidx.compose.foundation.clickable
44
import androidx.compose.foundation.gestures.Orientation
55
import androidx.compose.foundation.gestures.scrollable
6+
import androidx.compose.foundation.layout.Arrangement
67
import androidx.compose.foundation.layout.Column
78
import androidx.compose.foundation.layout.Row
89
import androidx.compose.foundation.layout.Spacer
10+
import androidx.compose.foundation.layout.fillMaxWidth
911
import androidx.compose.foundation.layout.height
1012
import androidx.compose.foundation.layout.padding
1113
import androidx.compose.foundation.layout.size
1214
import androidx.compose.foundation.layout.width
15+
import androidx.compose.foundation.lazy.LazyColumn
16+
import androidx.compose.foundation.lazy.itemsIndexed
1317
import androidx.compose.foundation.rememberScrollState
1418
import androidx.compose.foundation.shape.CircleShape
1519
import androidx.compose.foundation.shape.RoundedCornerShape
1620
import androidx.compose.foundation.verticalScroll
21+
import androidx.compose.material3.Button
22+
import androidx.compose.material3.ButtonDefaults
1723
import androidx.compose.material3.CenterAlignedTopAppBar
1824
import androidx.compose.material3.ExperimentalMaterial3Api
1925
import androidx.compose.material3.MaterialTheme
2026
import androidx.compose.material3.Text
2127
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.collectAsState
2229
import androidx.compose.ui.Alignment
2330
import androidx.compose.ui.Modifier
2431
import androidx.compose.ui.draw.clip
32+
import androidx.compose.ui.focus.focusModifier
2533
import androidx.compose.ui.graphics.Color
2634
import androidx.compose.ui.layout.ContentScale
2735
import androidx.compose.ui.res.painterResource
@@ -30,11 +38,14 @@ import androidx.compose.ui.text.style.TextOverflow
3038
import androidx.compose.ui.tooling.preview.Preview
3139
import androidx.compose.ui.unit.dp
3240
import androidx.compose.ui.unit.sp
41+
import androidx.hilt.navigation.compose.hiltViewModel
3342
import coil.compose.AsyncImage
3443
import com.example.eventtracker.R
44+
import com.example.eventtracker.model.EventData
3545

3646
@Composable
37-
fun ProfileScreen(modifier: Modifier = Modifier, name: String, email: String = "") {
47+
fun ProfileScreen(modifier: Modifier = Modifier,navigateToLogin: () -> Unit) {
48+
val viewModel: ProfileScreenViewModel = hiltViewModel()
3849
Column(
3950
modifier = Modifier
4051
.verticalScroll(rememberScrollState())
@@ -49,11 +60,15 @@ fun ProfileScreen(modifier: Modifier = Modifier, name: String, email: String = "
4960
error = painterResource(id = R.drawable.default_profile_image),
5061
)
5162
Spacer(modifier = Modifier.height(16.dp))
52-
Text(text = name, fontWeight = FontWeight.Bold, style = MaterialTheme.typography.titleLarge)
63+
Text(
64+
text = viewModel.userName.collectAsState().value,
65+
fontWeight = FontWeight.Bold,
66+
style = MaterialTheme.typography.titleLarge
67+
)
5368

5469
Spacer(modifier = Modifier.height(8.dp))
5570
Text(
56-
text = "USN - 1DS23IS188",
71+
text = "USN - ${viewModel.userUsn.collectAsState().value}",
5772
style = MaterialTheme.typography.bodyLarge,
5873
color = Color.Gray
5974
)
@@ -75,36 +90,57 @@ fun ProfileScreen(modifier: Modifier = Modifier, name: String, email: String = "
7590
fontWeight = FontWeight.Bold,
7691
fontSize = 20.sp
7792
)
78-
EventList()
93+
EventList(events = viewModel.userEvents.collectAsState().value)
94+
Spacer(modifier = Modifier.height(16.dp))
95+
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
96+
Button(
97+
onClick = {
98+
// viewModel.logOut()
99+
navigateToLogin()
100+
},
101+
modifier = Modifier.fillMaxWidth(0.87f),
102+
shape = MaterialTheme.shapes.small,
103+
colors = ButtonDefaults.buttonColors(Color.Black)
104+
) {
105+
Text(text = "Logout")
106+
}
107+
108+
}
79109
}
80110
}
81111

82112
@Composable
83-
fun EventList(modifier: Modifier = Modifier) {
84-
EventItem(eventName = "Introduction to Android Development")
85-
EventItem(eventName = "Introduction to Android Development")
86-
EventItem(eventName = "Introduction to Android Development")
87-
EventItem(eventName = "Introduction to Android Development")
88-
EventItem(eventName = "Introduction to Android Development")
113+
fun EventList(modifier: Modifier = Modifier, events: List<EventData>) {
114+
Column() {
115+
events.forEachIndexed { index, event ->
116+
EventItem(event = event)
117+
}
118+
}
119+
// EventItem(eventName = "Introduction to Android Development")
120+
// EventItem(eventName = "Introduction to Android Development")
121+
// EventItem(eventName = "Introduction to Android Development")
122+
// EventItem(eventName = "Introduction to Android Development")
123+
// EventItem(eventName = "Introduction to Android Development")
89124

90125
}
91126

92127
@Composable
93-
fun EventItem(modifier: Modifier = Modifier,eventName:String) {
94-
Row(modifier = modifier.padding(vertical = 16.dp).clickable { /*TODO*/ }) {
128+
fun EventItem(modifier: Modifier = Modifier, event: EventData) {
129+
Row(modifier = modifier
130+
.padding(vertical = 16.dp)
131+
.clickable { /*TODO*/ }) {
95132
AsyncImage(
96-
model = null,
133+
model = event.image,
97134
contentDescription = "Event image",
98135
modifier = modifier
99136
.clip(MaterialTheme.shapes.medium)
100137
.height(70.dp)
101-
.width(100.dp)
102-
,
138+
.width(100.dp),
103139
error = painterResource(id = R.drawable.default_image),
104140
contentScale = ContentScale.Crop
105141
)
106142
Spacer(modifier = Modifier.width(16.dp))
107-
Text(text = eventName , overflow = TextOverflow.Ellipsis,maxLines = 2, fontSize = 18.sp)
143+
Text(text = event.name, overflow = TextOverflow.Ellipsis, maxLines = 2, fontSize = 18.sp)
108144
}
109145
}
110146

@@ -114,8 +150,8 @@ fun ProfileScreenTopBar(modifier: Modifier = Modifier) {
114150
CenterAlignedTopAppBar(title = { Text(text = "Profile", fontWeight = FontWeight.Bold) })
115151
}
116152

117-
@Preview
118-
@Composable
119-
fun ProfileScreenPreview() {
120-
ProfileScreen(name = "James")
121-
}
153+
//@Preview
154+
//@Composable
155+
//fun ProfileScreenPreview() {
156+
// ProfileScreen(name = "James")
157+
//}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example.eventtracker.ui.profile
2+
3+
import android.util.Log
4+
import androidx.lifecycle.ViewModel
5+
import com.example.eventtracker.model.EventData
6+
import com.example.eventtracker.model.UserData
7+
import com.google.firebase.auth.FirebaseAuth
8+
import com.google.firebase.firestore.FirebaseFirestore
9+
import com.google.firebase.firestore.ListenerRegistration
10+
import dagger.hilt.android.lifecycle.HiltViewModel
11+
import kotlinx.coroutines.flow.MutableStateFlow
12+
import kotlinx.coroutines.flow.asStateFlow
13+
import kotlinx.coroutines.tasks.await
14+
import javax.inject.Inject
15+
16+
@HiltViewModel
17+
class ProfileScreenViewModel @Inject constructor(
18+
private val auth: FirebaseAuth,
19+
private val db: FirebaseFirestore
20+
) : ViewModel() {
21+
val currentUser = UserData()
22+
private val _userName = MutableStateFlow(currentUser.name)
23+
val userName = _userName.asStateFlow()
24+
private val _userEmail = MutableStateFlow(currentUser.email)
25+
val userEmail = _userEmail.asStateFlow()
26+
private val _userUsn = MutableStateFlow(currentUser.collegeId)
27+
val userUsn = _userUsn.asStateFlow()
28+
private val _userEvents = MutableStateFlow(listOf<EventData>())
29+
val userEvents = _userEvents.asStateFlow()
30+
var eventListener: ListenerRegistration? = null
31+
32+
init {
33+
db.collection("users")
34+
.document(auth.currentUser!!.uid)
35+
.get()
36+
.addOnSuccessListener {
37+
_userName.value = it.get("name") as String
38+
_userEmail.value = it.get("email") as String
39+
_userUsn.value = it.get("collegeId") as String
40+
}
41+
val user = auth.currentUser
42+
if (user != null) {
43+
eventListener = db.collection("users")
44+
.document(user.uid)
45+
.collection("Hosted Events")
46+
.addSnapshotListener { value, error ->
47+
if (error != null) {
48+
return@addSnapshotListener
49+
}
50+
if (value != null) {
51+
val events = value.toObjects(EventData::class.java)
52+
_userEvents.value = events
53+
}
54+
}
55+
56+
}
57+
}
58+
fun logOut() {
59+
auth.signOut()
60+
Log.d("TAG", auth.currentUser.toString())
61+
}
62+
}

app/src/main/java/com/example/eventtracker/ui/signIn/SignInScreen.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.eventtracker.ui.signIn
22

3+
import android.util.Log
34
import android.widget.Toast
45
import androidx.compose.foundation.Image
56
import androidx.compose.foundation.clickable
@@ -29,6 +30,7 @@ import androidx.compose.material3.Icon
2930
import androidx.compose.material3.OutlinedTextField
3031
import androidx.compose.material3.Text
3132
import androidx.compose.runtime.Composable
33+
import androidx.compose.runtime.LaunchedEffect
3234
import androidx.compose.runtime.collectAsState
3335
import androidx.compose.runtime.getValue
3436
import androidx.compose.runtime.mutableStateOf
@@ -61,6 +63,13 @@ fun SignInScreen(
6163
navigateToSignUp: () -> Unit = {},
6264
navigateToSignIn: () -> Unit = {},
6365
) {
66+
LaunchedEffect(Unit) {
67+
if(viewModel.checkIfLoggedIn()){
68+
navigateToHome()
69+
Log.d("TAG", "SignInScreen: ${viewModel.checkIfLoggedIn()}")
70+
}
71+
}
72+
6473
val context = LocalContext.current
6574
val uiState by viewModel.uiState.collectAsState()
6675
SignInScreenContent(

app/src/main/java/com/example/eventtracker/ui/signIn/SignInViewModel.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SignInViewModel @Inject constructor(
2323
private val _uiState = MutableStateFlow(SignInUiState())
2424
val uiState:StateFlow<SignInUiState> = _uiState.asStateFlow()
2525
var inProcess by mutableStateOf(false)
26-
var isSignedIn by mutableStateOf(false)
26+
2727
var currentUser = auth.currentUser
2828
fun updateEmail(email:String){
2929
_uiState.value = _uiState.value.copy(email = email)
@@ -52,7 +52,7 @@ class SignInViewModel @Inject constructor(
5252
.addOnSuccessListener {
5353
currentUser = auth.currentUser
5454
inProcess = false
55-
isSignedIn = true
55+
5656
onSuccess()
5757
}
5858
.addOnFailureListener{
@@ -95,4 +95,12 @@ class SignInViewModel @Inject constructor(
9595
Log.d("Sign", "onSignUp: ${it.message}")
9696
}
9797
}
98+
fun checkIfLoggedIn():Boolean {
99+
return currentUser != null
100+
}
101+
102+
fun signOut(){
103+
auth.signOut()
104+
currentUser = null
105+
}
98106
}
-144 KB
Binary file not shown.
1.25 KB
Loading
-1.05 MB
Loading

0 commit comments

Comments
 (0)