Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsrk committed Feb 14, 2023
1 parent 4c8bd5c commit 370a46e
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.util.Log
import com.example.buspayment.funtions.NotificationService
import com.example.buspayment.realtimeDB.responses.RealtimeBusResponse
import com.example.buspayment.realtimeDB.responses.RealtimeDistanceResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserHistoryResponse
import com.example.buspayment.realtimeDB.responses.RealtimePaymentResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import com.google.firebase.database.ChildEventListener
Expand Down Expand Up @@ -51,19 +51,21 @@ class DBRepository @Inject constructor(
}


override fun getConductorPaymentList(email: String): Flow<ResultState<List<RealtimeUserHistoryResponse>>> =
override fun getConductorPaymentList(email: String): Flow<ResultState<List<RealtimePaymentResponse>>> =
callbackFlow {
trySend(ResultState.Loading)

val valueEvent = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val payment = snapshot.children.map {
RealtimeUserHistoryResponse(
it.getValue(RealtimeUserHistoryResponse.PaymentResponse::class.java),
key = it.key
)
snapshot.children.map { item ->
val payment = item.children.map {
RealtimePaymentResponse(
it.getValue(RealtimePaymentResponse.PaymentResponse::class.java),
key = it.key
)
}
trySend(ResultState.Success(payment))
}
trySend(ResultState.Success(payment))
}

override fun onCancelled(error: DatabaseError) {
Expand All @@ -72,22 +74,22 @@ class DBRepository @Inject constructor(

}

db.child("conductorPaymentHistory").child(email).addValueEventListener(valueEvent)
db.child("paymentList").addValueEventListener(valueEvent)
awaitClose {
db.child("conductorPaymentHistory").child(email).removeEventListener(valueEvent)
db.child("paymentList").removeEventListener(valueEvent)
close()
}
}

override fun getPaymentHistoryByUser(email: String): Flow<ResultState<List<RealtimeUserHistoryResponse>>> =
override fun getPaymentHistoryByUser(email: String): Flow<ResultState<List<RealtimePaymentResponse>>> =
callbackFlow {
trySend(ResultState.Loading)

val valueEvent = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val payment = snapshot.children.map {
RealtimeUserHistoryResponse(
it.getValue(RealtimeUserHistoryResponse.PaymentResponse::class.java),
RealtimePaymentResponse(
it.getValue(RealtimePaymentResponse.PaymentResponse::class.java),
key = it.key
)
}
Expand Down Expand Up @@ -207,7 +209,10 @@ class DBRepository @Inject constructor(
}
}

override fun updatePayment(res: RealtimeUserHistoryResponse): Flow<ResultState<String>> =
override fun updatePayment(
email: String,
res: RealtimePaymentResponse
): Flow<ResultState<String>> =
callbackFlow {
trySend(ResultState.Loading)
val newUser = HashMap<String, Any>()
Expand All @@ -218,7 +223,7 @@ class DBRepository @Inject constructor(
newUser["paid"] = res.payment.paid!!
newUser["bus"] = res.payment.bus!!
newUser["status"] = res.payment.status!!
db.child("userPaymentList").child(res.key!!).updateChildren(
db.child("paymentList").child(email).child(res.key!!).updateChildren(
newUser
).addOnCompleteListener {
trySend(ResultState.Success("Data updated successfully"))
Expand All @@ -231,12 +236,22 @@ class DBRepository @Inject constructor(
}

override fun submitPayment(
payment: RealtimeUserHistoryResponse.PaymentResponse,
email: String
payment: RealtimePaymentResponse.PaymentResponse,
from: String,
to: String
): Flow<ResultState<String>> =
callbackFlow {
trySend(ResultState.Loading)
db.child("paymentList").child(email).push().setValue(
db.child("paymentList").child(from).push().setValue(
payment
).addOnCompleteListener {
if (it.isSuccessful)
trySend(ResultState.Success("Payment successful"))
}
.addOnFailureListener {
trySend(ResultState.Failure(it))
}
db.child("conductorPaymentList").child(to).push().setValue(
payment
).addOnCompleteListener {
if (it.isSuccessful)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.buspayment.realtimeDB.repository

import com.example.buspayment.realtimeDB.responses.RealtimeBusResponse
import com.example.buspayment.realtimeDB.responses.RealtimeDistanceResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserHistoryResponse
import com.example.buspayment.realtimeDB.responses.RealtimePaymentResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import kotlinx.coroutines.flow.Flow
Expand All @@ -17,13 +17,14 @@ interface Repository {
fun getDistance(): Flow<ResultState<List<RealtimeDistanceResponse>>>
fun updateBalance(pay: Double, userId: String): Flow<ResultState<String>>
fun submitPayment(
payment: RealtimeUserHistoryResponse.PaymentResponse,
email: String
payment: RealtimePaymentResponse.PaymentResponse,
from: String,
to: String
): Flow<ResultState<String>>

fun getConductorPaymentList(email: String): Flow<ResultState<List<RealtimeUserHistoryResponse>>>
fun getPaymentHistoryByUser(email: String): Flow<ResultState<List<RealtimeUserHistoryResponse>>>
fun updatePayment(res: RealtimeUserHistoryResponse): Flow<ResultState<String>>
fun getConductorPaymentList(email: String): Flow<ResultState<List<RealtimePaymentResponse>>>
fun getPaymentHistoryByUser(email: String): Flow<ResultState<List<RealtimePaymentResponse>>>
fun updatePayment(email: String, res: RealtimePaymentResponse): Flow<ResultState<String>>
fun deleteUser(
key: String
): Flow<ResultState<String>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.buspayment.realtimeDB.responses

data class RealtimeUserHistoryResponse(
data class RealtimePaymentResponse(
val payment: PaymentResponse?,
val key: String? = "",
) {
Expand All @@ -12,5 +12,6 @@ data class RealtimeUserHistoryResponse(
val to: String? = "",
val paid: Double? = 0.0,
val bus: String? = "",
val code: String? = "",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.lifecycle.viewModelScope
import com.example.buspayment.realtimeDB.repository.Repository
import com.example.buspayment.realtimeDB.responses.RealtimeBusResponse
import com.example.buspayment.realtimeDB.responses.RealtimeDistanceResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserHistoryResponse
import com.example.buspayment.realtimeDB.responses.RealtimePaymentResponse
import com.example.buspayment.realtimeDB.responses.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -32,8 +32,12 @@ class RealtimeViewModel @Inject constructor(
private val _userHisRes: MutableState<PaymentState> = mutableStateOf(PaymentState())
val userHisRes: State<PaymentState> = _userHisRes
fun addUser(users: RealtimeUserResponse.UserResponse) = repo.addUser(users)
fun submitPayment(payment: RealtimeUserHistoryResponse.PaymentResponse, email: String) =
repo.submitPayment(payment, email)
fun submitPayment(
payment: RealtimePaymentResponse.PaymentResponse,
from: String,
to: String
) =
repo.submitPayment(payment, from, to)

fun addBus(bus: RealtimeBusResponse.BusResponse) = repo.addBus(bus)

Expand Down Expand Up @@ -189,7 +193,9 @@ class RealtimeViewModel @Inject constructor(

fun delete(key: String) = repo.deleteUser(key)
fun updateUser(user: RealtimeUserResponse) = repo.updateUser(user)
fun updatePayment(payment: RealtimeUserHistoryResponse) = repo.updatePayment(payment)
fun updatePayment(email: String, payment: RealtimePaymentResponse) =
repo.updatePayment(email, payment)

fun updateBalance(pay: Double, userId: String) = repo.updateBalance(pay, userId)
}

Expand All @@ -212,7 +218,7 @@ data class DistState(
)

data class PaymentState(
val payment: List<RealtimeUserHistoryResponse> = emptyList(),
val payment: List<RealtimePaymentResponse> = emptyList(),
val error: String = "",
val isLoading: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ fun SplashScreen(navController: NavController) {
val mUserViewModel: UserViewModel =
viewModel(factory = UserViewModel.UserViewModelFactory(context.applicationContext as Application))
user = mUserViewModel.readUser.observeAsState(listOf()).value
LaunchedEffect(true) {
LaunchedEffect(user.isNotEmpty()) {
delay(2000)
if (user.isNotEmpty()) {
// Credentials().setEmail(user[0].email)
when (user[0].role) {
"User" -> {
"user" -> {
navController.navigate(Screens.UHome.route) {
popUpTo(0)
}
}

"Conductor" -> {
"conductor" -> {
navController.navigate(Screens.CHome.route) {
popUpTo(0);
}
}

"Admin" -> {
"admin" -> {
navController.navigate(Screens.AHome.route) {
popUpTo(0);
}
Expand Down
Loading

0 comments on commit 370a46e

Please sign in to comment.