Skip to content

Commit

Permalink
Added Show Contact Visibility Logic Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
binayshaw7777 committed Jun 2, 2024
1 parent 6a9d6b0 commit 6bcb2f4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/com/binay/shaw/justap/domain/FirebaseResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.binay.shaw.justap.domain

sealed class Resource<T> {
data class Success<T>(val data: T) : Resource<T>()
data class Error<T>(val exception: Exception) : Resource<T>()
data class Loading<T>(val message: String?) : Resource<T>()
data class Clear<T>(val message: String? = null) : Resource<T>()

companion object {
fun <T> success(data: T): Resource<T> {
return Success(data)
}

fun <T> error(exception: Exception): Resource<T> {
return Error(exception)
}

fun <T> loading(message: String? = null): Resource<T> {
return Loading(message)
}

fun <T> clear(message: String? = null): Resource<T> {
return Clear(message)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.binay.shaw.justap.presentation.home

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -28,10 +29,13 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color.Companion.Transparent
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.binay.shaw.justap.domain.Resource
import com.binay.shaw.justap.presentation.components.AccountCard
import com.binay.shaw.justap.presentation.components.EmptyState
import com.binay.shaw.justap.presentation.components.ProgressDialog
import com.binay.shaw.justap.presentation.components.SearchBar
import com.binay.shaw.justap.presentation.mainScreens.homeScreen.accountFragments.AddEditViewModel
import com.binay.shaw.justap.presentation.navigation.LocalNavHost
Expand All @@ -51,10 +55,14 @@ fun HomeScreen(
addEditViewModel: AddEditViewModel = hiltViewModel()
) {

val context = LocalContext.current
val navController = LocalNavHost.current

val userAccountList by accountViewModel.userAccountList.collectAsState(emptyList())
val user by localUserViewModel.user.collectAsState()
val navController = LocalNavHost.current
val updateAccountResult by addEditViewModel.updateAccountResult.collectAsState()

var showProgress by remember { mutableStateOf(false) }
var search by remember { mutableStateOf("") }
val filteredAccountList = if (search.isBlank()) {
userAccountList
Expand All @@ -65,6 +73,32 @@ fun HomeScreen(
}
}

LaunchedEffect(updateAccountResult) {
when (updateAccountResult) {
is Resource.Success<*> -> {
Toast.makeText(context, "Account updated successfully", Toast.LENGTH_SHORT)
.show()
showProgress = false
addEditViewModel.clearUpdateStatus()
}

is Resource.Error -> {
Toast.makeText(context, "Failed to update account", Toast.LENGTH_SHORT).show()
showProgress = false
}

is Resource.Loading -> {
Timber.d("Updating account")
showProgress = true
addEditViewModel.clearUpdateStatus()
}

is Resource.Clear -> {
showProgress = false
}
}
}

Rebugger(
trackMap = mapOf(
"userAccountList" to userAccountList,
Expand All @@ -83,6 +117,10 @@ fun HomeScreen(
Timber.d("User account list: $userAccountList")
}

if (showProgress) {
ProgressDialog {}
}

Scaffold(
topBar = {
TopAppBar(
Expand Down Expand Up @@ -128,7 +166,7 @@ fun HomeScreen(
navController.navigate(Screens.ContactDetailsScreen.name)
}
} else if (filteredAccountList.isEmpty()) {
EmptyState(text = "No accounts found",)
EmptyState(text = "No accounts found")
} else {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(8.dp)
Expand All @@ -139,6 +177,7 @@ fun HomeScreen(
) { account ->
AccountCard(account) { newValue ->
Timber.d("Switch clicked with value: $newValue")
addEditViewModel.updateVisibility(user.userID, account, newValue)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package com.binay.shaw.justap.presentation.mainScreens.homeScreen.accountFragmen
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.binay.shaw.justap.domain.Resource
import com.binay.shaw.justap.model.Accounts
import com.binay.shaw.justap.utilities.Constants
import com.binay.shaw.justap.utilities.Util
import com.binay.shaw.justap.model.Accounts
import com.binay.shaw.justap.viewModel.AccountsViewModel
import com.google.firebase.database.FirebaseDatabase
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import timber.log.Timber


class AddEditViewModel : ViewModel() {
Expand All @@ -20,6 +27,9 @@ class AddEditViewModel : ViewModel() {
private val firebaseDatabase = FirebaseDatabase.getInstance()
val selectedAccount = MutableLiveData<String>()

private val _updateAccountResult = MutableStateFlow<Resource<Any>>(Resource.Clear())
val updateAccountResult = _updateAccountResult.asStateFlow()

init {
saveStatus.value = 0
deleteStatus.value = 0
Expand Down Expand Up @@ -214,4 +224,34 @@ class AddEditViewModel : ViewModel() {
}
}


fun updateVisibility(
userId: String,
account: Accounts,
newVisibility: Boolean
) = viewModelScope.launch(Dispatchers.IO) {
_updateAccountResult.value = Resource.Loading("Updating visibility")

val firebaseDatabaseReference =
firebaseDatabase.getReference(
"/${Constants.users}/$userId/${Constants.accounts}/${account.accountID}"
)
val updates: Map<String, Any> = mapOf(
"showAccount" to newVisibility
)

try {
firebaseDatabaseReference.updateChildren(updates).await()
Timber.d("Updated visibility in Firebase")
val data = account.copy(showAccount = newVisibility)
_updateAccountResult.value = Resource.Success(data)
} catch (e: Exception) {
Timber.d("Failed to update visibility in Firebase")
_updateAccountResult.value = Resource.Error(e)
}
}

fun clearUpdateStatus() {
_updateAccountResult.value = Resource.Clear()
}
}

0 comments on commit 6bcb2f4

Please sign in to comment.