Skip to content

Commit

Permalink
Bug - Fixed Address, Deletion Issue
Browse files Browse the repository at this point in the history
 -Close #433
 -Close #587
  • Loading branch information
niyajali committed Dec 3, 2023
1 parent e20a58e commit b565d40
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object AddressTestTags {
const val ADDRESS_PRICE_LESS_THAN_TWO_ERROR = "Address short name must be more than 2 characters long"

const val DELETE_ADDRESS_ITEM_TITLE = "Delete Address?"
const val DELETE_ADDRESS_ITEM_MESSAGE = "Are you sure to delete these addresses?"
const val DELETE_ADDRESS_ITEM_MESSAGE = "Are you sure to delete these addresses, all orders will be deleted of this address."

const val ADDRESS_ITEM_TAG = "Tag-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,9 @@ import com.niyaj.common.tags.CartOrderTestTags.ORDER_SHORT_NAME_EMPTY_ERROR
import com.niyaj.data.mapper.toEntity
import com.niyaj.data.repository.CartOrderRepository
import com.niyaj.data.repository.validation.CartOrderValidationRepository
import com.niyaj.database.dao.AddressDao
import com.niyaj.database.dao.CartDao
import com.niyaj.database.dao.CartOrderDao
import com.niyaj.database.dao.CartPriceDao
import com.niyaj.database.dao.CustomerDao
import com.niyaj.database.dao.SelectedDao
import com.niyaj.database.model.CartAddOnItemsEntity
import com.niyaj.database.model.CartChargesEntity
import com.niyaj.database.model.CartOrderEntity
import com.niyaj.database.model.CartPriceEntity
import com.niyaj.database.model.SelectedEntity
import com.niyaj.database.model.asExternalModel
import com.niyaj.model.AddOnItem
import com.niyaj.model.Address
import com.niyaj.model.CartOrder
import com.niyaj.model.CartOrderWithAddOnAndCharges
import com.niyaj.model.Charges
import com.niyaj.model.Customer
import com.niyaj.model.OrderStatus
import com.niyaj.model.OrderType
import com.niyaj.model.SELECTED_ID
import com.niyaj.model.Selected
import com.niyaj.model.filterCartOrder
import com.niyaj.model.searchAddress
import com.niyaj.model.searchCustomer
import com.niyaj.database.dao.*
import com.niyaj.database.model.*
import com.niyaj.model.*
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
Expand Down Expand Up @@ -403,6 +381,12 @@ class CartOrderRepositoryImpl(
}
}

override suspend fun getCartOrderIdsByAddressId(addressId: Int): List<Int> {
return withContext(ioDispatcher) {
cartOrderDao.getCartOrdersByAddressId(addressId)
}
}

override suspend fun validateCustomerPhone(customerPhone: String): ValidationResult {
if (customerPhone.isEmpty()) {
return ValidationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ReportsRepositoryImpl(
return withContext(ioDispatcher) {
reportsDao.getProductWiseOrder(startDate.toLong(), endDate.toLong())
.mapLatest { it ->
it.filter {
it.asSequence().filter {
if (orderType.isNotEmpty()) {
it.cartOrderEntity.orderType.name == orderType
} else true
Expand All @@ -101,7 +101,7 @@ class ReportsRepositoryImpl(
},
quantity = groupedProducts.value.sumOf { it.quantity }
)
}.sortedByDescending { it.quantity }
}.sortedByDescending { it.quantity }.toList()
}
}
}
Expand All @@ -114,7 +114,7 @@ class ReportsRepositoryImpl(
return withContext(ioDispatcher) {
reportsDao.getProductWiseOrder(startDate.toLong(), endDate.toLong())
.mapLatest { orders ->
orders.filter {
orders.asSequence().filter {
orderType.isEmpty() || it.cartOrderEntity.orderType.name == orderType
}.flatMap { order ->
order.cartItems.map { cartItem -> cartItem.productId to cartItem.quantity }
Expand All @@ -137,7 +137,7 @@ class ReportsRepositoryImpl(
)
}.sortedByDescending { it.quantity }
)
}.sortedByDescending { it -> it.productWithQuantity.sumOf { it.quantity } }
}.sortedByDescending { it -> it.productWithQuantity.sumOf { it.quantity } }.toList()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.niyaj.data.repository

import com.niyaj.common.result.Resource
import com.niyaj.model.AddOnItem
import com.niyaj.model.Address
import com.niyaj.model.CartOrder
import com.niyaj.model.CartOrderWithAddOnAndCharges
import com.niyaj.model.Charges
import com.niyaj.model.Customer
import com.niyaj.model.Selected
import com.niyaj.model.*
import kotlinx.coroutines.flow.Flow

interface CartOrderRepository {
Expand Down Expand Up @@ -41,4 +35,6 @@ interface CartOrderRepository {
suspend fun deleteCartOrder(orderId: Int): Resource<Boolean>

suspend fun deleteCartOrders(orderIds: List<Int>): Resource<Boolean>

suspend fun getCartOrderIdsByAddressId(addressId: Int): List<Int>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.niyaj.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import androidx.room.Upsert
import androidx.room.*
import com.niyaj.database.model.AddressEntity
import com.niyaj.database.model.AddressWiseOrderDto
import kotlinx.coroutines.flow.Flow
Expand Down
23 changes: 10 additions & 13 deletions core/database/src/main/java/com/niyaj/database/dao/CartOrderDao.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package com.niyaj.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import com.niyaj.database.model.AddOnItemEntity
import com.niyaj.database.model.CartAddOnItemsEntity
import com.niyaj.database.model.CartChargesEntity
import com.niyaj.database.model.CartOrderEntity
import com.niyaj.database.model.CartOrderWithAddOnAndChargesDto
import com.niyaj.database.model.ChargesEntity
import androidx.room.*
import com.niyaj.database.model.*
import com.niyaj.model.ChargesPriceWithApplicable
import com.niyaj.model.OrderStatus
import kotlinx.coroutines.flow.Flow
import java.util.Date
import java.util.*

@Dao
interface CartOrderDao {
Expand Down Expand Up @@ -188,4 +178,11 @@ interface CartOrderDao {
status: OrderStatus = OrderStatus.PLACED,
updatedAt: Date = Date(),
): Int

@Query(
value = """
SELECT orderId FROM cartorder WHERE addressId = :addressId
"""
)
fun getCartOrdersByAddressId(addressId: Int): List<Int>
}
19 changes: 9 additions & 10 deletions core/database/src/main/java/com/niyaj/database/dao/ReportsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ interface ReportsDao {
@Transaction
@Query(
value = """
SELECT * FROM cartorder WHERE createdAt BETWEEN :startDate
SELECT * FROM cartorder WHERE (createdAt BETWEEN :startDate
AND :endDate OR updatedAt BETWEEN :startDate
AND :endDate AND orderStatus = :orderStatus AND orderType = :orderType
AND :endDate) AND orderStatus = :orderStatus AND orderType = :orderType
"""
)
suspend fun getTotalDineInOrders(
Expand All @@ -72,9 +72,9 @@ interface ReportsDao {
@Transaction
@Query(
value = """
SELECT * FROM cartorder WHERE createdAt BETWEEN :startDate
SELECT * FROM cartorder WHERE (createdAt BETWEEN :startDate
AND :endDate OR updatedAt BETWEEN :startDate
AND :endDate AND orderStatus = :orderStatus AND orderType = :orderType
AND :endDate) AND orderStatus = :orderStatus AND orderType = :orderType
"""
)
suspend fun getTotalDineOutOrders(
Expand Down Expand Up @@ -103,9 +103,8 @@ interface ReportsDao {
@Transaction
@Query(
value = """
SELECT addressId FROM cartorder WHERE createdAt BETWEEN :startDate
AND :endDate OR updatedAt BETWEEN :startDate
AND :endDate AND orderStatus = :orderStatus AND orderType = :orderType
SELECT addressId FROM cartorder WHERE orderStatus = :orderStatus AND orderType = :orderType
AND (createdAt BETWEEN :startDate AND :endDate OR updatedAt BETWEEN :startDate AND :endDate)
"""
)
fun getAddressWiseOrder(
Expand All @@ -119,9 +118,9 @@ interface ReportsDao {
@Transaction
@Query(
value = """
SELECT customerId FROM cartorder WHERE createdAt BETWEEN :startDate
AND :endDate OR updatedAt BETWEEN :startDate
AND :endDate AND orderStatus = :orderStatus AND orderType = :orderType
SELECT customerId FROM cartorder WHERE
(createdAt BETWEEN :startDate AND :endDate OR updatedAt BETWEEN :startDate AND :endDate)
AND orderStatus = :orderStatus AND orderType = :orderType
"""
)
fun getCustomerWiseOrder(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.niyaj.domain.use_cases

import com.niyaj.common.network.Dispatcher
import com.niyaj.common.network.PoposDispatchers
import com.niyaj.common.result.Resource
import com.niyaj.data.repository.AddressRepository
import com.niyaj.data.repository.CartOrderRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import javax.inject.Inject

class DeleteAddressesUseCase @Inject constructor(
private val addressRepository: AddressRepository,
private val cartOrderRepository: CartOrderRepository,
@Dispatcher(PoposDispatchers.IO)
private val ioDispatcher: CoroutineDispatcher,
) {
suspend operator fun invoke(addressIds: List<Int>): Resource<Boolean> {
return try {
withContext(ioDispatcher) {
addressRepository.deleteAddresses(addressIds).data?.let {
addressIds.forEach { addressId ->
val orderIds = cartOrderRepository.getCartOrderIdsByAddressId(addressId)
cartOrderRepository.deleteCartOrders(orderIds)
}
}
}

Resource.Success(true)
} catch (e: Exception) {
Resource.Error(e.message)
}
}
}
41 changes: 6 additions & 35 deletions feature/address/src/main/java/com/niyaj/address/AddressScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@ import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Business
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.FabPosition
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
Expand All @@ -42,11 +25,7 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.niyaj.address.destinations.AddEditAddressScreenDestination
import com.niyaj.address.destinations.AddressDetailsScreenDestination
import com.niyaj.address.destinations.AddressExportScreenDestination
import com.niyaj.address.destinations.AddressImportScreenDestination
import com.niyaj.address.destinations.AddressSettingsScreenDestination
import com.niyaj.address.destinations.*
import com.niyaj.common.tags.AddressTestTags.ADDRESS_ITEM_TAG
import com.niyaj.common.tags.AddressTestTags.ADDRESS_NOT_AVAILABLE
import com.niyaj.common.tags.AddressTestTags.ADDRESS_SCREEN_NOTE_TEXT
Expand All @@ -58,13 +37,7 @@ import com.niyaj.common.tags.AddressTestTags.DELETE_ADDRESS_ITEM_TITLE
import com.niyaj.common.utils.Constants.SEARCH_ITEM_NOT_FOUND
import com.niyaj.designsystem.theme.SpaceSmall
import com.niyaj.model.Address
import com.niyaj.ui.components.CircularBox
import com.niyaj.ui.components.ItemNotAvailable
import com.niyaj.ui.components.LoadingIndicator
import com.niyaj.ui.components.NoteCard
import com.niyaj.ui.components.ScaffoldNavActions
import com.niyaj.ui.components.StandardFAB
import com.niyaj.ui.components.StandardScaffold
import com.niyaj.ui.components.*
import com.niyaj.ui.event.UiState
import com.niyaj.ui.utils.Screens
import com.niyaj.ui.utils.UiEvent
Expand Down Expand Up @@ -287,9 +260,7 @@ fun AddressScreen(
Text(text = DELETE_ADDRESS_ITEM_TITLE)
},
text = {
Text(
text = DELETE_ADDRESS_ITEM_MESSAGE
)
Text(text = DELETE_ADDRESS_ITEM_MESSAGE)
},
confirmButton = {
TextButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@ package com.niyaj.address

import androidx.compose.runtime.snapshotFlow
import androidx.lifecycle.viewModelScope
import com.niyaj.common.network.Dispatcher
import com.niyaj.common.network.PoposDispatchers
import com.niyaj.common.result.Resource
import com.niyaj.data.repository.AddressRepository
import com.niyaj.domain.use_cases.DeleteAddressesUseCase
import com.niyaj.ui.event.BaseViewModel
import com.niyaj.ui.event.UiState
import com.niyaj.ui.utils.UiEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AddressViewModel @Inject constructor(
private val addressRepository: AddressRepository,
@Dispatcher(PoposDispatchers.IO)
private val ioDispatcher: CoroutineDispatcher
): BaseViewModel() {
private val deleteAddressesUseCase: DeleteAddressesUseCase,
) : BaseViewModel() {
override var totalItems: List<Int> = emptyList()

@OptIn(ExperimentalCoroutinesApi::class)
Expand All @@ -48,11 +41,13 @@ class AddressViewModel @Inject constructor(
override fun deleteItems() {
super.deleteItems()

viewModelScope.launch(ioDispatcher) {
when(val result = addressRepository.deleteAddresses(selectedItems.toList())) {
viewModelScope.launch {

when (val result = deleteAddressesUseCase(selectedItems.toList())) {
is Resource.Error -> {
mEventFlow.emit(UiEvent.OnError(result.message ?: "Unable"))
}

is Resource.Success -> {
mEventFlow.emit(
UiEvent.OnSuccess(
Expand Down

0 comments on commit b565d40

Please sign in to comment.