diff --git a/app/src/main/java/com/example/android_programming/GlobalUser.kt b/app/src/main/java/com/example/android_programming/GlobalUser.kt new file mode 100644 index 0000000..44ed531 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/GlobalUser.kt @@ -0,0 +1,25 @@ +package com.example.android_programming + +import com.example.android_programming.model.User + +class GlobalUser private constructor() { + private var user: User? = null + + fun setUser(user: User?) { + this.user = user + } + + fun getUser(): User? { + return user + } + + companion object { + private var instance: GlobalUser? = null + + fun getInstance(): GlobalUser { + return instance ?: synchronized(this) { + instance ?: GlobalUser().also { instance = it } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/MainActivity.kt b/app/src/main/java/com/example/android_programming/MainActivity.kt index b4a7940..6116e8a 100644 --- a/app/src/main/java/com/example/android_programming/MainActivity.kt +++ b/app/src/main/java/com/example/android_programming/MainActivity.kt @@ -30,26 +30,4 @@ fun MainContent() { Navigate() } -} - -class GlobalUser private constructor() { - private var user: User? = null - - fun setUser(user: User?) { - this.user = user - } - - fun getUser(): User? { - return user - } - - companion object { - private var instance: GlobalUser? = null - - fun getInstance(): GlobalUser { - return instance ?: synchronized(this) { - instance ?: GlobalUser().also { instance = it } - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/api/BackendService.kt b/app/src/main/java/com/example/android_programming/api/BackendService.kt index 24f9dba..353467b 100644 --- a/app/src/main/java/com/example/android_programming/api/BackendService.kt +++ b/app/src/main/java/com/example/android_programming/api/BackendService.kt @@ -6,8 +6,6 @@ import com.example.android_programming.api.model.OrderSneakerRemote import com.example.android_programming.api.model.SneakerRemote import com.example.android_programming.api.model.UserRemote import com.example.android_programming.api.model.UserRemoteSignIn -import com.example.android_programming.model.Order -import com.example.android_programming.model.Sneaker import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory import kotlinx.serialization.json.Json import okhttp3.MediaType.Companion.toMediaType @@ -52,6 +50,13 @@ interface BackendService { @Path("id") id: Int ) + @GET("sneaker/findSneakersByString/{string}") + suspend fun findSneakersByString( + @Path("string") str: String, + nextPageNumber: Int, + loadSize: Int + ): List + //USER @POST("user/signup") suspend fun SignUp( diff --git a/app/src/main/java/com/example/android_programming/api/repository/RestSneakerRepository.kt b/app/src/main/java/com/example/android_programming/api/repository/RestSneakerRepository.kt index 70c527e..38f5227 100644 --- a/app/src/main/java/com/example/android_programming/api/repository/RestSneakerRepository.kt +++ b/app/src/main/java/com/example/android_programming/api/repository/RestSneakerRepository.kt @@ -4,6 +4,9 @@ import androidx.paging.ExperimentalPagingApi import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.PagingData +import androidx.paging.PagingSource +import androidx.paging.PagingState +import androidx.paging.compose.collectAsLazyPagingItems import com.example.android_programming.di.AppContainer import com.example.android_programming.api.BackendService import com.example.android_programming.api.SneakerRemoteMediator @@ -15,6 +18,8 @@ import com.example.android_programming.model.Sneaker import com.example.android_programming.database.repository.SneakerRepoImpl import com.example.android_programming.businessLogic.repo.SneakerRepository import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.flowOf class RestSneakerRepository( private val service: BackendService, @@ -23,7 +28,6 @@ class RestSneakerRepository( private val dbRemoteKeyRepository: RemoteKeysRepositoryImpl ) : SneakerRepository { - override fun getAllSneakers(): Flow> { val pagingSourceFactory = { dbSneakerRepository.getAllSneakersPagingSource() } diff --git a/app/src/main/java/com/example/android_programming/businessLogic/vmodel/SneakerViewModel.kt b/app/src/main/java/com/example/android_programming/businessLogic/vmodel/SneakerViewModel.kt index f37aab7..3284d57 100644 --- a/app/src/main/java/com/example/android_programming/businessLogic/vmodel/SneakerViewModel.kt +++ b/app/src/main/java/com/example/android_programming/businessLogic/vmodel/SneakerViewModel.kt @@ -4,12 +4,15 @@ import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import androidx.paging.PagingData import com.example.android_programming.R import com.example.android_programming.api.repository.RestSneakerRepository import com.example.android_programming.businessLogic.repo.SneakerRepository import com.example.android_programming.model.Sneaker import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.launch class SneakerViewModel(private val sneakerRepository: SneakerRepository): ViewModel() { @@ -19,6 +22,7 @@ class SneakerViewModel(private val sneakerRepository: SneakerRepository): ViewMo val description = mutableStateOf("") val price = mutableStateOf("") val photo = mutableIntStateOf(R.drawable.img) + fun insertSneaker() = viewModelScope.launch { val sneaker = Sneaker( brand = brand.value, diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt index 97b9de4..c552120 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt @@ -12,6 +12,7 @@ import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color @@ -32,21 +33,7 @@ fun HomeScreen(navHostController: NavHostController) { modifier = Modifier .fillMaxSize() .background(Color.White) - /*.verticalScroll(rememberScrollState())*/ ) { - Row { - // Поле для поиска - SearchField( - modifier = Modifier - .fillMaxWidth() - .padding(20.dp, 20.dp, 20.dp, 10.dp) - .clip(RoundedCornerShape(10.dp)) - ) { searchText -> - // Обработка введенного текста поиска - } - } - Sales() - FilterByBrand() RecyclerView(navHostController = navHostController) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt index 1a40e3e..a51fb6e 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt @@ -1,10 +1,15 @@ package com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView 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.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -13,6 +18,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController @@ -23,6 +29,9 @@ import com.example.android_programming.model.Sneaker import com.example.android_programming.businessLogic.vmodel.AppViewModelProvider import com.example.android_programming.businessLogic.vmodel.OrderViewModel import com.example.android_programming.businessLogic.vmodel.SneakerViewModel +import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.FilterByBrand +import com.example.android_programming.composeui.Screens.HomeScreen.Sales +import com.example.android_programming.composeui.Screens.HomeScreen.SearchField.SearchField import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -35,9 +44,23 @@ fun RecyclerView(navHostController: NavHostController, sneakerViewModel: Sneaker .fillMaxSize() .padding(bottom = 60.dp) ) { + Row { + // Поле для поиска + SearchField( + modifier = Modifier + .fillMaxWidth() + .padding(20.dp, 20.dp, 20.dp, 10.dp) + .clip(RoundedCornerShape(10.dp)) + ) { searchText -> + // Обработка введенного текста поиска + } + } + Sales() + FilterByBrand() val sneakerLazyPagingItems = sneakerViewModel.sneakerList.collectAsLazyPagingItems() - LazyVerticalGrid( + modifier = Modifier + .fillMaxSize(), columns = GridCells.Fixed(2) ) { items( @@ -55,3 +78,4 @@ fun RecyclerView(navHostController: NavHostController, sneakerViewModel: Sneaker + diff --git a/app/src/main/java/com/example/android_programming/di/AppContainer.kt b/app/src/main/java/com/example/android_programming/di/AppContainer.kt index 45785aa..6031d8f 100644 --- a/app/src/main/java/com/example/android_programming/di/AppContainer.kt +++ b/app/src/main/java/com/example/android_programming/di/AppContainer.kt @@ -15,7 +15,7 @@ interface AppContainer { val basketRepo: BasketRepository companion object { - const val TIMEOUT = 5000L + const val TIMEOUT = 500L const val LIMIT = 10 } } \ No newline at end of file