Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsrk committed Feb 10, 2023
1 parent 4602b22 commit a82318d
Show file tree
Hide file tree
Showing 14 changed files with 510 additions and 156 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/example/buspayment/data/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ data class User(
val name: String,
val email: String,
val phone: String,
val role: String
val role: String,
val balance: Double
)
10 changes: 8 additions & 2 deletions app/src/main/java/com/example/buspayment/navigations/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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.ManageAccounts
import com.example.buspayment.screens.admin.ManageAccountsScreen
import com.example.buspayment.screens.admin.ManageBusScreen
import com.example.buspayment.screens.common.LoginScreen
import com.example.buspayment.screens.common.RegisterScreen
Expand Down Expand Up @@ -67,7 +68,7 @@ fun SetupNavGraph(navController: NavHostController) {
composable(
route = Screens.MAccount.route
) {
ManageAccounts(navController)
ManageAccountsScreen(navController)
}

composable(
Expand Down Expand Up @@ -105,5 +106,10 @@ fun SetupNavGraph(navController: NavHostController) {
) {
ManageBusScreen(navController)
}
composable(
route = Screens.ABus.route
) {
AddBusScreen(navController)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ sealed class Screens(val route: String) {
object Register : Screens(route = "register_screen")
object AHome : Screens(route = "admin_home_screen")
object MBus : Screens(route = "mbus_screen")
object ABus : Screens(route = "abus_screen")

object MAccount : Screens(route = "admin_manage_account_screen")
object CHome : Screens(route = "conductor_home_screen")
object PList : Screens(route = "payment_list_screen")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,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.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import com.google.firebase.database.DataSnapshot
Expand Down Expand Up @@ -108,6 +109,33 @@ class DBRepository @Inject constructor(
}
}

override fun getDistance(): Flow<ResultState<List<RealtimeDistanceResponse>>> = callbackFlow {
trySend(ResultState.Loading)

val valueEvent = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val distance = snapshot.children.map {
RealtimeDistanceResponse(
it.getValue(RealtimeDistanceResponse.DistanceResponse::class.java),
key = it.key
)
}
trySend(ResultState.Success(distance))
}

override fun onCancelled(error: DatabaseError) {
trySend(ResultState.Failure(error.toException()))
}

}

db.child("distance").addValueEventListener(valueEvent)
awaitClose {
db.child("distance").removeEventListener(valueEvent)
close()
}
}

override fun getBus(): Flow<ResultState<List<RealtimeBusResponse>>> = callbackFlow {
trySend(ResultState.Loading)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,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.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import kotlinx.coroutines.flow.Flow
Expand All @@ -12,6 +13,7 @@ interface Repository {

fun getUser(): Flow<ResultState<List<RealtimeUserResponse>>>
fun getBus(): Flow<ResultState<List<RealtimeBusResponse>>>
fun getDistance(): Flow<ResultState<List<RealtimeDistanceResponse>>>
fun delete(
key: String
): Flow<ResultState<String>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.buspayment.realtimeDB.responses

data class RealtimeDistanceResponse(
val dist: DistanceResponse?,
val key: String?,
) {
data class DistanceResponse(
val name: String = "",
val value: Double = 0.0,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
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.RealtimeUserResponse
import com.example.buspayment.utils.ResultState
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -17,8 +18,12 @@ import javax.inject.Inject
class RealtimeViewModel @Inject constructor(
private val repo: Repository
) : ViewModel() {
private val _res: MutableState<UserState> = mutableStateOf(UserState())
val res: State<UserState> = _res
private val _userRes: MutableState<UserState> = mutableStateOf(UserState())
val userRes: State<UserState> = _userRes
private val _distRes: MutableState<DistState> = mutableStateOf(DistState())
val distRes: State<DistState> = _distRes
private val _busRes: MutableState<BusState> = mutableStateOf(BusState())
val busRes: State<BusState> = _busRes
fun addUser(users: RealtimeUserResponse.UserResponse) = repo.addUser(users)
fun addBus(bus: RealtimeBusResponse.BusResponse) = repo.addBus(bus)

Expand All @@ -27,19 +32,65 @@ class RealtimeViewModel @Inject constructor(
repo.getUser().collect {
when (it) {
is ResultState.Success -> {
_res.value = UserState(
_userRes.value = UserState(
user = it.data
)
}

is ResultState.Failure -> {
_res.value = UserState(
_userRes.value = UserState(
error = it.msg.toString()
)
}

is ResultState.Loading -> {
_res.value = UserState(
_userRes.value = UserState(
isLoading = true
)
}
}
}
}
viewModelScope.launch {
repo.getBus().collect {
when (it) {
is ResultState.Success -> {
_busRes.value = BusState(
bus = it.data
)
}

is ResultState.Failure -> {
_busRes.value = BusState(
error = it.msg.toString()
)
}

is ResultState.Loading -> {
_busRes.value = BusState(
isLoading = true
)
}
}
}
}
viewModelScope.launch {
repo.getDistance().collect {
when (it) {
is ResultState.Success -> {
_distRes.value = DistState(
dist = it.data
)
}

is ResultState.Failure -> {
_busRes.value = BusState(
error = it.msg.toString()
)
}

is ResultState.Loading -> {
_busRes.value = BusState(
isLoading = true
)
}
Expand All @@ -56,4 +107,16 @@ data class UserState(
val user: List<RealtimeUserResponse> = emptyList(),
val error: String = "",
val isLoading: Boolean = false
)
)

data class BusState(
val bus: List<RealtimeBusResponse> = emptyList(),
val error: String = "",
val isLoading: Boolean = false
)

data class DistState(
val dist: List<RealtimeDistanceResponse> = emptyList(),
val error: String = "",
val isLoading: Boolean = false
)
106 changes: 106 additions & 0 deletions app/src/main/java/com/example/buspayment/screens/admin/AddBusScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package com.example.buspayment.screens.admin

import android.widget.Toast
import androidx.compose.foundation.background
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.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.example.buspayment.realtimeDB.responses.RealtimeBusResponse
import com.example.buspayment.realtimeDB.ui.RealtimeViewModel
import com.example.buspayment.utils.ResultState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun AddBusScreen(
navController: NavController,
viewModel: RealtimeViewModel = hiltViewModel()
) {
var busName by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
val context = LocalContext.current
Column {
Row(
Modifier
.fillMaxWidth()
.background(Color.Red, RoundedCornerShape(0.dp, 0.dp, 30.dp, 30.dp))
.padding(20.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
modifier = Modifier.clickable {
navController.popBackStack()
}, imageVector = Icons.Filled.ArrowBack, contentDescription = "Back button"
)
Text(text = "Manage buses", style = MaterialTheme.typography.headlineSmall)
Text(text = "", style = MaterialTheme.typography.headlineSmall)
}
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
OutlinedTextField(
busName,
{ text ->
busName = text
},
)
Button(onClick = {
scope.launch(Dispatchers.Main) {
viewModel.addBus(
RealtimeBusResponse.BusResponse(
name = busName,
"",
"Chandra",
"Mirpur"
)
).collect { response ->
when (response) {
is ResultState.Success -> {
Toast.makeText(context, "User created", Toast.LENGTH_LONG).show()
}

is ResultState.Failure -> {
Toast.makeText(context, "Failed", Toast.LENGTH_LONG).show()
}

is ResultState.Loading -> {}
}
}
}
}) {
Text("Submit")
}
}
}
}
Loading

0 comments on commit a82318d

Please sign in to comment.