diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 7c452ed..0000000 --- a/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/AppModule.kt b/app/src/main/java/com/example/shawarma/AppModule.kt index 49dbb3e..6170be3 100644 --- a/app/src/main/java/com/example/shawarma/AppModule.kt +++ b/app/src/main/java/com/example/shawarma/AppModule.kt @@ -52,7 +52,7 @@ object AppModule { @Provides @Singleton fun provideProductRepository(db: AppDatabase, restProductRepository: RestProductRepository) : ProductRepository { - return ProductRepository(database = db, db.productDao(), db.orderProductDao(), restProductRepository) + return ProductRepository(database = db, db.productDao(), restProductRepository) } @Provides diff --git a/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt b/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt index 75bf813..1e8d35b 100644 --- a/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt +++ b/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt @@ -19,6 +19,7 @@ import retrofit2.http.Header import retrofit2.http.POST import retrofit2.http.PUT import retrofit2.http.Path +import retrofit2.http.Query interface MyServerService { @@ -110,6 +111,14 @@ interface MyServerService { @POST("order/product/delete") suspend fun deleteOrderProduct(@Body orderProduct: OrderProductRemote, @Header("Authorization") token: String) + // + // COURSEWORK ANALYSIS + // + + @GET("analysis/products") + suspend fun getTopProducts(@Query("period") period : String, @Header("Authorization") token: String) : List + + companion object { private const val BASE_URL = "https://10.0.2.2:80/api/" diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt index 29914b6..30e8b4b 100644 --- a/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt @@ -1,6 +1,7 @@ package com.example.shawarma.data.api.repos import com.example.shawarma.data.api.MyServerService +import com.example.shawarma.data.api.models.ProductModelRemote import com.example.shawarma.data.api.models.toProductModelRemote import com.example.shawarma.data.models.ProductModel import javax.inject.Inject @@ -19,4 +20,8 @@ class RestProductRepository @Inject constructor( suspend fun delete(id: Int, token: String) { service.deleteProduct(id, token) } + + suspend fun getTopProducts(period: String, token: String) : List { + return service.getTopProducts(period, token) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt index 0311af2..f8713cd 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt @@ -6,9 +6,9 @@ import androidx.paging.PagingConfig import androidx.paging.PagingData import com.example.shawarma.data.api.MyServerService import com.example.shawarma.data.api.mediators.ProductRemoteMediator +import com.example.shawarma.data.api.models.toProductModel import com.example.shawarma.data.api.repos.RestProductRepository import com.example.shawarma.data.db.AppDatabase -import com.example.shawarma.data.interfaces.dao.OrderProductDao import com.example.shawarma.data.interfaces.dao.ProductDao import com.example.shawarma.data.models.ProductModel import kotlinx.coroutines.flow.Flow @@ -17,7 +17,6 @@ import javax.inject.Inject class ProductRepository @Inject constructor( private val database: AppDatabase, private val productDao: ProductDao, - private val orderProductDao: OrderProductDao, private val restRepository: RestProductRepository ) { suspend fun insert(product: ProductModel, token: String) { @@ -63,4 +62,8 @@ class ProductRepository @Inject constructor( remoteMediator = ProductRemoteMediator(database = database, serverService = MyServerService.getInstance(), query = "items", token = token) ).flow + suspend fun getTopProducts(period: String, token: String) : List { + return restRepository.getTopProducts(period, token).map { it.toProductModel() } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt b/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt index feb306b..eccee6d 100644 --- a/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt +++ b/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt @@ -2,9 +2,7 @@ package com.example.shawarma.screens import android.annotation.SuppressLint import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding import androidx.compose.material.Scaffold -import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier @@ -20,6 +18,7 @@ import com.example.shawarma.screens.discount.DiscountScreen import com.example.shawarma.screens.home.HomeScreen import com.example.shawarma.screens.orders.OrdersScreen import com.example.shawarma.screens.products.ProductScreen +import com.example.shawarma.screens.products.ProductsAnalysis import com.example.shawarma.screens.products.ProductsScreen import com.example.shawarma.screens.registration.RegistrationScreen import com.example.shawarma.ui.theme.MyLightYellow @@ -77,6 +76,9 @@ fun MainNavBar() { val productId = navBackStackEntry.arguments?.getInt("productId") ProductScreen(navController, productId) } + composable(ScreenPaths.analysis.name){ + ProductsAnalysis() + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt index cb08f18..76ec8d2 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt @@ -160,6 +160,6 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) { ) } } - } -} \ No newline at end of file +} + diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt new file mode 100644 index 0000000..8c90308 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductsAnalysis.kt @@ -0,0 +1,190 @@ +package com.example.shawarma.screens.products + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.Button +import androidx.compose.material.ButtonDefaults +import androidx.compose.material.Card +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.compose.ui.zIndex +import androidx.hilt.navigation.compose.hiltViewModel +import com.example.shawarma.data.models.ProductModel +import com.example.shawarma.data.sharedpref.PreferencesManager +import com.example.shawarma.ui.theme.MarckFamily +import com.example.shawarma.ui.theme.MyLightYellow +import com.example.shawarma.ui.theme.MyMainBackground +import com.example.shawarma.ui.theme.MyOrange +import com.example.shawarma.ui.theme.NunitoFamily +import com.example.shawarma.viewmodels.ProductsViewModel +import com.example.shawarma.widgets.ShawarmaLogo2 + +@Composable +fun ProductsAnalysis() { + val productsViewModel: ProductsViewModel = hiltViewModel() + Box( + contentAlignment = Alignment.TopCenter + ) { + TopProductsWidget(productsViewModel) + ShawarmaLogo2() + } +} + +@Composable +fun TopProductsWidget(productsViewModel: ProductsViewModel) { + val preferencesManager = PreferencesManager(LocalContext.current) + val searchToken = preferencesManager.getData("token", "") + val topProductsList = productsViewModel.topProductList.observeAsState().value + + Box( + modifier = Modifier + .clip(shape = RoundedCornerShape(30.dp)) + .padding(top = 100.dp) + .fillMaxSize() + .background(color = MyMainBackground) + .zIndex(2f), + + contentAlignment = Alignment.TopCenter + ){ + Text( + text = "Аналитика", + fontFamily = MarckFamily, + fontSize = 40.sp, + modifier = Modifier.padding(top = 15.dp) + ) + Row( + modifier = Modifier + .fillMaxHeight() + .padding(top = 60.dp) + ){ + Button( + modifier = Modifier + .fillMaxWidth(0.33f) + .weight(1f), + colors = ButtonDefaults.buttonColors( + backgroundColor = MyLightYellow + ), + border = BorderStroke(2.dp, color = MyOrange), + shape = RoundedCornerShape(20.dp), + onClick = { + productsViewModel.getTopProductsList(searchToken, "week") + } + ) { + Text(text = "Неделя") + } + Button( + modifier = Modifier + .fillMaxWidth(0.33f) + .weight(1f), + colors = ButtonDefaults.buttonColors( + backgroundColor = MyLightYellow + ), + border = BorderStroke(2.dp, color = MyOrange), + shape = RoundedCornerShape(20.dp), + onClick = { + productsViewModel.getTopProductsList(searchToken, "month") + } + ) { + Text(text = "Месяц") + } + Button( + modifier = Modifier + .fillMaxWidth(0.33f) + .weight(1f), + colors = ButtonDefaults.buttonColors( + backgroundColor = MyLightYellow + ), + border = BorderStroke(2.dp, color = MyOrange), + shape = RoundedCornerShape(20.dp), + onClick = { + productsViewModel.getTopProductsList(searchToken, "year") + } + ) { + Text(text = "Год") + } + } + if (!topProductsList.isNullOrEmpty()) { + LazyColumn( + modifier = Modifier.padding(top = 120.dp, bottom = 60.dp), + + ){ + items( + topProductsList.size + ) { + TopProductItem(topProductsList[it]) + Spacer(modifier = Modifier.size(20.dp)) + } + } + } + } +} + +@Composable +fun TopProductItem(product: ProductModel){ + Card( + border = BorderStroke(width = 2.dp, color = MyOrange), + shape = RoundedCornerShape(size = 20.dp), + backgroundColor = Color.White, + modifier = Modifier.size(340.dp, 100.dp) + ) { + Row( + modifier = Modifier + .padding(20.dp), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceAround + ) { + Column( + modifier = Modifier.fillMaxWidth(1f) + ) { + Text( + text = product.title, + fontFamily = NunitoFamily, + fontSize = 20.sp, + fontWeight = FontWeight.Bold, + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + if (product.oldPrice != null) { + Text( + text = product.price.toString() + " руб.", + fontFamily = NunitoFamily, + fontSize = 20.sp, + fontWeight = FontWeight.Bold, + color = Color.Red + ) + } + else { + Text( + text = product.price.toString() + " руб.", + fontFamily = NunitoFamily, + fontSize = 20.sp, + fontWeight = FontWeight.Bold + ) + } + + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt index 519280e..f683f76 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt @@ -103,9 +103,29 @@ fun ProductsList(navHostController: NavHostController, productsViewModel: Produc fontWeight = FontWeight.Bold ) } + Button( + modifier = Modifier + .padding(top = 160.dp) + .size(340.dp, 60.dp), + colors = ButtonDefaults.buttonColors( + backgroundColor = MyOrange + ), + border = BorderStroke(2.dp, color = MyLightYellow), + shape = RoundedCornerShape(20.dp), + onClick = { + navHostController.navigate(ScreenPaths.analysis.name) + } + ) { + Text( + text = "Статистика", + fontFamily = NunitoFamily, + fontSize = 24.sp, + fontWeight = FontWeight.Bold + ) + } LazyColumn( horizontalAlignment = Alignment.CenterHorizontally, - modifier = Modifier.padding(top = 160.dp) + modifier = Modifier.padding(top = 240.dp) ) { items( diff --git a/app/src/main/java/com/example/shawarma/utils/ScreenPaths.kt b/app/src/main/java/com/example/shawarma/utils/ScreenPaths.kt index 75b5ccb..1df66de 100644 --- a/app/src/main/java/com/example/shawarma/utils/ScreenPaths.kt +++ b/app/src/main/java/com/example/shawarma/utils/ScreenPaths.kt @@ -1,5 +1,5 @@ package com.example.shawarma.utils enum class ScreenPaths { - authorization, registration, home, discount, cart, orders, products, product + authorization, registration, home, discount, cart, orders, products, product, analysis } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt index 8b13c1e..c316521 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/ProductsViewModel.kt @@ -1,5 +1,6 @@ package com.example.shawarma.viewmodels +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData @@ -25,5 +26,13 @@ class ProductsViewModel @Inject constructor( } } + val topProductList = MutableLiveData?>(null) + + fun getTopProductsList(token: String, period: String) { + viewModelScope.launch { + topProductList.postValue(productRepository.getTopProducts(period, token)) + } + } + } \ No newline at end of file