Created
June 2, 2025 02:59
-
-
Save Sprajapati123/88e1d8d3e263e2fb7c03871d03a8412a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.c36b.view | |
import android.app.Activity | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.LaunchedEffect | |
import androidx.compose.runtime.livedata.observeAsState | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.platform.LocalContext | |
import com.example.c36b.repository.UserRepositoryImpl | |
import com.example.c36b.viewmodel.UserViewModel | |
class DashboardActitivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
DashboardBody() | |
} | |
} | |
} | |
@Composable | |
fun DashboardBody() { | |
val context = LocalContext.current | |
val activity = context as Activity | |
val email: String? = activity.intent.getStringExtra("email") | |
val password: String? = activity.intent.getStringExtra("password") | |
val repo = remember { UserRepositoryImpl() } | |
val viewModel = remember { UserViewModel(repo) } | |
val users = viewModel.allUsers.observeAsState(initial = emptyList()) | |
LaunchedEffect(Unit) { | |
viewModel.getAllUserFromDatabase() | |
} | |
Scaffold { innerPadding -> | |
LazyColumn( | |
modifier = Modifier | |
.padding(innerPadding) | |
.fillMaxSize() | |
) { | |
item { | |
Text("Good Morning, $email") | |
Text("Good Morning, $email") | |
Text("Good Morning, $email") | |
Text("Good Morning, $email") | |
} | |
items(users.value.size) { index -> | |
val user = users.value[index] | |
Text(text = "${user.email})") | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.c36b.viewmodel | |
import android.util.Log | |
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.example.c36b.model.UserModel | |
import com.example.c36b.repository.UserRepository | |
import com.google.firebase.auth.FirebaseUser | |
class UserViewModel(val repo: UserRepository) : ViewModel() { | |
fun login( | |
email: String, password: String, | |
callback: (Boolean, String) -> Unit | |
) { | |
repo.login(email, password, callback) | |
} | |
//authentication ko function | |
fun register( | |
email: String, password: String, | |
callback: (Boolean, String, String) -> Unit | |
) { | |
repo.register(email, password, callback) | |
} | |
//real time database ko function | |
fun addUserToDatabase( | |
userId: String, model: UserModel, | |
callback: (Boolean, String) -> Unit | |
) { | |
repo.addUserToDatabase(userId, model, callback) | |
} | |
fun forgetPassword(email: String, callback: (Boolean, String) -> Unit) { | |
repo.forgetPassword(email, callback) | |
} | |
fun getCurrentUser(): FirebaseUser? { | |
return repo.getCurrentUser() | |
} | |
// { | |
// "success" : true, | |
// "message": "logout succesfull", | |
// } | |
var _userData = MutableLiveData<UserModel?>() | |
var userData = MutableLiveData<UserModel?>() | |
get() = _userData | |
fun getUserFromDatabase( | |
userId: String, | |
) { | |
repo.getUserFromDatabase(userId) { success, message, data -> | |
if (success) { | |
_userData.value = data | |
} else { | |
_userData.value = null | |
} | |
} | |
} | |
private val _allUsers = MutableLiveData<List<UserModel>>() | |
val allUsers: LiveData<List<UserModel>> get() = _allUsers | |
fun getAllUserFromDatabase() { | |
repo.getAllUserFromData { success, message, users -> | |
if (success) { | |
_allUsers.postValue(users) | |
} | |
} | |
} | |
fun logout(callback: (Boolean, String) -> Unit) { | |
repo.logout(callback) | |
} | |
fun editProfile( | |
userId: String, | |
data: MutableMap<String, Any?>, | |
callback: (Boolean, String) -> Unit | |
) { | |
repo.editProfile(userId, data, callback) | |
} | |
fun deleteAccount(userId: String, callback: (Boolean, String) -> Unit) { | |
repo.deleteAccount(userId, callback) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment