Compare commits
No commits in common. "fb7e7a1a43d543022956da6cd21dd97742cc5651" and "69175e82f08b6cab4976c64d9afb8c00987796ce" have entirely different histories.
fb7e7a1a43
...
69175e82f0
@ -76,8 +76,4 @@ dependencies {
|
||||
ksp("androidx.room:room-compiler:$room_version")
|
||||
implementation("androidx.room:room-ktx:$room_version")
|
||||
implementation("androidx.room:room-paging:$room_version")
|
||||
|
||||
//Paging
|
||||
implementation ("androidx.paging:paging-compose:3.2.1")
|
||||
implementation ("androidx.paging:paging-runtime:3.2.1")
|
||||
}
|
@ -3,7 +3,6 @@
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".MobileApp"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
@ -1,12 +0,0 @@
|
||||
package com.example.mobileapp
|
||||
|
||||
import android.app.Application
|
||||
|
||||
class MobileApp: Application() {
|
||||
lateinit var container: MobileAppContainer
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
container = MobileAppDataContainer(this)
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package com.example.mobileapp
|
||||
|
||||
import android.content.Context
|
||||
import com.example.mobileapp.database.MobileAppDataBase
|
||||
import com.example.mobileapp.database.repositories.MailRepository
|
||||
import com.example.mobileapp.database.repositories.OfflineMailRepository
|
||||
import com.example.mobileapp.database.repositories.OfflineStoryRepository
|
||||
import com.example.mobileapp.database.repositories.OfflineUserRepository
|
||||
import com.example.mobileapp.database.repositories.StoryRepository
|
||||
import com.example.mobileapp.database.repositories.UserRepository
|
||||
|
||||
interface MobileAppContainer {
|
||||
val mailRepository: MailRepository
|
||||
val storyRepository: StoryRepository
|
||||
val userRepository: UserRepository
|
||||
}
|
||||
|
||||
class MobileAppDataContainer(private val context: Context): MobileAppContainer {
|
||||
override val mailRepository: MailRepository by lazy {
|
||||
OfflineMailRepository(MobileAppDataBase.getInstance(context).mailDao())
|
||||
}
|
||||
|
||||
override val storyRepository: StoryRepository by lazy {
|
||||
OfflineStoryRepository(MobileAppDataBase.getInstance(context).storyDao())
|
||||
}
|
||||
|
||||
override val userRepository: UserRepository by lazy {
|
||||
OfflineUserRepository(MobileAppDataBase.getInstance(context).userDao())
|
||||
}
|
||||
|
||||
companion object{
|
||||
const val TIMEOUT = 5000L
|
||||
}
|
||||
}
|
@ -82,14 +82,13 @@ fun PasswordInputField(label: String, startValue: String? = null, onPasswordChan
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SearchInputField(onTextChanged: (String) -> Unit){
|
||||
fun SearchInputField(){
|
||||
var text = remember { mutableStateOf("") }
|
||||
|
||||
OutlinedTextField(
|
||||
value = text.value,
|
||||
onValueChange = {
|
||||
text.value = it
|
||||
onTextChanged(it)
|
||||
},
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
|
@ -14,8 +14,6 @@ import androidx.compose.foundation.layout.requiredHeight
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.AlertDialog
|
||||
@ -38,22 +36,15 @@ import androidx.compose.ui.res.painterResource
|
||||
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.NavHostController
|
||||
import androidx.paging.compose.LazyPagingItems
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.mobileapp.R
|
||||
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.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.StoryViewModel
|
||||
import com.example.mobileapp.database.viewmodels.UserViewModel
|
||||
import com.example.mobileapp.ui.theme.BackgroundItem2
|
||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
@ -76,7 +67,7 @@ fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>
|
||||
items(dataList){ item ->
|
||||
when(item){
|
||||
is Story -> StoryListItem(item = item, navController = navController)
|
||||
is Mail -> MailListItem(item = item, navController = navController)
|
||||
is Mail -> MailListItem(item = item)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -87,11 +78,9 @@ inline fun <reified T> List<*>.isListOf(): Boolean {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun StoryListItem(item: Story, navController: NavHostController,
|
||||
isReadOnly: Boolean? = false,
|
||||
storyViewModel: StoryViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
fun StoryListItem(item: Story, navController: NavHostController){
|
||||
val context = LocalContext.current
|
||||
|
||||
val isExpanded = remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
@ -100,6 +89,10 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
val delete = remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -142,13 +135,6 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
||||
visible = isExpanded.value,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
if (isReadOnly!!){
|
||||
DataListItemButton(label = "Подробнее", backgroundColor = ButtonColor2,
|
||||
textColor = Color.White, onClickAction = {
|
||||
navController.navigate("viewstory/${item.id}")
|
||||
})
|
||||
}
|
||||
else{
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End
|
||||
@ -163,17 +149,26 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(showDialog.value) {
|
||||
DialogWindow(label = "Подтверждение",
|
||||
message = "Вы уверены что хотите удалить запись?", onConfirmAction = {
|
||||
storyViewModel.deleteStory(item)
|
||||
delete.value = !delete.value
|
||||
showDialog.value = !showDialog.value
|
||||
}, onDismissAction = {
|
||||
showDialog.value = !showDialog.value
|
||||
})
|
||||
}
|
||||
|
||||
if(delete.value) {
|
||||
LaunchedEffect(Unit){
|
||||
withContext(Dispatchers.IO){
|
||||
MobileAppDataBase.getInstance(context).storyDao().delete(item)
|
||||
}
|
||||
}
|
||||
delete.value = !delete.value
|
||||
navController.navigate("story")
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@ -195,24 +190,11 @@ fun DataListItemButton(label: String, backgroundColor: Color, textColor: Color,
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun MailListItem(item: Mail, navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
fun MailListItem(item: Mail){
|
||||
val isExpanded = remember {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
val userName = remember { mutableStateOf("") }
|
||||
|
||||
LaunchedEffect(Unit){
|
||||
userViewModel.getUser(item.userId).collect {
|
||||
if (it != null) {
|
||||
userName.value = it.email
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -244,7 +226,7 @@ fun MailListItem(item: Mail, navController: NavHostController,
|
||||
modifier = Modifier.padding(8.dp)
|
||||
){
|
||||
Text(
|
||||
text = "${userName.value} | ${dateFormat.format(Date(item.postdate!!))}",
|
||||
text = "item.username | ${dateFormat.format(Date(item.postdate!!))}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold)
|
||||
Text(text = item.message)
|
||||
@ -254,10 +236,22 @@ fun MailListItem(item: Mail, navController: NavHostController,
|
||||
visible = isExpanded.value,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
DataListItemButton(label = "Подробнее", backgroundColor = ButtonColor2,
|
||||
textColor = Color.White, onClickAction = {
|
||||
navController.navigate("viewmail/${item.id}")
|
||||
})
|
||||
Button(
|
||||
onClick = { /* Действие при нажатии кнопки */ },
|
||||
modifier = Modifier
|
||||
.requiredHeight(64.dp)
|
||||
.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(10.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = ButtonColor2
|
||||
)
|
||||
) {
|
||||
Text(
|
||||
text = "Подробнее",
|
||||
color = Color.White,
|
||||
fontSize = 18.sp,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,17 +18,13 @@ import androidx.compose.material3.NavigationBar
|
||||
import androidx.compose.material3.NavigationBarItem
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.Font
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
@ -41,13 +37,11 @@ import com.example.mobileapp.screens.Authorization
|
||||
import com.example.mobileapp.screens.EditMailScreen
|
||||
import com.example.mobileapp.screens.EditStoryScreen
|
||||
import com.example.mobileapp.screens.EditUserScreen
|
||||
import com.example.mobileapp.screens.ListDataScreen
|
||||
import com.example.mobileapp.screens.ListMailScreen
|
||||
import com.example.mobileapp.screens.ListStoryScreen
|
||||
import com.example.mobileapp.screens.MailViewScreen
|
||||
import com.example.mobileapp.screens.MainScreen
|
||||
import com.example.mobileapp.screens.Registration
|
||||
import com.example.mobileapp.screens.SettingsScreen
|
||||
import com.example.mobileapp.screens.StoryViewScreen
|
||||
|
||||
val navBarItems = listOf(
|
||||
NavBarItem(route = "main", label = "Главная", icon = R.drawable.home),
|
||||
@ -59,32 +53,9 @@ val navBarItems = listOf(
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun NavBar(navController: NavHostController) {
|
||||
val topBarState = rememberSaveable { (mutableStateOf(false)) }
|
||||
val bottomBarState = rememberSaveable { (mutableStateOf(false)) }
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
AnimatedVisibility(
|
||||
visible = topBarState.value,
|
||||
enter = slideInVertically(initialOffsetY = { it }),
|
||||
exit = slideOutVertically(targetOffsetY = { it }),
|
||||
content = {
|
||||
TopAppBar(
|
||||
title = {
|
||||
Text(
|
||||
text = "Storyteller!",
|
||||
textAlign = TextAlign.Center,
|
||||
fontFamily = FontFamily(
|
||||
Font(
|
||||
R.font.irishgrover_regular, FontWeight.Bold
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
bottomBar = {
|
||||
AnimatedVisibility(
|
||||
visible = bottomBarState.value,
|
||||
@ -127,37 +98,30 @@ fun NavBar(navController: NavHostController) {
|
||||
modifier = Modifier.padding(innerPaddings)
|
||||
) {
|
||||
composable("authorization"){
|
||||
topBarState.value = true
|
||||
bottomBarState.value = false
|
||||
Authorization(navController = navController)
|
||||
}
|
||||
composable("registration"){
|
||||
topBarState.value = true
|
||||
bottomBarState.value = false
|
||||
Registration(navController = navController)
|
||||
}
|
||||
composable("main"){
|
||||
topBarState.value = false
|
||||
bottomBarState.value = true
|
||||
MainScreen(navController = navController)
|
||||
}
|
||||
composable("story"){
|
||||
topBarState.value = false
|
||||
bottomBarState.value = true
|
||||
ListStoryScreen(navController = navController)
|
||||
ListDataScreen(navController = navController)
|
||||
}
|
||||
composable("mail"){
|
||||
topBarState.value = false
|
||||
bottomBarState.value = true
|
||||
ListMailScreen(navController = navController)
|
||||
}
|
||||
composable("settings"){
|
||||
topBarState.value = true
|
||||
bottomBarState.value = true
|
||||
SettingsScreen(navController = navController)
|
||||
}
|
||||
composable("editstory"){ // Без аргумента
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
EditStoryScreen(navController = navController)
|
||||
}
|
||||
@ -166,41 +130,18 @@ fun NavBar(navController: NavHostController) {
|
||||
arguments = listOf(navArgument("id") { type = NavType.IntType }) //С аргументом
|
||||
) { backStackEntry ->
|
||||
backStackEntry.arguments?.let {
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
EditStoryScreen(navController = navController, storyId = it.getInt("id"))
|
||||
}
|
||||
}
|
||||
composable("editmail"){ // Без аргумента
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
EditMailScreen(navController = navController)
|
||||
}
|
||||
composable("edituser"){
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
EditUserScreen(navController = navController)
|
||||
}
|
||||
composable(
|
||||
"viewstory/{id}",
|
||||
arguments = listOf(navArgument("id") { type = NavType.IntType }) //С аргументом
|
||||
) { backStackEntry ->
|
||||
backStackEntry.arguments?.let {
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
StoryViewScreen(navController = navController, storyId = it.getInt("id"))
|
||||
}
|
||||
}
|
||||
composable(
|
||||
"viewmail/{id}",
|
||||
arguments = listOf(navArgument("id") { type = NavType.IntType }) //С аргументом
|
||||
) { backStackEntry ->
|
||||
backStackEntry.arguments?.let {
|
||||
topBarState.value = false
|
||||
bottomBarState.value = false
|
||||
MailViewScreen(navController = navController, mailId = it.getInt("id"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,18 +45,10 @@ abstract class MobileAppDataBase : RoomDatabase() {
|
||||
cover = BitmapFactory.decodeResource(appContext.resources, R.drawable.king), userId = 2))
|
||||
|
||||
val mailDao = database.mailDao()
|
||||
for (i in 0..50){
|
||||
if (i % 2 == 0){
|
||||
mailDao.insert(Mail(message = "Выложил новые страницы", userId = 1))
|
||||
}
|
||||
else{
|
||||
mailDao.insert(Mail(message = "Меня отменили в Твиттере", userId = 2))
|
||||
}
|
||||
}
|
||||
/*mailDao.insert(Mail(message = "Выложил новые страницы", userId = 1))
|
||||
mailDao.insert(Mail(message = "Меня отменили в Твиттере", userId = 2))*/
|
||||
}
|
||||
}
|
||||
|
||||
fun getInstance(appContext: Context): MobileAppDataBase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.example.mobileapp.database.dao
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
@ -8,16 +7,15 @@ import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.example.mobileapp.database.entities.Mail
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface MailDao {
|
||||
@Query("select * from mails order by id desc")
|
||||
fun getAll(): PagingSource<Int, Mail>
|
||||
@Query("select * from mails")
|
||||
fun getAll(): Flow<List<Mail>>
|
||||
|
||||
@Query("select * from mails where mails.id = :id")
|
||||
fun getById(id: Int): Flow<Mail?>
|
||||
fun getById(id: Int): Mail?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(mail: Mail)
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.example.mobileapp.database.dao
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
@ -12,14 +11,14 @@ import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface StoryDao {
|
||||
@Query("select * from stories order by id desc")
|
||||
fun getAll(): PagingSource<Int, Story>
|
||||
@Query("select * from stories")
|
||||
fun getAll(): Flow<List<Story>>
|
||||
|
||||
@Query("select * from stories where stories.id = :id")
|
||||
fun getById(id: Int): Flow<Story?>
|
||||
fun getById(id: Int): Story?
|
||||
|
||||
@Query("select * from stories where stories.user_id = :userId order by stories.id desc")
|
||||
fun getByUserId(userId: Int): PagingSource<Int, Story>
|
||||
@Query("select * from stories where stories.user_id = :userId")
|
||||
fun getByUserId(userId: Int): Flow<List<Story>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(story: Story)
|
||||
|
@ -12,13 +12,10 @@ import kotlinx.coroutines.flow.Flow
|
||||
@Dao
|
||||
interface UserDao {
|
||||
@Query("select * from users")
|
||||
fun getAll(): Flow<List<User>>
|
||||
fun getAll():Flow<List<User>>
|
||||
|
||||
@Query("select * from users where users.id = :id")
|
||||
fun getById(id: Int): Flow<User?>
|
||||
|
||||
@Query("select * from users where users.login = :login")
|
||||
suspend fun getByLogin(login: String): User?
|
||||
fun getById(id: Int): User?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(user: User)
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import androidx.paging.PagingData
|
||||
import com.example.mobileapp.database.entities.Mail
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface MailRepository {
|
||||
fun getAllMails(): Flow<PagingData<Mail>>
|
||||
|
||||
fun getMail(id: Int): Flow<Mail?>
|
||||
|
||||
suspend fun insertMail(mail: Mail)
|
||||
|
||||
suspend fun updateMail(mail: Mail)
|
||||
|
||||
suspend fun deleteMail(mail: Mail)
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import com.example.mobileapp.database.dao.MailDao
|
||||
import com.example.mobileapp.database.entities.Mail
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineMailRepository(private val mailDao: MailDao): MailRepository {
|
||||
override fun getAllMails(): Flow<PagingData<Mail>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 8,
|
||||
prefetchDistance = 2,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 12,
|
||||
maxSize = 24
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
mailDao.getAll()
|
||||
}
|
||||
).flow
|
||||
}
|
||||
|
||||
override fun getMail(id: Int): Flow<Mail?> = mailDao.getById(id)
|
||||
|
||||
override suspend fun insertMail(mail: Mail) = mailDao.insert(mail)
|
||||
|
||||
override suspend fun updateMail(mail: Mail) = mailDao.update(mail)
|
||||
|
||||
override suspend fun deleteMail(mail: Mail) = mailDao.delete(mail)
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import com.example.mobileapp.database.dao.StoryDao
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineStoryRepository(private val storyDao: StoryDao): StoryRepository {
|
||||
override fun getAllStories(): Flow<PagingData<Story>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 5,
|
||||
prefetchDistance = 1,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 10,
|
||||
maxSize = 15
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
storyDao.getAll()
|
||||
}
|
||||
).flow
|
||||
}
|
||||
|
||||
override fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 5,
|
||||
prefetchDistance = 1,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 10,
|
||||
maxSize = 15
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
storyDao.getByUserId(userId)
|
||||
}
|
||||
).flow
|
||||
}
|
||||
|
||||
override fun getStoryById(id: Int): Flow<Story?> = storyDao.getById(id)
|
||||
|
||||
override suspend fun insertStory(story: Story) = storyDao.insert(story)
|
||||
|
||||
override suspend fun updateStory(story: Story) = storyDao.update(story)
|
||||
|
||||
override suspend fun deleteStory(story: Story) = storyDao.delete(story)
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import com.example.mobileapp.database.dao.UserDao
|
||||
import com.example.mobileapp.database.entities.User
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineUserRepository(private val userDao: UserDao): UserRepository {
|
||||
override fun getAllUsers(): Flow<List<User>> = userDao.getAll()
|
||||
|
||||
override fun getUser(id: Int): Flow<User?> = userDao.getById(id)
|
||||
|
||||
override suspend fun getUserByLogin(login: String): User? = userDao.getByLogin(login)
|
||||
|
||||
override suspend fun insertUser(user: User) = userDao.insert(user)
|
||||
|
||||
override suspend fun updateUser(user: User) = userDao.update(user)
|
||||
|
||||
override suspend fun deleteUser(user: User) = userDao.delete(user)
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.PagingSource
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface StoryRepository {
|
||||
fun getAllStories(): Flow<PagingData<Story>>
|
||||
|
||||
fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>>
|
||||
|
||||
fun getStoryById(id: Int): Flow<Story?>
|
||||
|
||||
suspend fun insertStory(story: Story)
|
||||
|
||||
suspend fun updateStory(story: Story)
|
||||
|
||||
suspend fun deleteStory(story: Story)
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.example.mobileapp.database.repositories
|
||||
|
||||
import com.example.mobileapp.database.entities.User
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface UserRepository {
|
||||
fun getAllUsers(): Flow<List<User>>
|
||||
|
||||
fun getUser(id: Int): Flow<User?>
|
||||
|
||||
suspend fun getUserByLogin(login: String): User?
|
||||
|
||||
suspend fun insertUser(user: User)
|
||||
|
||||
suspend fun updateUser(user: User)
|
||||
|
||||
suspend fun deleteUser(user: User)
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.example.mobileapp.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
import com.example.mobileapp.database.entities.Mail
|
||||
import com.example.mobileapp.database.repositories.MailRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MailViewModel(private val mailRepository: MailRepository): ViewModel() {
|
||||
val getAllMails: Flow<PagingData<Mail>> = mailRepository.getAllMails().cachedIn(viewModelScope)
|
||||
|
||||
fun getMail(id: Int): Flow<Mail?> = mailRepository.getMail(id)
|
||||
|
||||
fun insertMail(mail: Mail) = viewModelScope.launch {
|
||||
mailRepository.insertMail(mail)
|
||||
}
|
||||
|
||||
fun updateMail(mail: Mail) = viewModelScope.launch {
|
||||
mailRepository.updateMail(mail)
|
||||
}
|
||||
|
||||
fun deleteMail(mail: Mail) = viewModelScope.launch {
|
||||
mailRepository.deleteMail(mail)
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.example.mobileapp.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import com.example.mobileapp.MobileApp
|
||||
|
||||
object MobileAppViewModelProvider {
|
||||
val Factory = viewModelFactory {
|
||||
initializer {
|
||||
MailViewModel(app().container.mailRepository)
|
||||
}
|
||||
initializer {
|
||||
StoryViewModel(app().container.storyRepository)
|
||||
}
|
||||
initializer {
|
||||
UserViewModel(app().container.userRepository)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun CreationExtras.app(): MobileApp =
|
||||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MobileApp)
|
@ -1,31 +0,0 @@
|
||||
package com.example.mobileapp.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import com.example.mobileapp.database.repositories.StoryRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class StoryViewModel(private val storyRepository: StoryRepository): ViewModel() {
|
||||
|
||||
val getAllStories: Flow<PagingData<Story>> = storyRepository.getAllStories().cachedIn(viewModelScope)
|
||||
|
||||
fun getStoryById(id: Int): Flow<Story?> = storyRepository.getStoryById(id)
|
||||
|
||||
fun getStoriesByUserId(userId: Int): Flow<PagingData<Story>> = storyRepository.getStoriesByUserId(userId).cachedIn(viewModelScope)
|
||||
|
||||
fun insertStory(story: Story) = viewModelScope.launch {
|
||||
storyRepository.insertStory(story)
|
||||
}
|
||||
|
||||
fun updateStory(story: Story) = viewModelScope.launch {
|
||||
storyRepository.updateStory(story)
|
||||
}
|
||||
|
||||
fun deleteStory(story: Story) = viewModelScope.launch {
|
||||
storyRepository.deleteStory(story)
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package com.example.mobileapp.database.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.mobileapp.GlobalUser
|
||||
import com.example.mobileapp.database.entities.User
|
||||
import com.example.mobileapp.database.repositories.UserRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class UserViewModel(private val userRepository: UserRepository): ViewModel() {
|
||||
val getAllUsers = userRepository.getAllUsers()
|
||||
|
||||
fun getUser(id: Int): Flow<User?> = userRepository.getUser(id)
|
||||
|
||||
fun updateUser(user: User) = viewModelScope.launch {
|
||||
if (user.login.isEmpty()){
|
||||
return@launch
|
||||
}
|
||||
|
||||
if (user.email.isEmpty() || !isValidEmail(user.email)){
|
||||
return@launch
|
||||
}
|
||||
|
||||
if (user.password.isEmpty()){
|
||||
return@launch
|
||||
}
|
||||
userRepository.updateUser(user)
|
||||
GlobalUser.getInstance().setUser(user)
|
||||
}
|
||||
|
||||
fun deleteUser(user: User) = viewModelScope.launch {
|
||||
userRepository.deleteUser(user)
|
||||
}
|
||||
|
||||
fun regUser(user: User) = viewModelScope.launch {
|
||||
val globalUser = userRepository.getUserByLogin(user.login)
|
||||
globalUser?.let {
|
||||
return@launch
|
||||
} ?: run {
|
||||
if(user.password.isEmpty()){
|
||||
return@launch
|
||||
}
|
||||
|
||||
if(user.email.isEmpty() || !isValidEmail(user.email)){
|
||||
return@launch
|
||||
}
|
||||
userRepository.insertUser(user)
|
||||
GlobalUser.getInstance().setUser(userRepository.getUserByLogin(user.login))
|
||||
}
|
||||
}
|
||||
|
||||
fun authUser(user: User) = viewModelScope.launch {
|
||||
val globalUser = userRepository.getUserByLogin(user.login)
|
||||
globalUser?.let {
|
||||
if (user.password.isNotEmpty() && user.password == globalUser.password){
|
||||
GlobalUser.getInstance().setUser(globalUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValidEmail(email: String): Boolean {
|
||||
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
|
||||
}
|
||||
}
|
@ -8,9 +8,7 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@ -19,32 +17,31 @@ import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.ActiveButton
|
||||
import com.example.mobileapp.components.NavigationButton
|
||||
import com.example.mobileapp.components.PasswordInputField
|
||||
import com.example.mobileapp.components.PlaceholderInputField
|
||||
import com.example.mobileapp.database.MobileAppDataBase
|
||||
import com.example.mobileapp.database.entities.User
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.UserViewModel
|
||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun Authorization(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val users = userViewModel.getAllUsers.collectAsState(emptyList()).value
|
||||
|
||||
val login = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
fun Authorization(navController: NavHostController){
|
||||
val context = LocalContext.current
|
||||
val users = remember { mutableStateListOf<User>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
MobileAppDataBase.getInstance(context).userDao().getAll().collect { data ->
|
||||
users.clear()
|
||||
users.addAll(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -56,28 +53,13 @@ fun Authorization(navController: NavHostController,
|
||||
contentDescription = "login",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.size(448.dp)
|
||||
.size(512.dp)
|
||||
.padding(8.dp)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
PlaceholderInputField(label = "Логин", isSingleLine = true, onTextChanged = {newlogin ->
|
||||
login.value = newlogin
|
||||
})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = {newpassword ->
|
||||
password.value = newpassword
|
||||
})
|
||||
ActiveButton(label = "Вход", backgroundColor = ButtonColor2,
|
||||
textColor = Color.White, onClickAction = {
|
||||
if (login.value.isNotEmpty() && password.value.isNotEmpty()) {
|
||||
userViewModel.authUser(
|
||||
User(
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
email = String()
|
||||
)
|
||||
)
|
||||
navController.navigate("main")
|
||||
}
|
||||
})
|
||||
PlaceholderInputField(label = "Логин", isSingleLine = true, onTextChanged = {})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = {})
|
||||
NavigationButton(navController = navController, destination = "main", label = "Вход",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
NavigationButton(navController = navController, destination = "registration", label = "Регистрация",
|
||||
backgroundColor = ButtonColor1, textColor = Color.Black)
|
||||
}
|
||||
|
@ -9,51 +9,40 @@ import android.provider.MediaStore
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
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
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.mobileapp.GlobalUser
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.ActiveButton
|
||||
import com.example.mobileapp.components.NavigationButton
|
||||
import com.example.mobileapp.components.PasswordInputField
|
||||
import com.example.mobileapp.components.PlaceholderInputField
|
||||
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.entities.User
|
||||
import com.example.mobileapp.database.viewmodels.MailViewModel
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.StoryViewModel
|
||||
import com.example.mobileapp.database.viewmodels.UserViewModel
|
||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
||||
storyViewModel: StoryViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
fun EditStoryScreen(navController: NavHostController, storyId: Int? = null) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val cover = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.editplaceholder)) }
|
||||
@ -69,6 +58,7 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
||||
if (Build.VERSION.SDK_INT < 28) {
|
||||
cover.value = MediaStore.Images
|
||||
.Media.getBitmap(context.contentResolver, imageData.value)
|
||||
|
||||
} else {
|
||||
val source = ImageDecoder
|
||||
.createSource(context.contentResolver, imageData.value!!)
|
||||
@ -76,20 +66,46 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
||||
}
|
||||
}
|
||||
|
||||
storyId?.let{
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val story = MobileAppDataBase.getInstance(context).storyDao().getById(storyId!!)
|
||||
cover.value = story!!.cover
|
||||
title.value = story!!.title
|
||||
description.value = story!!.description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val edit = remember { mutableStateOf(false) }
|
||||
if (edit.value){
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
storyId?.let {
|
||||
storyViewModel.getStoryById(storyId).collect {
|
||||
if (it != null) {
|
||||
cover.value = it.cover
|
||||
}
|
||||
if (it != null) {
|
||||
title.value = it.title
|
||||
}
|
||||
if (it != null) {
|
||||
description.value = it.description
|
||||
MobileAppDataBase.getInstance(context).storyDao()
|
||||
.update(
|
||||
Story(
|
||||
id = storyId,
|
||||
title = title.value,
|
||||
description = description.value,
|
||||
cover = cover.value,
|
||||
userId = 1)
|
||||
)
|
||||
|
||||
} ?: run {
|
||||
MobileAppDataBase.getInstance(context).storyDao()
|
||||
.insert(
|
||||
Story(
|
||||
title = title.value,
|
||||
description = description.value,
|
||||
cover = cover.value,
|
||||
userId = 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
edit.value = !edit.value
|
||||
navController.navigate("story")
|
||||
}
|
||||
|
||||
Column(
|
||||
@ -118,27 +134,7 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
||||
description.value = newDescription
|
||||
})
|
||||
ActiveButton(label = "Сохранить", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
||||
storyId?.let {
|
||||
storyViewModel.updateStory(
|
||||
Story(
|
||||
id = storyId,
|
||||
cover = cover.value,
|
||||
title = title.value,
|
||||
description = description.value,
|
||||
userId = GlobalUser.getInstance().getUser()?.id!!
|
||||
)
|
||||
)
|
||||
} ?: run {
|
||||
storyViewModel.insertStory(
|
||||
Story(
|
||||
cover = cover.value,
|
||||
title = title.value,
|
||||
description = description.value,
|
||||
userId = GlobalUser.getInstance().getUser()?.id!!
|
||||
)
|
||||
)
|
||||
}
|
||||
navController.navigate("story")
|
||||
edit.value = !edit.value
|
||||
})
|
||||
NavigationButton(navController = navController, destination = "story", label = "Назад",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
@ -146,12 +142,22 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditMailScreen(navController: NavHostController,
|
||||
mailViewModel: MailViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
fun EditMailScreen(navController: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val message = remember { mutableStateOf("") }
|
||||
|
||||
val create = remember { mutableStateOf(false) }
|
||||
if(create.value){
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
MobileAppDataBase.getInstance(context).mailDao()
|
||||
.insert(Mail(message = message.value, userId = 2))
|
||||
}
|
||||
}
|
||||
create.value = !create.value
|
||||
navController.navigate("mail")
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -170,13 +176,7 @@ fun EditMailScreen(navController: NavHostController,
|
||||
message.value = newmessage
|
||||
})
|
||||
ActiveButton(label = "Сохранить", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
||||
mailViewModel.insertMail(
|
||||
Mail(
|
||||
message = message.value,
|
||||
userId = GlobalUser.getInstance().getUser()?.id!!
|
||||
)
|
||||
)
|
||||
navController.navigate("mail")
|
||||
create.value = !create.value
|
||||
})
|
||||
NavigationButton(navController = navController, destination = "mail", label = "Назад",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
@ -184,15 +184,11 @@ fun EditMailScreen(navController: NavHostController,
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditUserScreen(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
fun EditUserScreen(navController: NavHostController){
|
||||
val context = LocalContext.current
|
||||
|
||||
var userId = remember { mutableStateOf(0) }
|
||||
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
|
||||
val login = remember { mutableStateOf("") }
|
||||
val name = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
val email = remember { mutableStateOf("") }
|
||||
|
||||
@ -205,6 +201,7 @@ fun EditUserScreen(navController: NavHostController,
|
||||
if (Build.VERSION.SDK_INT < 28) {
|
||||
photo.value = MediaStore.Images
|
||||
.Media.getBitmap(context.contentResolver, imageData.value)
|
||||
|
||||
} else {
|
||||
val source = ImageDecoder
|
||||
.createSource(context.contentResolver, imageData.value!!)
|
||||
@ -212,17 +209,6 @@ fun EditUserScreen(navController: NavHostController,
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
GlobalUser.getInstance().getUser()?.let { user ->
|
||||
if (user!!.photo != null)
|
||||
photo.value = user!!.photo!!
|
||||
userId.value = user!!.id!!
|
||||
login.value = user!!.login
|
||||
password.value = user!!.password
|
||||
email.value = user!!.email
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -234,20 +220,15 @@ fun EditUserScreen(navController: NavHostController,
|
||||
contentDescription = "editplaceholder",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.clip(CircleShape)
|
||||
.size(384.dp)
|
||||
.border(
|
||||
width = 2.dp,
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
.padding(8.dp)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
ActiveButton(label = "Выбрать фото", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
||||
launcher.launch("image/*")
|
||||
})
|
||||
PlaceholderInputField(label = "Никнейм", isSingleLine = true,
|
||||
startValue = login.value, onTextChanged = { newLogin ->
|
||||
login.value = newLogin
|
||||
startValue = name.value, onTextChanged = { newName ->
|
||||
name.value = newName
|
||||
})
|
||||
PlaceholderInputField(label = "Пароль", isSingleLine = true,
|
||||
startValue = password.value, onTextChanged = { newPassword ->
|
||||
@ -258,20 +239,9 @@ fun EditUserScreen(navController: NavHostController,
|
||||
email.value = newEmail
|
||||
})
|
||||
ActiveButton(label = "Сохранить", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
||||
userViewModel.updateUser(
|
||||
User(
|
||||
id = userId.value,
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
email = email.value,
|
||||
photo = photo.value
|
||||
)
|
||||
)
|
||||
navController.navigate("settings")
|
||||
})
|
||||
ActiveButton(label = "Назад", backgroundColor = ButtonColor2, textColor = Color.White,
|
||||
onClickAction = {
|
||||
navController.navigate("settings")
|
||||
//edit.value = !edit.value
|
||||
})
|
||||
NavigationButton(navController = navController, destination = "story", label = "Назад",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.example.mobileapp.screens
|
||||
|
||||
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.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.mobileapp.components.DataListScroll
|
||||
import com.example.mobileapp.components.NavBar
|
||||
import com.example.mobileapp.database.MobileAppDataBase
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import com.example.mobileapp.ui.theme.BackgroundItem1
|
||||
import com.example.mobileapp.ui.theme.BackgroundItem2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun ListDataScreen(navController: NavHostController){
|
||||
|
||||
val context = LocalContext.current
|
||||
val stories = remember { mutableStateListOf<Story>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
MobileAppDataBase.getInstance(context).storyDao().getAll().collect { data ->
|
||||
stories.clear()
|
||||
stories.addAll(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(BackgroundItem1)
|
||||
) {
|
||||
DataListScroll(navController, stories)
|
||||
}
|
||||
}
|
@ -3,61 +3,40 @@ package com.example.mobileapp.screens
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.paging.compose.collectAsLazyPagingItems
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.mobileapp.components.DataListScroll
|
||||
import com.example.mobileapp.components.MailListItem
|
||||
import com.example.mobileapp.components.StoryListItem
|
||||
import com.example.mobileapp.components.addNewListItem
|
||||
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.viewmodels.MailViewModel
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.ui.theme.BackgroundItem1
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun ListMailScreen(navController: NavHostController,
|
||||
mailViewModel: MailViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val mails = mailViewModel.getAllMails.collectAsLazyPagingItems()
|
||||
fun ListMailScreen(navController: NavHostController){
|
||||
|
||||
val context = LocalContext.current
|
||||
val mails = remember { mutableStateListOf<Mail>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
MobileAppDataBase.getInstance(context).mailDao().getAll().collect { data ->
|
||||
mails.clear()
|
||||
mails.addAll(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(BackgroundItem1)
|
||||
) {
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Fixed(1)
|
||||
) {
|
||||
item {
|
||||
addNewListItem(navController, "editmail")
|
||||
}
|
||||
items(
|
||||
count = mails.itemCount,
|
||||
key = mails.itemKey { item -> item.id!! }
|
||||
) { index: Int ->
|
||||
val mail: Mail? = mails[index]
|
||||
if (mail != null) {
|
||||
MailListItem(item = mail, navController = navController)
|
||||
}
|
||||
}
|
||||
}
|
||||
//DataListScroll(navController, mails)
|
||||
DataListScroll(navController, mails)
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package com.example.mobileapp.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.paging.compose.collectAsLazyPagingItems
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.mobileapp.GlobalUser
|
||||
import com.example.mobileapp.components.DataListScroll
|
||||
import com.example.mobileapp.components.StoryListItem
|
||||
import com.example.mobileapp.components.addNewListItem
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.StoryViewModel
|
||||
import com.example.mobileapp.ui.theme.BackgroundItem1
|
||||
|
||||
@Composable
|
||||
fun ListStoryScreen(navController: NavHostController,
|
||||
storyViewModel: StoryViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val stories = storyViewModel.getStoriesByUserId(GlobalUser.getInstance().getUser()?.id!!).collectAsLazyPagingItems()
|
||||
//val stories = storyViewModel.getAllStories.collectAsLazyPagingItems()
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(BackgroundItem1)
|
||||
) {
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Fixed(1)
|
||||
) {
|
||||
item {
|
||||
addNewListItem(navController, "editstory")
|
||||
}
|
||||
items(
|
||||
count = stories.itemCount,
|
||||
key = stories.itemKey { item -> item.id!! }
|
||||
) { index: Int ->
|
||||
val story: Story? = stories[index]
|
||||
if (story != null) {
|
||||
StoryListItem(item = story, navController = navController)
|
||||
}
|
||||
}
|
||||
}
|
||||
//DataListScroll(navController, stories)
|
||||
}
|
||||
}
|
@ -8,12 +8,8 @@ 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.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
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
|
||||
@ -22,52 +18,22 @@ import androidx.compose.ui.res.painterResource
|
||||
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.NavHostController
|
||||
import androidx.paging.compose.collectAsLazyPagingItems
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.NavBar
|
||||
import com.example.mobileapp.components.SearchInputField
|
||||
import com.example.mobileapp.components.StoryListItem
|
||||
import com.example.mobileapp.components.addNewListItem
|
||||
import com.example.mobileapp.database.entities.Story
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.StoryViewModel
|
||||
|
||||
@Composable
|
||||
fun MainScreen(navController: NavHostController,
|
||||
storyViewModel: StoryViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val stories = storyViewModel.getAllStories.collectAsLazyPagingItems()
|
||||
|
||||
val search = remember { mutableStateOf("") }
|
||||
|
||||
fun MainScreen(navController: NavHostController) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
) {
|
||||
SearchInputField(onTextChanged = {newsearch ->
|
||||
search.value = newsearch
|
||||
})
|
||||
if (stories.itemCount > 0){
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Fixed(1)
|
||||
) {
|
||||
items(
|
||||
count = stories.itemCount,
|
||||
key = stories.itemKey { item -> item.id!! }
|
||||
) { index: Int ->
|
||||
val story: Story? = stories[index]
|
||||
if (story != null && (search.value.isEmpty() || story.title.contains(search.value, ignoreCase = true))) {
|
||||
StoryListItem(item = story, navController = navController, isReadOnly = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
SearchInputField()
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight(0.89f),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
@ -86,5 +52,4 @@ fun MainScreen(navController: NavHostController,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,37 +7,22 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
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.layout.ContentScale
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.ActiveButton
|
||||
import com.example.mobileapp.components.NavigationButton
|
||||
import com.example.mobileapp.components.PasswordInputField
|
||||
import com.example.mobileapp.components.PlaceholderInputField
|
||||
import com.example.mobileapp.database.entities.User
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.UserViewModel
|
||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||
|
||||
@Composable
|
||||
fun Registration(navController: NavHostController,
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val login = remember { mutableStateOf("") }
|
||||
val email = remember { mutableStateOf("") }
|
||||
val password = remember { mutableStateOf("") }
|
||||
val repeatepassword = remember { mutableStateOf("") }
|
||||
|
||||
fun Registration(navController: NavHostController){
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -49,34 +34,15 @@ fun Registration(navController: NavHostController,
|
||||
contentDescription = "registration",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.size(320.dp)
|
||||
.size(384.dp)
|
||||
.padding(8.dp)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
PlaceholderInputField(label = "Логин", isSingleLine = true, onTextChanged = {newlogin ->
|
||||
login.value = newlogin
|
||||
})
|
||||
PlaceholderInputField(label = "Email", isSingleLine = true, onTextChanged = {newemail ->
|
||||
email.value = newemail
|
||||
})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = {newpassword ->
|
||||
password.value = newpassword
|
||||
})
|
||||
PasswordInputField(label = "Пароль ещё раз", onPasswordChanged = {newpassword ->
|
||||
repeatepassword.value = newpassword
|
||||
})
|
||||
ActiveButton(label = "Зарегистрироваться", backgroundColor = ButtonColor2,
|
||||
textColor = Color.White, onClickAction = {
|
||||
if (password.value == repeatepassword.value){
|
||||
userViewModel.regUser(
|
||||
User(
|
||||
login = login.value,
|
||||
password = password.value,
|
||||
email = email.value
|
||||
)
|
||||
)
|
||||
}
|
||||
navController.navigate("main")
|
||||
})
|
||||
PlaceholderInputField(label = "Логин", isSingleLine = true, onTextChanged = {})
|
||||
PlaceholderInputField(label = "Email", isSingleLine = true, onTextChanged = {})
|
||||
PasswordInputField(label = "Пароль", onPasswordChanged = {})
|
||||
PasswordInputField(label = "Пароль ещё раз", onPasswordChanged = {})
|
||||
NavigationButton(navController = navController, destination = "main",
|
||||
label = "Зарегистрироваться", backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
NavigationButton(navController = navController, destination = "authorization",
|
||||
label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black)
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.mobileapp.GlobalUser
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.IconButton
|
||||
import com.example.mobileapp.components.NavBar
|
||||
@ -47,7 +46,7 @@ fun SettingsScreen(navController: NavHostController){
|
||||
contentDescription = "settings",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.size(320.dp)
|
||||
.size(384.dp)
|
||||
.padding(8.dp))
|
||||
IconButton(iconLeft = Icons.Default.AccountCircle, label = "Учётная запись",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White, onClickAction = {
|
||||
@ -67,7 +66,6 @@ fun SettingsScreen(navController: NavHostController){
|
||||
})
|
||||
IconButton(iconLeft = Icons.Default.ExitToApp, label = "Выйти",
|
||||
backgroundColor = Color.Red, textColor = Color.White, onClickAction = {
|
||||
GlobalUser.getInstance().setUser(null)
|
||||
navController.navigate("authorization")
|
||||
})
|
||||
}
|
||||
|
@ -1,168 +0,0 @@
|
||||
package com.example.mobileapp.screens
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
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
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
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.NavHostController
|
||||
import com.example.mobileapp.R
|
||||
import com.example.mobileapp.components.NavigationButton
|
||||
import com.example.mobileapp.database.viewmodels.MailViewModel
|
||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
||||
import com.example.mobileapp.database.viewmodels.StoryViewModel
|
||||
import com.example.mobileapp.database.viewmodels.UserViewModel
|
||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
val dateFormat = SimpleDateFormat("dd.MM.yyyy")
|
||||
|
||||
@Composable
|
||||
fun StoryViewScreen(navController: NavHostController, storyId: Int,
|
||||
storyViewModel: StoryViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val cover = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.editplaceholder)) }
|
||||
val title = remember { mutableStateOf("") }
|
||||
val description = remember { mutableStateOf("") }
|
||||
val postdate = remember { mutableStateOf<Long>(0) }
|
||||
|
||||
val story by storyViewModel.getStoryById(storyId).collectAsState(null)
|
||||
story?.let {
|
||||
cover.value = it.cover
|
||||
title.value = it.title
|
||||
description.value = it.description
|
||||
postdate.value = it.postdate!!
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(bottom = 8.dp),
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Image(
|
||||
bitmap = cover.value.asImageBitmap(),
|
||||
contentDescription = "cover",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.size(512.dp)
|
||||
.padding(8.dp)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
Text(text = "Название: ${title.value}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
Text(text = "Дата публикации: ${dateFormat.format(Date(postdate.value))}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
Text(text = "Описание: ${description.value}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
NavigationButton(navController = navController, destination = "main", label = "Назад",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun MailViewScreen(navController: NavHostController, mailId: Int,
|
||||
mailViewModel: MailViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
),
|
||||
userViewModel: UserViewModel = viewModel(
|
||||
factory = MobileAppViewModelProvider.Factory
|
||||
)) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val userName = remember { mutableStateOf("") }
|
||||
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
|
||||
val message = remember { mutableStateOf("") }
|
||||
val postdate = remember { mutableStateOf<Long>(0) }
|
||||
|
||||
LaunchedEffect(Unit){
|
||||
mailViewModel.getMail(mailId).collect{
|
||||
if (it != null) {
|
||||
message.value = it.message
|
||||
postdate.value = it.postdate!!
|
||||
userViewModel.getUser(it.userId).collect {user ->
|
||||
if (user != null) {
|
||||
if(user.photo != null) {
|
||||
photo.value = user.photo
|
||||
}
|
||||
userName.value = user.email
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(bottom = 8.dp),
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Image(
|
||||
bitmap = photo.value.asImageBitmap(),
|
||||
contentDescription = "editplaceholder",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.clip(CircleShape)
|
||||
.size(384.dp)
|
||||
.border(
|
||||
width = 2.dp,
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
Text(text = "Автор: ${userName.value}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
Text(text = "Дата публикации: ${dateFormat.format(Date(postdate.value))}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
Text(text = "Текст: ${message.value}",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(8.dp))
|
||||
NavigationButton(navController = navController, destination = "mail", label = "Назад",
|
||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user