Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsrk committed Feb 16, 2023
1 parent 370a46e commit a1c9780
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 64 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ dependencies {

// Work manager
implementation 'androidx.work:work-runtime-ktx:2.8.0'

// Navigation animation
implementation 'com.google.accompanist:accompanist-navigation-animation:0.29.1-alpha'
}
11 changes: 7 additions & 4 deletions app/src/main/java/com/example/buspayment/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
@file:OptIn(ExperimentalAnimationApi::class)

package com.example.buspayment

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.buspayment.funtions.NotificationService
import com.example.buspayment.funtions.ToastService
import com.example.buspayment.navigations.SetupNavGraph
import com.example.buspayment.realtimeDB.repository.DBRepository
import com.example.buspayment.ui.theme.BusPaymentTheme
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private lateinit var navController: NavHostController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DBRepository.setContext(this)
ToastService.setContext(this)
NotificationService(applicationContext).createNotificationChannel()
setContent {
BusPaymentTheme {
val navController = rememberAnimatedNavController()
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
modifier = Modifier.fillMaxSize(),
) {
navController = rememberNavController()
SetupNavGraph(navController)
}
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/buspayment/funtions/ToastService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.buspayment.funtions

import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast

class ToastService() {
companion object {
@SuppressLint("StaticFieldLeak")
private lateinit var context: Context
fun setContext(con: Context) {
context = con
}
}

fun showToast(text: String) {
Toast.makeText(context, "", Toast.LENGTH_SHORT).show()
}
}
24 changes: 6 additions & 18 deletions app/src/main/java/com/example/buspayment/navigations/NavGraph.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@file:OptIn(ExperimentalAnimationApi::class)

package com.example.buspayment.navigations

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.example.buspayment.screens.admin.AddBusScreen
import com.example.buspayment.screens.admin.AdminHomeScreen
import com.example.buspayment.screens.admin.ManageAccountsScreen
Expand All @@ -18,27 +19,14 @@ import com.example.buspayment.screens.user.RechargeScreen
import com.example.buspayment.screens.user.ScanScreen
import com.example.buspayment.screens.user.UserHistoryScreen
import com.example.buspayment.screens.user.UserHomeScreen
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable

@Composable
fun SetupNavGraph(navController: NavHostController) {
// val context = LocalContext.current
// val mUserViewModel: UserViewModel =
// viewModel(factory = UserViewModel.UserViewModelFactory(context.applicationContext as Application))
// var email = mUserViewModel.readUser.observeAsState(listOf()).value
// if (email.isNotEmpty()) {
// Credentials().setEmail(email[0].email.toString())
// navController.navigate(Screens.Home.route) {
// popUpTo(0)
// }
// }
NavHost(
AnimatedNavHost(
navController,
startDestination = Screens.Splash.route
// startDestination = if (
// Credentials().getEmail()
// .isNotEmpty()
// ) Screens.Home.route
// else Screens.Login.route
) {
composable(
route = Screens.Login.route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class DBRepository @Inject constructor(
it.value
}
notificationService.showNotification(
"Payment ${payment[4]}",
"Your payment of ${payment[3]} taka from ${payment[1]} to ${payment[5]} has been ${payment[4]}",
"Payment ${payment[5]}",
"Your payment of ${payment[4]} taka from ${payment[2]} to ${payment[6]} has been ${payment[5]}",
)
}

Expand Down Expand Up @@ -308,15 +308,16 @@ class DBRepository @Inject constructor(
}
}

override fun updateBalance(pay: Double, userId: String): Flow<ResultState<String>> =
override fun updateBalance(pay: Double, from: String, to: String): Flow<ResultState<String>> =
callbackFlow {
Log.d("entered", "entered")
trySend(ResultState.Loading)
val paymentTransaction = object : Transaction.Handler {
override fun doTransaction(currentData: MutableData): Transaction.Result {
Log.i("firebase payment", "Updating balance for $userId")
val currentValue = currentData.getValue(RealtimeUserResponse.UserResponse::class.java)
currentValue?.balance = currentValue?.balance?.plus(pay)!!
if (currentValue!!.userId == from)
currentValue.balance = currentValue.balance.plus(pay)
else
currentValue.balance = currentValue.balance.plus(-pay)
currentData.value = currentValue
return Transaction.success(currentData)
}
Expand Down Expand Up @@ -347,12 +348,18 @@ class DBRepository @Inject constructor(
}

}
db.child("userList").orderByChild("userId").equalTo(userId)
db.child("userList").orderByChild("userId").equalTo(from)
.addListenerForSingleValueEvent(singleValueListener)
if (to.isNotEmpty())
db.child("userList").orderByChild("userId").equalTo(to)
.addListenerForSingleValueEvent(singleValueListener)
// db.child("userList").runTransaction(paymentTransaction)
awaitClose {
db.child("userList").orderByChild("userId").equalTo(userId)
db.child("userList").orderByChild("userId").equalTo(from)
.removeEventListener(singleValueListener)
if (to.isNotEmpty())
db.child("userList").orderByChild("userId").equalTo(to)
.removeEventListener(singleValueListener)
close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Repository {
fun getUser(email: String = ""): Flow<ResultState<List<RealtimeUserResponse>>>
fun getBus(): Flow<ResultState<List<RealtimeBusResponse>>>
fun getDistance(): Flow<ResultState<List<RealtimeDistanceResponse>>>
fun updateBalance(pay: Double, userId: String): Flow<ResultState<String>>
fun updateBalance(pay: Double, from: String, to: String): Flow<ResultState<String>>
fun submitPayment(
payment: RealtimePaymentResponse.PaymentResponse,
from: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class RealtimeViewModel @Inject constructor(
fun updatePayment(email: String, payment: RealtimePaymentResponse) =
repo.updatePayment(email, payment)

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

data class UserState(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package com.example.buspayment.screens.conductor

import android.app.Application
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ElevatedAssistChip
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.buspayment.data.User
import com.example.buspayment.data.UserViewModel
import com.example.buspayment.navigations.Screens
import com.example.buspayment.realtimeDB.ui.RealtimeViewModel
import com.example.buspayment.ui.theme.LogoImage

@Composable
fun ConductorHomeScreen(navController: NavController) {
fun ConductorHomeScreen(
navController: NavController,
viewModel: RealtimeViewModel = hiltViewModel()
) {
val context = LocalContext.current
val users = viewModel.userRes.value
var balance by remember { mutableStateOf(0.0) }
var user by remember { mutableStateOf(listOf<User>()) }
val mUserViewModel: UserViewModel =
viewModel(factory = UserViewModel.UserViewModelFactory(context.applicationContext as Application))
user = mUserViewModel.readUser.observeAsState(emptyList()).value
LaunchedEffect(key1 = user) {
if (user.isNotEmpty()) {
viewModel.getUser(user[0].email)
}
}
if (users.user.isNotEmpty()) {
balance = users.user[0].user?.balance!!
}
Column {
Column(
Modifier
Expand All @@ -35,8 +76,37 @@ fun ConductorHomeScreen(navController: NavController) {
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
) {
Row {
Text(text = "Conductor")
Row(
modifier = Modifier.animateContentSize(
animationSpec = tween(
durationMillis = 500,
easing = LinearOutSlowInEasing
)
)
) {
ElevatedAssistChip(onClick = { navController.navigate(Screens.Recharge.route) }, label = {
Row(
modifier = Modifier.animateContentSize(
animationSpec = tween(
durationMillis = 500,
easing = LinearOutSlowInEasing
),
),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Balance "
)
if (users.isLoading) {
CircularProgressIndicator(
modifier = Modifier
.height(16.dp)
.width(16.dp)
)
} else Text("$balance")

}
})
}
Row {
Text(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@file:OptIn(ExperimentalFoundationApi::class)

package com.example.buspayment.screens.conductor

import android.app.Application
import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -89,7 +92,7 @@ fun PaymentListScreen(
"@"
)
}
LazyColumn() {
LazyColumn(reverseLayout = true) {
items(
paymentList,
key = {
Expand Down Expand Up @@ -141,6 +144,7 @@ fun PaymentListRow(
RealtimePaymentResponse(
payment = RealtimePaymentResponse.PaymentResponse(
fromUser = itemState.payment.fromUser,
code = itemState.payment.code,
status = "Accepted",
toUser = itemState.payment.toUser,
from = itemState.payment.from,
Expand Down Expand Up @@ -176,6 +180,7 @@ fun PaymentListRow(
RealtimePaymentResponse(
payment = RealtimePaymentResponse.PaymentResponse(
fromUser = itemState.payment.fromUser,
code = itemState.payment.code,
status = "Rejected",
toUser = itemState.payment.toUser,
from = itemState.payment.from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun RechargeScreen(
.clickable {
scope.launch {
viewModel
.updateBalance(50.0, user[0].email.substringBefore("@"))
.updateBalance(50.0, user[0].email.substringBefore("@"), "")
.collect { response ->
when (response) {
is ResultState.Success -> {
Expand Down Expand Up @@ -118,7 +118,7 @@ fun RechargeScreen(
.clickable {
scope.launch {
viewModel
.updateBalance(100.0, user[0].email.substringBefore("@"))
.updateBalance(100.0, user[0].email.substringBefore("@"), "")
.collect { response ->
when (response) {
is ResultState.Success -> {
Expand Down Expand Up @@ -154,7 +154,7 @@ fun RechargeScreen(
.clickable {
scope.launch {
viewModel
.updateBalance(500.0, user[0].email.substringBefore("@"))
.updateBalance(500.0, user[0].email.substringBefore("@"), "")
.collect { response ->
when (response) {
is ResultState.Success -> {
Expand Down
Loading

0 comments on commit a1c9780

Please sign in to comment.