Yes, i'am the badAssss

This commit is contained in:
maxnes3 2023-12-27 20:56:03 +04:00
parent 153661a5f0
commit 7baad4c44e
7 changed files with 98 additions and 36 deletions

View File

@ -5,6 +5,7 @@ import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import androidx.room.withTransaction
import com.example.mobileapp.GlobalUser
import com.example.mobileapp.api.model.toStory
import com.example.mobileapp.api.model.toUser
import com.example.mobileapp.database.MobileAppDataBase
@ -18,7 +19,7 @@ import retrofit2.HttpException
import java.io.IOException
@OptIn(ExperimentalPagingApi::class)
class ServiceRemoteMediator(private val service: ServerService,
class StoryRemoteMediator(private val service: ServerService,
private val storyRepository: OfflineStoryRepository,
private val userRepository: OfflineUserRepository,
private val database: MobileAppDataBase,
@ -51,8 +52,8 @@ class ServiceRemoteMediator(private val service: ServerService,
}
try {
val users = service.getUsers().map { it.toUser() }
val stories = service.getStories(page, state.config.pageSize).map { it.toStory() }
val user = GlobalUser.getInstance().getUser()
val stories = service.getUserStories(page, state.config.pageSize, user!!.id!!).map { it.toStory() }
val endOfPaginationReached = stories.isEmpty()
database.withTransaction {
if (loadType == LoadType.REFRESH) {
@ -71,7 +72,7 @@ class ServiceRemoteMediator(private val service: ServerService,
)
}
dbRemoteKeyRepository.createRemoteKeys(keys)
userRepository.insertUsers(users)
userRepository.insertUser(user)
storyRepository.insertStories(stories)
}
return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)

View File

@ -8,19 +8,16 @@ import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.mobileapp.MobileAppContainer
import com.example.mobileapp.api.ServerService
import com.example.mobileapp.api.ServiceRemoteMediator
import com.example.mobileapp.api.model.toMail
import com.example.mobileapp.api.StoryRemoteMediator
import com.example.mobileapp.api.model.toStory
import com.example.mobileapp.api.model.toStoryRemote
import com.example.mobileapp.database.MobileAppDataBase
import com.example.mobileapp.database.entities.Mail
import com.example.mobileapp.database.entities.Story
import com.example.mobileapp.database.repositories.OfflineStoryRepository
import com.example.mobileapp.database.repositories.OfflineUserRepository
import com.example.mobileapp.database.repositories.RemoteKeysRepositoryImpl
import com.example.mobileapp.database.repositories.StoryRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
class RestStoryRepository(private var service: ServerService,
private val dbStoryRepository: OfflineStoryRepository,
@ -30,7 +27,7 @@ class RestStoryRepository(private var service: ServerService,
): StoryRepository {
override fun getAllStories(): Flow<PagingData<Story>> {
val pagingSourceFactory = {
/*val pagingSourceFactory = {
dbStoryRepository.getAllStoriesPagingSource()
}
@ -40,7 +37,36 @@ class RestStoryRepository(private var service: ServerService,
pageSize = MobileAppContainer.LIMIT,
enablePlaceholders = false
),
remoteMediator = ServiceRemoteMediator(
remoteMediator = StoryRemoteMediator(
service,
dbStoryRepository,
dbUserRepository,
database,
dbRemoteKeyRepository,
),
pagingSourceFactory = pagingSourceFactory
).flow*/
return Pager(
config = PagingConfig(
pageSize = MobileAppContainer.LIMIT,
enablePlaceholders = false
),
pagingSourceFactory = { StoryPagingSource(service) }
).flow
}
override fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>> {
val pagingSourceFactory = {
dbStoryRepository.getUserStoriesPagingSource(userId)
}
@OptIn(ExperimentalPagingApi::class)
return Pager(
config = PagingConfig(
pageSize = MobileAppContainer.LIMIT,
enablePlaceholders = false
),
remoteMediator = StoryRemoteMediator(
service,
dbStoryRepository,
dbUserRepository,
@ -50,22 +76,12 @@ class RestStoryRepository(private var service: ServerService,
pagingSourceFactory = pagingSourceFactory
).flow
/*return Pager(
config = PagingConfig(
pageSize = MobileAppContainer.LIMIT,
enablePlaceholders = false
),
pagingSourceFactory = { StoryPagingSource(service) }
).flow*/
}
override fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>> {
return Pager(
config = PagingConfig(
pageSize = MobileAppContainer.LIMIT,
enablePlaceholders = false
),
pagingSourceFactory = { StoryPagingSource(service, userId) }
).flow
).flow*/
}
override suspend fun getStoryById(id: Int): Story? = service.getStory(id).toStory()

View File

@ -32,7 +32,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mobileapp.ui.theme.ButtonColor2
import com.example.mobileapp.ui.theme.ButtonColor1
import com.example.mobileapp.ui.theme.MobileAppTheme
import java.text.SimpleDateFormat
import java.util.Calendar
@ -197,8 +197,8 @@ fun DatePicker(startValue: Long? = null, onDateSelected: (Long) -> Unit) {
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Selected Date: ${dateFormatter.format(selectedDate.value)}")
ActiveButton(label = "Выбрать дату", backgroundColor = ButtonColor2,
textColor = Color.White, onClickAction = {
ActiveButton(label = "Выбрать дату", backgroundColor = ButtonColor1,
textColor = Color.Black, onClickAction = {
datePickerDialog.value = DatePickerDialog(
context,
{ _, year: Int, month: Int, dayOfMonth: Int ->

View File

@ -33,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
@ -247,7 +248,8 @@ fun MailListItem(item: Mail, navController: NavHostController,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(96.dp)
.padding(8.dp))
.padding(8.dp)
.clip(RoundedCornerShape(16.dp)))
Column(
modifier = Modifier.padding(8.dp)
){

View File

@ -19,7 +19,7 @@ interface StoryDao {
suspend fun getById(id: Int): Story?
@Query("select * from stories where stories.user_id = :userId order by stories.id desc")
fun getByUserId(userId: Int): Flow<List<Story>>
fun getByUserId(userId: Int): PagingSource<Int, Story>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(vararg story: Story)

View File

@ -25,7 +25,18 @@ class OfflineStoryRepository(private val storyDao: StoryDao): StoryRepository {
}
override fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>> {
TODO("Not yet implemented")
return Pager(
config = PagingConfig(
pageSize = 5,
prefetchDistance = 1,
enablePlaceholders = true,
initialLoadSize = 10,
maxSize = 15
),
pagingSourceFactory = {
storyDao.getByUserId(userId)
}
).flow
}
override suspend fun getStoryById(id: Int): Story? = storyDao.getById(id)
@ -41,4 +52,6 @@ class OfflineStoryRepository(private val storyDao: StoryDao): StoryRepository {
storyDao.insert(*stories.toTypedArray())
fun getAllStoriesPagingSource(): PagingSource<Int, Story> = storyDao.getAll()
fun getUserStoriesPagingSource(userId: Int): PagingSource<Int, Story> = storyDao.getByUserId(userId)
}

View File

@ -3,12 +3,17 @@ package com.example.mobileapp.screens
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.mobileapp.components.ActiveButton
@ -45,11 +50,36 @@ fun ReportScreen(
})
}
else{
Text(text = "Дата с ${dateFormat.format(reportViewModel.report.value?.dateFrom?.let { Date(it) })}")
Text(text = "Дата по ${dateFormat.format(reportViewModel.report.value?.dateTo?.let { Date(it) })}")
Text(text = "Кол-в публикаций за период: ${reportViewModel.report.value?.postCount}")
Text(text = "Наибольшее число публикаций у: ${reportViewModel.report.value?.mostPostAuthor}")
Text(text = "Кол-во публикаций у пользователя:${reportViewModel.report.value?.mostPostCount}")
Text(text = "Отчёт по публикациям иллюстраций",
fontSize = 28.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
Text(text = "Дата с ${dateFormat.format(reportViewModel.report.value?.dateFrom?.let { Date(it) })}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
Text(text = "Дата по ${dateFormat.format(reportViewModel.report.value?.dateTo?.let { Date(it) })}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
Text(text = "Кол-в публикаций за период: ${reportViewModel.report.value?.postCount}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
Text(text = "Наибольшее число публикаций у: ${reportViewModel.report.value?.mostPostAuthor}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
Text(text = "Кол-во публикаций у пользователя:${reportViewModel.report.value?.mostPostCount}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp))
ActiveButton(label = "Сбросить", backgroundColor = ButtonColor2,
textColor = Color.White, onClickAction = {
reportViewModel.clearReport()