исправление багов в медиаторе заказов

This commit is contained in:
dasha 2023-12-18 22:35:26 +04:00
parent 30f50e94c7
commit baf333dea1
9 changed files with 21 additions and 36 deletions

View File

@ -53,8 +53,8 @@ class OrderRemoteMediator(
try { try {
val orders = service.getOrders( val orders = service.getOrders(
LiveStore.user.value?.uid ?: 0, userId = LiveStore.user.value?.uid ?: 0,
page, state.config.pageSize page = page, limit = state.config.pageSize
).map { it.toOrder() } ).map { it.toOrder() }
val endOfPaginationReached = orders.isEmpty() val endOfPaginationReached = orders.isEmpty()
database.withTransaction { database.withTransaction {

View File

@ -1,5 +1,6 @@
package com.example.myapplication.api.order package com.example.myapplication.api.order
import android.util.Log
import androidx.paging.ExperimentalPagingApi import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager import androidx.paging.Pager
import androidx.paging.PagingConfig import androidx.paging.PagingConfig
@ -24,9 +25,9 @@ class RestOrderRepository(
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository, private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
private val database: AppDatabase private val database: AppDatabase
) : OrderRepository { ) : OrderRepository {
override fun getAllOrders(userId: Int): Flow<PagingData<Order>> { override fun getAllOrders(): Flow<PagingData<Order>> {
val pagingSourceFactory = { dbOrderRepository.getAllOrdersPagingSource(userId) } val pagingSourceFactory = { dbOrderRepository.getAllOrdersPagingSource() }
Log.d("RestOrderRepository", "?")
@OptIn(ExperimentalPagingApi::class) @OptIn(ExperimentalPagingApi::class)
return Pager( return Pager(
config = PagingConfig( config = PagingConfig(

View File

@ -15,8 +15,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
@ -26,7 +24,6 @@ import androidx.navigation.NavController
import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey import androidx.paging.compose.itemKey
import com.example.myapplication.LiveStore
import com.example.myapplication.composeui.navigation.Screen import com.example.myapplication.composeui.navigation.Screen
import com.example.myapplication.ui.theme.PmudemoTheme import com.example.myapplication.ui.theme.PmudemoTheme
import org.threeten.bp.format.DateTimeFormatter import org.threeten.bp.format.DateTimeFormatter
@ -36,11 +33,8 @@ fun OrderList(
navController: NavController?, navController: NavController?,
viewModel: OrderListViewModel = viewModel(factory = AppViewModelProvider.Factory) viewModel: OrderListViewModel = viewModel(factory = AppViewModelProvider.Factory)
) { ) {
val user = LiveStore.user.observeAsState()
val ordersUiState = viewModel.orderListUiState.collectAsLazyPagingItems() val ordersUiState = viewModel.orderListUiState.collectAsLazyPagingItems()
LaunchedEffect(user.value?.uid) {
viewModel.refreshState(user.value?.uid ?: 0)
}
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()

View File

@ -5,16 +5,9 @@ import com.example.myapplication.composeui.MyViewModel
import com.example.myapplication.database.entities.model.Order import com.example.myapplication.database.entities.model.Order
import com.example.myapplication.database.entities.repository.OrderRepository import com.example.myapplication.database.entities.repository.OrderRepository
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
class OrderListViewModel( class OrderListViewModel(
private val orderRepository: OrderRepository orderRepository: OrderRepository
) : MyViewModel() { ) : MyViewModel() {
var orderListUiState: Flow<PagingData<Order>> = emptyFlow() var orderListUiState: Flow<PagingData<Order>> = orderRepository.getAllOrders()
fun refreshState(userId: Int = 0) {
runInScope(actionSuccess = {
orderListUiState = orderRepository.getAllOrders(userId)
})
}
} }

View File

@ -92,7 +92,7 @@ fun OrderView(
Text( Text(
text = "${session.cinema.name}, ${session.cinema.year}\n" + text = "${session.cinema.name}, ${session.cinema.year}\n" +
"Цена: ${session.frozenPrice}\n" + "Цена: ${session.frozenPrice}\n" +
"Количество: $count", "Количество: ${count.intValue}",
color = MaterialTheme.colorScheme.onSecondary color = MaterialTheme.colorScheme.onSecondary
) )
} }

View File

@ -4,6 +4,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig import androidx.paging.PagingConfig
import androidx.paging.PagingData import androidx.paging.PagingData
import androidx.paging.PagingSource import androidx.paging.PagingSource
import com.example.myapplication.LiveStore
import com.example.myapplication.database.AppContainer import com.example.myapplication.database.AppContainer
import com.example.myapplication.database.entities.dao.OrderDao import com.example.myapplication.database.entities.dao.OrderDao
import com.example.myapplication.database.entities.model.Order import com.example.myapplication.database.entities.model.Order
@ -11,19 +12,21 @@ import com.example.myapplication.database.entities.model.SessionFromOrder
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class OfflineOrderRepository(private val orderDao: OrderDao) : OrderRepository { class OfflineOrderRepository(private val orderDao: OrderDao) : OrderRepository {
override fun getAllOrders(userId: Int): Flow<PagingData<Order>> = Pager( override fun getAllOrders(): Flow<PagingData<Order>> = Pager(
config = PagingConfig( config = PagingConfig(
pageSize = AppContainer.LIMIT, pageSize = AppContainer.LIMIT,
enablePlaceholders = false enablePlaceholders = false
), ),
pagingSourceFactory = { orderDao.getAll(userId) } pagingSourceFactory = { orderDao.getAll(LiveStore.user.value?.uid ?: 0) }
).flow ).flow
override suspend fun getOrder(uid: Int): List<SessionFromOrder> = orderDao.getByUid(uid) override suspend fun getOrder(uid: Int): List<SessionFromOrder> = orderDao.getByUid(uid)
override suspend fun insertOrder(order: Order): Long = orderDao.insert(order).first() override suspend fun insertOrder(order: Order): Long = orderDao.insert(order).first()
fun getAllOrdersPagingSource(userId: Int?): PagingSource<Int, Order> = orderDao.getAll(userId) fun getAllOrdersPagingSource(): PagingSource<Int, Order> {
return orderDao.getAll(LiveStore.user.value?.uid ?: 0)
}
suspend fun clearOrders() = orderDao.deleteAll() suspend fun clearOrders() = orderDao.deleteAll()

View File

@ -6,7 +6,7 @@ import com.example.myapplication.database.entities.model.SessionFromOrder
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface OrderRepository { interface OrderRepository {
fun getAllOrders(userId: Int): Flow<PagingData<Order>> fun getAllOrders(): Flow<PagingData<Order>>
suspend fun getOrder(uid: Int): List<SessionFromOrder> suspend fun getOrder(uid: Int): List<SessionFromOrder>
suspend fun insertOrder(order: Order): Long suspend fun insertOrder(order: Order): Long
} }

View File

@ -24,19 +24,13 @@
"id": 1, "id": 1,
"userId": 3, "userId": 3,
"sessionId": 1, "sessionId": 1,
"count": 5 "count": 3
},
{
"userId": 2,
"sessionId": 1,
"count": 1,
"id": 2
}, },
{ {
"id": 3,
"userId": 2, "userId": 2,
"sessionId": 2, "sessionId": 2,
"count": 1, "count": 3
"id": 3
} }
], ],
"orders": [ "orders": [

View File

@ -44,7 +44,7 @@ module.exports = (req, res, next) => {
const cinema = cinemas.find(cinema => cinema.id === session.cinemaId) const cinema = cinemas.find(cinema => cinema.id === session.cinemaId)
return { return {
cinema_name: cinema ? cinema.name : "Unknown", cinema_name: cinema ? cinema.name : "Неизвестно",
current_ticket_date_time: session.dateTime, current_ticket_date_time: session.dateTime,
current_ticket_price: session.price, current_ticket_price: session.price,
max_ticket_quantity: session.maxCount, max_ticket_quantity: session.maxCount,