Compare commits
2 Commits
CourseWork
...
LabWork_02
Author | SHA1 | Date | |
---|---|---|---|
|
105629fa62 | ||
|
469c57088c |
2
.idea/kotlinc.xml
generated
2
.idea/kotlinc.xml
generated
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="KotlinJpsPluginSettings">
|
<component name="KotlinJpsPluginSettings">
|
||||||
<option name="version" value="1.8.20" />
|
<option name="version" value="1.8.10" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,5 +0,0 @@
|
|||||||
# Программирование мобильных устройств
|
|
||||||
|
|
||||||
## ПИбд-32 || Бондаренко М.С.
|
|
||||||
|
|
||||||
## Отчёты загружены в папку reports/
|
|
@ -1,8 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("com.android.application")
|
id("com.android.application")
|
||||||
id("org.jetbrains.kotlin.android")
|
id("org.jetbrains.kotlin.android")
|
||||||
id("com.google.devtools.ksp")
|
|
||||||
kotlin("plugin.serialization") version "1.4.21"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
@ -32,17 +30,17 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = "17"
|
jvmTarget = "1.8"
|
||||||
}
|
}
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
compose = true
|
compose = true
|
||||||
}
|
}
|
||||||
composeOptions {
|
composeOptions {
|
||||||
kotlinCompilerExtensionVersion = "1.4.5"
|
kotlinCompilerExtensionVersion = "1.4.3"
|
||||||
}
|
}
|
||||||
packaging {
|
packaging {
|
||||||
resources {
|
resources {
|
||||||
@ -69,25 +67,4 @@ dependencies {
|
|||||||
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
|
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
|
||||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||||
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
||||||
implementation ("androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0-alpha03")
|
|
||||||
|
|
||||||
// Room
|
|
||||||
val room_version = "2.5.2"
|
|
||||||
implementation("androidx.room:room-runtime:$room_version")
|
|
||||||
annotationProcessor("androidx.room:room-compiler:$room_version")
|
|
||||||
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")
|
|
||||||
|
|
||||||
// retrofit
|
|
||||||
val retrofitVersion = "2.9.0"
|
|
||||||
implementation("com.squareup.retrofit2:retrofit:$retrofitVersion")
|
|
||||||
implementation("com.squareup.okhttp3:logging-interceptor:4.11.0")
|
|
||||||
implementation("androidx.paging:paging-compose:3.2.1")
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
|
|
||||||
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
|
|
||||||
}
|
}
|
@ -1,11 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".MobileApp"
|
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
android:fullBackupContent="@xml/backup_rules"
|
android:fullBackupContent="@xml/backup_rules"
|
||||||
@ -14,8 +11,7 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.MobileApp"
|
android:theme="@style/Theme.MobileApp"
|
||||||
tools:targetApi="31"
|
tools:targetApi="31">
|
||||||
android:networkSecurityConfig="@xml/network_security_config">
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
|
@ -3,14 +3,28 @@ package com.example.mobileapp
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.navigation.NavHostController
|
||||||
|
import androidx.navigation.compose.NavHost
|
||||||
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import com.example.mobileapp.components.NavBar
|
import com.example.mobileapp.entities.Mail
|
||||||
import com.example.mobileapp.database.entities.User
|
import com.example.mobileapp.entities.MailSingleton
|
||||||
|
import com.example.mobileapp.entities.Story
|
||||||
|
import com.example.mobileapp.entities.StorySingleton
|
||||||
|
import com.example.mobileapp.screens.Authorization
|
||||||
|
import com.example.mobileapp.screens.EditMailScreen
|
||||||
|
import com.example.mobileapp.screens.EditStoryScreen
|
||||||
|
import com.example.mobileapp.screens.ListDataScreen
|
||||||
|
import com.example.mobileapp.screens.ListMailScreen
|
||||||
|
import com.example.mobileapp.screens.MainScreen
|
||||||
|
import com.example.mobileapp.screens.Registration
|
||||||
|
import com.example.mobileapp.screens.SettingsScreen
|
||||||
import com.example.mobileapp.ui.theme.MobileAppTheme
|
import com.example.mobileapp.ui.theme.MobileAppTheme
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
@ -24,31 +38,57 @@ class MainActivity : ComponentActivity() {
|
|||||||
color = MaterialTheme.colorScheme.background
|
color = MaterialTheme.colorScheme.background
|
||||||
) {
|
) {
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
NavBar(navController = navController)
|
val mailSingleton = MailSingleton()
|
||||||
|
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
|
||||||
|
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
|
||||||
|
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
|
||||||
|
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
|
||||||
|
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
|
||||||
|
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
|
||||||
|
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
|
||||||
|
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
|
||||||
|
|
||||||
|
val storySingleton = StorySingleton()
|
||||||
|
storySingleton.addStory(Story(0, "Чужак", "Знаменитая книга стивена кинга", R.drawable.king))
|
||||||
|
storySingleton.addStory(Story(1, "Переулок", "История ужасов от Дзюнзи Ито", R.drawable.dzun))
|
||||||
|
storySingleton.addStory(Story(2, "Чужак", "Знаменитая книга стивена кинга", R.drawable.king))
|
||||||
|
storySingleton.addStory(Story(3, "Переулок", "История ужасов от Дзюнзи Ито", R.drawable.dzun))
|
||||||
|
|
||||||
|
AppNavigation(navController = navController)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GlobalUser private constructor() {
|
@Composable
|
||||||
private var user = mutableStateOf<User?>(null)
|
fun AppNavigation(navController: NavHostController){
|
||||||
|
NavHost(
|
||||||
fun setUser(user: User?) {
|
navController = navController, startDestination = "authorization"
|
||||||
this.user.value = user
|
) {
|
||||||
}
|
composable("authorization"){
|
||||||
|
Authorization(navController = navController)
|
||||||
fun getUser(): User? {
|
}
|
||||||
return user.value
|
composable("registration"){
|
||||||
}
|
Registration(navController = navController)
|
||||||
|
}
|
||||||
companion object {
|
composable("main"){
|
||||||
private var instance: GlobalUser? = null
|
MainScreen(navController = navController)
|
||||||
|
}
|
||||||
fun getInstance(): GlobalUser {
|
composable("listdata"){
|
||||||
return instance ?: synchronized(this) {
|
ListDataScreen(navController = navController)
|
||||||
instance ?: GlobalUser().also { instance = it }
|
}
|
||||||
}
|
composable("mail"){
|
||||||
|
ListMailScreen(navController = navController)
|
||||||
|
}
|
||||||
|
composable("settings"){
|
||||||
|
SettingsScreen(navController = navController)
|
||||||
|
}
|
||||||
|
composable("editstory"){
|
||||||
|
EditStoryScreen(navController = navController)
|
||||||
|
}
|
||||||
|
composable("editmail"){
|
||||||
|
EditMailScreen(navController = navController)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,63 +0,0 @@
|
|||||||
package com.example.mobileapp
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import com.example.mobileapp.api.ServerService
|
|
||||||
import com.example.mobileapp.api.repository.RestMailRepository
|
|
||||||
import com.example.mobileapp.api.repository.RestReportRepository
|
|
||||||
import com.example.mobileapp.api.repository.RestStoryRepository
|
|
||||||
import com.example.mobileapp.api.repository.RestUserRepository
|
|
||||||
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.RemoteKeysRepositoryImpl
|
|
||||||
import com.example.mobileapp.database.repositories.ReportRepository
|
|
||||||
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
|
|
||||||
val reportRepository: ReportRepository
|
|
||||||
|
|
||||||
companion object{
|
|
||||||
const val TIMEOUT = 5000L
|
|
||||||
const val LIMIT = 10
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MobileAppDataContainer(private val context: Context): MobileAppContainer {
|
|
||||||
override val mailRepository: MailRepository by lazy {
|
|
||||||
RestMailRepository(ServerService.getInstance())
|
|
||||||
}
|
|
||||||
|
|
||||||
override val storyRepository: StoryRepository by lazy {
|
|
||||||
RestStoryRepository(ServerService.getInstance(),
|
|
||||||
storyReposLocal,
|
|
||||||
userReposLocal,
|
|
||||||
MobileAppDataBase.getInstance(context),
|
|
||||||
remoteKeyRepository)
|
|
||||||
}
|
|
||||||
|
|
||||||
override val userRepository: UserRepository by lazy {
|
|
||||||
RestUserRepository(ServerService.getInstance())
|
|
||||||
}
|
|
||||||
|
|
||||||
override val reportRepository: ReportRepository by lazy {
|
|
||||||
RestReportRepository(ServerService.getInstance())
|
|
||||||
}
|
|
||||||
|
|
||||||
private val remoteKeyRepository: RemoteKeysRepositoryImpl by lazy{
|
|
||||||
RemoteKeysRepositoryImpl(MobileAppDataBase.getInstance(context).remoteKeysDao())
|
|
||||||
}
|
|
||||||
|
|
||||||
private val storyReposLocal: OfflineStoryRepository by lazy {
|
|
||||||
OfflineStoryRepository(MobileAppDataBase.getInstance(context).storyDao())
|
|
||||||
}
|
|
||||||
|
|
||||||
private val userReposLocal: OfflineUserRepository by lazy {
|
|
||||||
OfflineUserRepository(MobileAppDataBase.getInstance(context).userDao())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package com.example.mobileapp.api
|
|
||||||
|
|
||||||
enum class ApiStatus {
|
|
||||||
LOADING, ERROR, DONE, NONE
|
|
||||||
}
|
|
@ -1,137 +0,0 @@
|
|||||||
package com.example.mobileapp.api
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.model.MailRemote
|
|
||||||
import com.example.mobileapp.api.model.ReportRemote
|
|
||||||
import com.example.mobileapp.api.model.StoryRemote
|
|
||||||
import com.example.mobileapp.api.model.UserRemote
|
|
||||||
import com.example.mobileapp.api.model.UserRemoteSignIn
|
|
||||||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
|
||||||
import okhttp3.OkHttpClient
|
|
||||||
import retrofit2.Retrofit
|
|
||||||
import retrofit2.http.Body
|
|
||||||
import retrofit2.http.DELETE
|
|
||||||
import retrofit2.http.GET
|
|
||||||
import retrofit2.http.POST
|
|
||||||
import retrofit2.http.PUT
|
|
||||||
import retrofit2.http.Path
|
|
||||||
import retrofit2.http.Query
|
|
||||||
|
|
||||||
interface ServerService {
|
|
||||||
//USER
|
|
||||||
@GET("user/get/{id}")
|
|
||||||
suspend fun getUser(
|
|
||||||
@Path("id") id: Int,
|
|
||||||
): UserRemote
|
|
||||||
|
|
||||||
@POST("user/signup")
|
|
||||||
suspend fun SignUp(
|
|
||||||
@Body user: UserRemote,
|
|
||||||
): UserRemote
|
|
||||||
|
|
||||||
@POST("user/signin")
|
|
||||||
suspend fun SignIn(
|
|
||||||
@Body user: UserRemoteSignIn
|
|
||||||
): UserRemote
|
|
||||||
|
|
||||||
@POST("user/update")
|
|
||||||
suspend fun updateUser(
|
|
||||||
@Body user: UserRemote
|
|
||||||
): UserRemote
|
|
||||||
|
|
||||||
@GET("user/getAll")
|
|
||||||
suspend fun getUsers(): List<UserRemote>
|
|
||||||
|
|
||||||
//STORY
|
|
||||||
@GET("story/get/{id}")
|
|
||||||
suspend fun getStory(
|
|
||||||
@Path("id") id: Int,
|
|
||||||
): StoryRemote
|
|
||||||
|
|
||||||
@GET("story/getAll")
|
|
||||||
suspend fun getStories(
|
|
||||||
@Query("page") page: Int,
|
|
||||||
@Query("size") size: Int,
|
|
||||||
): List<StoryRemote>
|
|
||||||
|
|
||||||
@POST("story/create")
|
|
||||||
suspend fun createStory(
|
|
||||||
@Body service: StoryRemote,
|
|
||||||
): StoryRemote
|
|
||||||
|
|
||||||
@PUT("story/update/{id}")
|
|
||||||
suspend fun updateStory(
|
|
||||||
@Path("id") id: Int,
|
|
||||||
@Body service: StoryRemote
|
|
||||||
): StoryRemote
|
|
||||||
|
|
||||||
@DELETE("story/delete/{id}")
|
|
||||||
suspend fun deleteStory(
|
|
||||||
@Path("id") id: Int
|
|
||||||
)
|
|
||||||
|
|
||||||
@GET("story/getByUser")
|
|
||||||
suspend fun getUserStories(
|
|
||||||
@Query("page") page: Int,
|
|
||||||
@Query("size") size: Int,
|
|
||||||
@Query("userId") userId: Int,
|
|
||||||
): List<StoryRemote>
|
|
||||||
|
|
||||||
//MAIL
|
|
||||||
@GET("mail/get/{id}")
|
|
||||||
suspend fun getMail(
|
|
||||||
@Path("id") id: Int,
|
|
||||||
): MailRemote
|
|
||||||
|
|
||||||
@GET("mail/getAll")
|
|
||||||
suspend fun getMails(
|
|
||||||
@Query("page") page: Int,
|
|
||||||
@Query("size") size: Int,
|
|
||||||
): List<MailRemote>
|
|
||||||
|
|
||||||
@POST("mail/create")
|
|
||||||
suspend fun createMail(
|
|
||||||
@Body service: MailRemote,
|
|
||||||
): MailRemote
|
|
||||||
|
|
||||||
@PUT("mail/update/{id}")
|
|
||||||
suspend fun updateMail(
|
|
||||||
@Path("id") id: Int,
|
|
||||||
@Body service: MailRemote
|
|
||||||
): MailRemote
|
|
||||||
|
|
||||||
@DELETE("mail/delete/{id}")
|
|
||||||
suspend fun deleteMail(
|
|
||||||
@Path("id") id: Int
|
|
||||||
)
|
|
||||||
|
|
||||||
//REPORT
|
|
||||||
@GET("report/createReport/{dateFrom}/{dateTo}")
|
|
||||||
suspend fun createReport(
|
|
||||||
@Path("dateFrom") dateFrom: Long,
|
|
||||||
@Path("dateTo") dateTo: Long
|
|
||||||
): ReportRemote
|
|
||||||
|
|
||||||
//INSTANCE
|
|
||||||
companion object {
|
|
||||||
private const val BASE_URL = "https://7hz21fz1-8080.euw.devtunnels.ms/api/"
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
private var INSTANCE: ServerService? = null
|
|
||||||
|
|
||||||
fun getInstance(): ServerService {
|
|
||||||
return INSTANCE ?: synchronized(this) {
|
|
||||||
val client = OkHttpClient.Builder()
|
|
||||||
.build()
|
|
||||||
return Retrofit.Builder()
|
|
||||||
.baseUrl(BASE_URL)
|
|
||||||
.client(client)
|
|
||||||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
|
|
||||||
.build()
|
|
||||||
.create(ServerService::class.java)
|
|
||||||
.also { INSTANCE = it }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,109 +0,0 @@
|
|||||||
package com.example.mobileapp.api
|
|
||||||
|
|
||||||
import androidx.paging.ExperimentalPagingApi
|
|
||||||
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
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeyType
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeys
|
|
||||||
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 retrofit2.HttpException
|
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
@OptIn(ExperimentalPagingApi::class)
|
|
||||||
class StoryRemoteMediator(private val service: ServerService,
|
|
||||||
private val storyRepository: OfflineStoryRepository,
|
|
||||||
private val userRepository: OfflineUserRepository,
|
|
||||||
private val database: MobileAppDataBase,
|
|
||||||
private val dbRemoteKeyRepository: RemoteKeysRepositoryImpl
|
|
||||||
) : RemoteMediator<Int, Story>() {
|
|
||||||
override suspend fun initialize(): InitializeAction {
|
|
||||||
return InitializeAction.LAUNCH_INITIAL_REFRESH
|
|
||||||
}
|
|
||||||
override suspend fun load(
|
|
||||||
loadType: LoadType,
|
|
||||||
state: PagingState<Int, Story>
|
|
||||||
): MediatorResult {
|
|
||||||
val page = when (loadType) {
|
|
||||||
LoadType.REFRESH -> {
|
|
||||||
val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
|
|
||||||
remoteKeys?.nextKey?.minus(1) ?: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
LoadType.PREPEND -> {
|
|
||||||
val remoteKeys = getRemoteKeyForFirstItem(state)
|
|
||||||
remoteKeys?.prevKey
|
|
||||||
?: return MediatorResult.Success(endOfPaginationReached = remoteKeys != null)
|
|
||||||
}
|
|
||||||
|
|
||||||
LoadType.APPEND -> {
|
|
||||||
val remoteKeys = getRemoteKeyForLastItem(state)
|
|
||||||
remoteKeys?.nextKey
|
|
||||||
?: return MediatorResult.Success(endOfPaginationReached = remoteKeys != null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
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) {
|
|
||||||
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.STORY)
|
|
||||||
storyRepository.clearStories()
|
|
||||||
userRepository.clearUsers()
|
|
||||||
}
|
|
||||||
val prevKey = if (page == 1) null else page - 1
|
|
||||||
val nextKey = if (endOfPaginationReached) null else page + 1
|
|
||||||
val keys = stories.map {
|
|
||||||
RemoteKeys(
|
|
||||||
entityId = it.id!!,
|
|
||||||
type = RemoteKeyType.STORY,
|
|
||||||
prevKey = prevKey,
|
|
||||||
nextKey = nextKey
|
|
||||||
)
|
|
||||||
}
|
|
||||||
dbRemoteKeyRepository.createRemoteKeys(keys)
|
|
||||||
userRepository.insertUser(user)
|
|
||||||
storyRepository.insertStories(stories)
|
|
||||||
}
|
|
||||||
return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
|
|
||||||
} catch (exception: IOException) {
|
|
||||||
return MediatorResult.Error(exception)
|
|
||||||
} catch (exception: HttpException) {
|
|
||||||
return MediatorResult.Error(exception)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, Story>): RemoteKeys? {
|
|
||||||
return state.pages.lastOrNull { it.data.isNotEmpty() }?.data?.lastOrNull()
|
|
||||||
?.let { story ->
|
|
||||||
story.id?.let { dbRemoteKeyRepository.getAllRemoteKeys(it, RemoteKeyType.STORY) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getRemoteKeyForFirstItem(state: PagingState<Int, Story>): RemoteKeys? {
|
|
||||||
return state.pages.firstOrNull { it.data.isNotEmpty() }?.data?.firstOrNull()
|
|
||||||
?.let { story ->
|
|
||||||
story.id?.let { dbRemoteKeyRepository.getAllRemoteKeys(it, RemoteKeyType.STORY) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getRemoteKeyClosestToCurrentPosition(
|
|
||||||
state: PagingState<Int, Story>
|
|
||||||
): RemoteKeys? {
|
|
||||||
return state.anchorPosition?.let { position ->
|
|
||||||
state.closestItemToPosition(position)?.id?.let { storyUid ->
|
|
||||||
dbRemoteKeyRepository.getAllRemoteKeys(storyUid, RemoteKeyType.STORY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import com.example.mobileapp.database.entities.Mail
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class MailRemote(
|
|
||||||
val id: Int? = null,
|
|
||||||
val message: String,
|
|
||||||
val postdate: Long? = Date().time,
|
|
||||||
val userId: Int
|
|
||||||
)
|
|
||||||
|
|
||||||
fun MailRemote.toMail(): Mail = Mail(
|
|
||||||
id,
|
|
||||||
message,
|
|
||||||
postdate,
|
|
||||||
userId
|
|
||||||
)
|
|
||||||
|
|
||||||
fun Mail.toMailRemote():MailRemote = MailRemote(
|
|
||||||
id,
|
|
||||||
message,
|
|
||||||
postdate,
|
|
||||||
userId
|
|
||||||
)
|
|
@ -1,62 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.util.Base64
|
|
||||||
import java.io.ByteArrayOutputStream
|
|
||||||
|
|
||||||
class RemoteConverters {
|
|
||||||
companion object {
|
|
||||||
private const val CHARSET_UTF8 = "UTF-8"
|
|
||||||
private const val MAX_IMAGE_SIZE = 1024 // Размер в килобайтах, который вы хотите использовать
|
|
||||||
|
|
||||||
fun fromBitmap(bitmap: Bitmap): String {
|
|
||||||
val outputStream = ByteArrayOutputStream()
|
|
||||||
|
|
||||||
val extendedBitmap = scaleRatioBitmap(bitmap)
|
|
||||||
|
|
||||||
// Сжимаем изображение до указанного максимального размера
|
|
||||||
val quality = calculateQuality(extendedBitmap)
|
|
||||||
extendedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
|
|
||||||
|
|
||||||
val byteArray = outputStream.toByteArray()
|
|
||||||
return Base64.encodeToString(byteArray, Base64.NO_WRAP)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toBitmap(base64String: String): Bitmap {
|
|
||||||
val byteArray = Base64.decode(base64String, Base64.NO_WRAP)
|
|
||||||
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateQuality(bitmap: Bitmap): Int {
|
|
||||||
val outputStream = ByteArrayOutputStream()
|
|
||||||
|
|
||||||
// Уменьшаем качество изображения, пока его размер не станет меньше максимального
|
|
||||||
var quality = 100
|
|
||||||
while (outputStream.size() / 1024 > MAX_IMAGE_SIZE && quality > 0) {
|
|
||||||
outputStream.reset()
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
|
|
||||||
quality -= 10
|
|
||||||
}
|
|
||||||
|
|
||||||
// Возвращаем качество, при котором размер изображения удовлетворяет ограничению
|
|
||||||
return if (quality < 0) 0 else quality
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun scaleRatioBitmap(bitmap: Bitmap): Bitmap {
|
|
||||||
val maxWidth = 990
|
|
||||||
val maxHeight = 990
|
|
||||||
if (bitmap.width > maxWidth || bitmap.height > maxHeight) {
|
|
||||||
// Если размер превышает максимальный, масштабируем изображение
|
|
||||||
val ratio = Math.min(maxWidth.toFloat() / bitmap.width,
|
|
||||||
maxHeight.toFloat() / bitmap.height)
|
|
||||||
|
|
||||||
val width = (ratio * bitmap.width).toInt()
|
|
||||||
val height = (ratio * bitmap.height).toInt()
|
|
||||||
|
|
||||||
return Bitmap.createScaledBitmap(bitmap, width, height, true)
|
|
||||||
}
|
|
||||||
return bitmap
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class ReportRemote(
|
|
||||||
val dateFrom: Long,
|
|
||||||
val dateTo: Long,
|
|
||||||
val postCount: Int,
|
|
||||||
val mostPostAuthor: UserRemote,
|
|
||||||
val mostPostCount: Int,
|
|
||||||
val listPostAuthor: List<StoryRemote>
|
|
||||||
)
|
|
@ -1,35 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import com.example.mobileapp.database.entities.Story
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class StoryRemote(
|
|
||||||
val id: Int? = null,
|
|
||||||
val title: String,
|
|
||||||
val description: String,
|
|
||||||
val cover: String,
|
|
||||||
val postdate: Long? = Date().time,
|
|
||||||
val userId: Int
|
|
||||||
)
|
|
||||||
|
|
||||||
fun StoryRemote.toStory(): Story = Story(
|
|
||||||
id,
|
|
||||||
title,
|
|
||||||
description,
|
|
||||||
RemoteConverters.toBitmap(cover),
|
|
||||||
postdate,
|
|
||||||
userId
|
|
||||||
)
|
|
||||||
|
|
||||||
fun Story.toStoryRemote(): StoryRemote = StoryRemote(
|
|
||||||
id,
|
|
||||||
title,
|
|
||||||
description,
|
|
||||||
RemoteConverters.fromBitmap(cover),
|
|
||||||
postdate,
|
|
||||||
userId
|
|
||||||
)
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class UserRemote(
|
|
||||||
val id: Int? = 0,
|
|
||||||
val login: String,
|
|
||||||
val password: String = "",
|
|
||||||
val email: String = "",
|
|
||||||
val photo: String? = null
|
|
||||||
)
|
|
||||||
|
|
||||||
fun UserRemote.toUser(): User = User(
|
|
||||||
id,
|
|
||||||
login,
|
|
||||||
password,
|
|
||||||
email,
|
|
||||||
photo?.let { RemoteConverters.toBitmap(it) },
|
|
||||||
)
|
|
||||||
|
|
||||||
fun User.toUserRemote():UserRemote = UserRemote(
|
|
||||||
id,
|
|
||||||
login,
|
|
||||||
password,
|
|
||||||
email,
|
|
||||||
photo?.let { RemoteConverters.fromBitmap(it) },
|
|
||||||
)
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.example.mobileapp.api.model
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class UserRemoteSignIn(
|
|
||||||
val login: String = "",
|
|
||||||
val password: String = "",
|
|
||||||
)
|
|
@ -1,64 +0,0 @@
|
|||||||
package com.example.mobileapp.api.repository
|
|
||||||
|
|
||||||
import androidx.paging.Pager
|
|
||||||
import androidx.paging.PagingConfig
|
|
||||||
import androidx.paging.PagingData
|
|
||||||
import androidx.paging.PagingSource
|
|
||||||
import androidx.paging.PagingState
|
|
||||||
import com.example.mobileapp.MobileAppContainer
|
|
||||||
import com.example.mobileapp.api.ServerService
|
|
||||||
import com.example.mobileapp.api.model.toMail
|
|
||||||
import com.example.mobileapp.api.model.toMailRemote
|
|
||||||
import com.example.mobileapp.database.entities.Mail
|
|
||||||
import com.example.mobileapp.database.repositories.MailRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
class RestMailRepository(private var service: ServerService): MailRepository {
|
|
||||||
override fun getAllMails(): Flow<PagingData<Mail>> {
|
|
||||||
return Pager(
|
|
||||||
config = PagingConfig(
|
|
||||||
pageSize = MobileAppContainer.LIMIT,
|
|
||||||
enablePlaceholders = false
|
|
||||||
),
|
|
||||||
pagingSourceFactory = { MailPagingSource(service) }
|
|
||||||
).flow
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getMail(id: Int): Mail? = service.getMail(id).toMail()
|
|
||||||
|
|
||||||
override suspend fun insertMail(mail: Mail) {
|
|
||||||
service.createMail(mail.toMailRemote())
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun updateMail(mail: Mail) {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteMail(mail: Mail) {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MailPagingSource(private val service: ServerService) : PagingSource<Int, Mail>() {
|
|
||||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Mail> {
|
|
||||||
try {
|
|
||||||
val nextPageNumber = params.key ?: 1
|
|
||||||
val pageSize = params.loadSize
|
|
||||||
|
|
||||||
val response = service.getMails(nextPageNumber, pageSize)
|
|
||||||
val mails = response.map { it.toMail() } // Преобразование MailRemote в Mail
|
|
||||||
|
|
||||||
return LoadResult.Page(
|
|
||||||
data = mails,
|
|
||||||
prevKey = if (nextPageNumber == 1) null else nextPageNumber - 1,
|
|
||||||
nextKey = if (mails.isEmpty()) null else nextPageNumber + 1
|
|
||||||
)
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
return LoadResult.Error(exception)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getRefreshKey(state: PagingState<Int, Mail>): Int? {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.example.mobileapp.api.repository
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.ServerService
|
|
||||||
import com.example.mobileapp.api.model.ReportRemote
|
|
||||||
import com.example.mobileapp.database.repositories.ReportRepository
|
|
||||||
|
|
||||||
class RestReportRepository(private var service: ServerService): ReportRepository {
|
|
||||||
override suspend fun createReport(dateFrom: Long, dateTo: Long): ReportRemote = service.createReport(dateFrom, dateTo)
|
|
||||||
}
|
|
@ -1,134 +0,0 @@
|
|||||||
package com.example.mobileapp.api.repository
|
|
||||||
|
|
||||||
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 com.example.mobileapp.MobileAppContainer
|
|
||||||
import com.example.mobileapp.api.ServerService
|
|
||||||
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.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
|
|
||||||
|
|
||||||
class RestStoryRepository(private var service: ServerService,
|
|
||||||
private val dbStoryRepository: OfflineStoryRepository,
|
|
||||||
private val dbUserRepository: OfflineUserRepository,
|
|
||||||
private val database: MobileAppDataBase,
|
|
||||||
private val dbRemoteKeyRepository: RemoteKeysRepositoryImpl
|
|
||||||
): StoryRepository {
|
|
||||||
|
|
||||||
override fun getAllStories(): Flow<PagingData<Story>> {
|
|
||||||
/*val pagingSourceFactory = {
|
|
||||||
dbStoryRepository.getAllStoriesPagingSource()
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalPagingApi::class)
|
|
||||||
return Pager(
|
|
||||||
config = PagingConfig(
|
|
||||||
pageSize = MobileAppContainer.LIMIT,
|
|
||||||
enablePlaceholders = false
|
|
||||||
),
|
|
||||||
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,
|
|
||||||
database,
|
|
||||||
dbRemoteKeyRepository,
|
|
||||||
),
|
|
||||||
pagingSourceFactory = pagingSourceFactory
|
|
||||||
).flow
|
|
||||||
/*return Pager(
|
|
||||||
config = PagingConfig(
|
|
||||||
pageSize = MobileAppContainer.LIMIT,
|
|
||||||
enablePlaceholders = false
|
|
||||||
),
|
|
||||||
pagingSourceFactory = { StoryPagingSource(service, userId) }
|
|
||||||
).flow*/
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getStoryById(id: Int): Story? = service.getStory(id).toStory()
|
|
||||||
|
|
||||||
override suspend fun insertStory(story: Story) {
|
|
||||||
service.createStory(story.toStoryRemote())
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun updateStory(story: Story) {
|
|
||||||
story.id?.let {
|
|
||||||
service.updateStory(it, story.toStoryRemote())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteStory(story: Story) {
|
|
||||||
try {
|
|
||||||
story.id?.let { this.service.deleteStory(it) }
|
|
||||||
dbStoryRepository.deleteStory(story)
|
|
||||||
}catch (ex: Exception){}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class StoryPagingSource(private val service: ServerService,
|
|
||||||
private val userId: Int? = null) : PagingSource<Int, Story>() {
|
|
||||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Story> {
|
|
||||||
try {
|
|
||||||
val nextPageNumber = params.key ?: 1
|
|
||||||
val pageSize = params.loadSize
|
|
||||||
|
|
||||||
val response = if (userId != null) {
|
|
||||||
service.getUserStories(nextPageNumber, pageSize, userId)
|
|
||||||
} else {
|
|
||||||
service.getStories(nextPageNumber, pageSize)
|
|
||||||
}
|
|
||||||
val stories = response.map { it.toStory() }
|
|
||||||
|
|
||||||
return LoadResult.Page(
|
|
||||||
data = stories,
|
|
||||||
prevKey = if (nextPageNumber == 1) null else nextPageNumber - 1,
|
|
||||||
nextKey = if (stories.isEmpty()) null else nextPageNumber + 1
|
|
||||||
)
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
return LoadResult.Error(exception)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getRefreshKey(state: PagingState<Int, Story>): Int? {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package com.example.mobileapp.api.repository
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.ServerService
|
|
||||||
import com.example.mobileapp.api.model.UserRemoteSignIn
|
|
||||||
import com.example.mobileapp.api.model.toUser
|
|
||||||
import com.example.mobileapp.api.model.toUserRemote
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import com.example.mobileapp.database.repositories.UserRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
class RestUserRepository(private var service: ServerService): UserRepository {
|
|
||||||
override fun getAllUsers(): Flow<List<User>> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getUser(id: Int): User = service.getUser(id).toUser()
|
|
||||||
|
|
||||||
|
|
||||||
override suspend fun getUserByLogin(user: UserRemoteSignIn): User {
|
|
||||||
return service.SignIn(user).toUser()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun insertUser(user: User) {
|
|
||||||
service.SignUp(user.toUserRemote())
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun updateUser(user: User) {
|
|
||||||
service.updateUser(user.toUserRemote())
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteUser(user: User) {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,5 @@
|
|||||||
package com.example.mobileapp.components
|
package com.example.mobileapp.components
|
||||||
|
|
||||||
import android.app.DatePickerDialog
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
@ -22,37 +19,26 @@ import androidx.compose.material3.Text
|
|||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
import androidx.navigation.NavHostController
|
||||||
import com.example.mobileapp.ui.theme.MobileAppTheme
|
import com.example.mobileapp.ui.theme.MobileAppTheme
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Calendar
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
val buttonHeightStandard = 72.dp
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun PlaceholderInputField(label: String, startValue: String? = null, isSingleLine: Boolean, onTextChanged: (String) -> Unit){
|
fun PlaceholderInputField(label: String){
|
||||||
var text = remember { mutableStateOf("") }
|
var text = remember { mutableStateOf("") }
|
||||||
startValue?.let{
|
|
||||||
text.value = startValue
|
|
||||||
}
|
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = text.value,
|
value = text.value,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
text.value = it
|
text.value = it
|
||||||
onTextChanged(it)
|
|
||||||
},
|
},
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(label)
|
Text(label)
|
||||||
@ -60,24 +46,18 @@ fun PlaceholderInputField(label: String, startValue: String? = null, isSingleLin
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||||
shape = RoundedCornerShape(10.dp),
|
shape = RoundedCornerShape(10.dp))
|
||||||
singleLine = isSingleLine
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun PasswordInputField(label: String, startValue: String? = null, onPasswordChanged: (String) -> Unit){
|
fun PasswordInputField(label: String){
|
||||||
var text = remember { mutableStateOf("") }
|
var text = remember { mutableStateOf("") }
|
||||||
startValue?.let{
|
|
||||||
text.value = startValue
|
|
||||||
}
|
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = text.value,
|
value = text.value,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
text.value = it
|
text.value = it
|
||||||
onPasswordChanged(it)
|
|
||||||
},
|
},
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(label)
|
Text(label)
|
||||||
@ -91,14 +71,13 @@ fun PasswordInputField(label: String, startValue: String? = null, onPasswordChan
|
|||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun SearchInputField(onTextChanged: (String) -> Unit){
|
fun SearchInputField(){
|
||||||
var text = remember { mutableStateOf("") }
|
var text = remember { mutableStateOf("") }
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = text.value,
|
value = text.value,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
text.value = it
|
text.value = it
|
||||||
onTextChanged(it)
|
|
||||||
},
|
},
|
||||||
leadingIcon = {
|
leadingIcon = {
|
||||||
Icon(
|
Icon(
|
||||||
@ -113,8 +92,7 @@ fun SearchInputField(onTextChanged: (String) -> Unit){
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(top = 16.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
.padding(top = 16.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||||
shape = RoundedCornerShape(10.dp),
|
shape = RoundedCornerShape(10.dp)
|
||||||
singleLine = true
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +102,7 @@ fun IconButton(iconLeft: ImageVector, label: String, backgroundColor: Color, tex
|
|||||||
onClick = onClickAction,
|
onClick = onClickAction,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.requiredHeight(buttonHeightStandard)
|
.requiredHeight(64.dp)
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = backgroundColor
|
containerColor = backgroundColor
|
||||||
@ -156,7 +134,7 @@ fun ActiveButton(label: String, backgroundColor: Color, textColor: Color, onClic
|
|||||||
onClick = onClickAction,
|
onClick = onClickAction,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.requiredHeight(buttonHeightStandard)
|
.requiredHeight(64.dp)
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = backgroundColor
|
containerColor = backgroundColor
|
||||||
@ -172,58 +150,12 @@ fun ActiveButton(label: String, backgroundColor: Color, textColor: Color, onClic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun DatePicker(startValue: Long? = null, onDateSelected: (Long) -> Unit) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
val calendar = Calendar.getInstance()
|
|
||||||
val year = calendar.get(Calendar.YEAR)
|
|
||||||
val month = calendar.get(Calendar.MONTH)
|
|
||||||
val day = calendar.get(Calendar.DAY_OF_MONTH)
|
|
||||||
|
|
||||||
val dateFormatter = remember { SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()) }
|
|
||||||
val selectedDate = remember { mutableStateOf<Long>(0) }
|
|
||||||
|
|
||||||
startValue?.let {
|
|
||||||
selectedDate.value = startValue
|
|
||||||
}
|
|
||||||
|
|
||||||
val datePickerDialog = remember { mutableStateOf(DatePickerDialog(context)) }
|
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
|
||||||
) {
|
|
||||||
Text(text = "Selected Date: ${dateFormatter.format(selectedDate.value)}")
|
|
||||||
ActiveButton(label = "Выбрать дату", backgroundColor = ButtonColor1,
|
|
||||||
textColor = Color.Black, onClickAction = {
|
|
||||||
datePickerDialog.value = DatePickerDialog(
|
|
||||||
context,
|
|
||||||
{ _, year: Int, month: Int, dayOfMonth: Int ->
|
|
||||||
val selectedDateInMillis = Calendar.getInstance().apply {
|
|
||||||
set(year, month, dayOfMonth)
|
|
||||||
}.timeInMillis
|
|
||||||
|
|
||||||
selectedDate.value = selectedDateInMillis
|
|
||||||
onDateSelected(selectedDateInMillis)
|
|
||||||
},
|
|
||||||
year,
|
|
||||||
month,
|
|
||||||
day
|
|
||||||
)
|
|
||||||
datePickerDialog.value.show()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Preview(showBackground = true)
|
@Preview(showBackground = true)
|
||||||
@Composable
|
@Composable
|
||||||
fun PlaceholderTextFieldPreview() {
|
fun PlaceholderTextFieldPreview() {
|
||||||
MobileAppTheme {
|
MobileAppTheme {
|
||||||
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
||||||
PlaceholderInputField("Email", null,true, { })
|
PlaceholderInputField("Email")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,5 @@
|
|||||||
package com.example.mobileapp.components
|
package com.example.mobileapp.components
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@ -16,94 +14,60 @@ import androidx.compose.foundation.layout.requiredHeight
|
|||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.layout.width
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
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.lazy.items
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.AlertDialog
|
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
import androidx.compose.material3.Card
|
import androidx.compose.material3.Card
|
||||||
import androidx.compose.material3.CardDefaults
|
import androidx.compose.material3.CardDefaults
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.paging.compose.LazyPagingItems
|
|
||||||
import androidx.paging.compose.itemKey
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.database.MobileAppDataBase
|
import com.example.mobileapp.entities.Mail
|
||||||
import com.example.mobileapp.database.entities.Mail
|
import com.example.mobileapp.entities.MailSingleton
|
||||||
import com.example.mobileapp.database.entities.Story
|
import com.example.mobileapp.entities.Story
|
||||||
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
|
import com.example.mobileapp.entities.StorySingleton
|
||||||
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.BackgroundItem2
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor1
|
import com.example.mobileapp.ui.theme.ButtonColor1
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
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
|
|
||||||
|
|
||||||
val dateFormat = SimpleDateFormat("dd.MM.yyyy")
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>){
|
fun DataListScroll(navController: NavHostController){
|
||||||
|
val storySingleton = StorySingleton()
|
||||||
|
val isFirstTime = remember { mutableStateOf(true) }
|
||||||
|
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
){
|
){
|
||||||
item {
|
item {
|
||||||
when {
|
addNewListItem(navController, "editstory")
|
||||||
dataList.isListOf<Story>() -> addNewListItem(navController, "editstory")
|
|
||||||
dataList.isListOf<Mail>() -> addNewListItem(navController, "editmail")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
items(dataList){ item ->
|
items(storySingleton.getStoryList()){ item ->
|
||||||
when(item){
|
DataListItem(item = item, navController = navController)
|
||||||
is Story -> StoryListItem(item = item, navController = navController)
|
|
||||||
is Mail -> MailListItem(item = item, navController = navController)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T> List<*>.isListOf(): Boolean {
|
|
||||||
return isNotEmpty() && all { it is T }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun StoryListItem(item: Story, navController: NavHostController,
|
fun DataListItem(item: Story, navController: NavHostController){
|
||||||
isReadOnly: Boolean? = false,
|
|
||||||
storyViewModel: StoryViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val isExpanded = remember {
|
val isExpanded = remember {
|
||||||
mutableStateOf(false)
|
mutableStateOf(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
val showDialog = remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@ -125,7 +89,7 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
|||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.Top
|
verticalAlignment = Alignment.Top
|
||||||
){
|
){
|
||||||
Image(bitmap = item.cover.asImageBitmap(),
|
Image(painter = painterResource(id = item.cover),
|
||||||
contentDescription = item.description,
|
contentDescription = item.description,
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -135,7 +99,7 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
|||||||
modifier = Modifier.padding(8.dp)
|
modifier = Modifier.padding(8.dp)
|
||||||
){
|
){
|
||||||
Text(
|
Text(
|
||||||
text = "${item.title} | ${dateFormat.format(Date(item.postdate!!))}",
|
text = item.title,
|
||||||
fontSize = 20.sp,
|
fontSize = 20.sp,
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
@ -146,39 +110,16 @@ fun StoryListItem(item: Story, navController: NavHostController,
|
|||||||
visible = isExpanded.value,
|
visible = isExpanded.value,
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
) {
|
) {
|
||||||
if (isReadOnly!!){
|
Row(
|
||||||
DataListItemButton(label = "Подробнее", backgroundColor = ButtonColor2,
|
modifier = Modifier.fillMaxWidth(),
|
||||||
textColor = Color.White, onClickAction = {
|
horizontalArrangement = Arrangement.End
|
||||||
navController.navigate("viewstory/${item.id}")
|
){
|
||||||
})
|
DataListItemButton("Изменить", ButtonColor2, Color.White, onClickAction = { navController.navigate("editstory") })
|
||||||
}
|
DataListItemButton("Удалить", Color.Red, Color.White, onClickAction = { })
|
||||||
else{
|
|
||||||
Row(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
horizontalArrangement = Arrangement.End
|
|
||||||
){
|
|
||||||
DataListItemButton("Изменить", ButtonColor2, Color.White, onClickAction = {
|
|
||||||
navController.navigate("editstory/${item.id}")
|
|
||||||
})
|
|
||||||
DataListItemButton("Удалить", Color.Red, Color.White, onClickAction = {
|
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(showDialog.value) {
|
|
||||||
DialogWindow(label = "Подтверждение",
|
|
||||||
message = "Вы уверены что хотите удалить запись?", onConfirmAction = {
|
|
||||||
storyViewModel.deleteStory(item)
|
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
navController.navigate("story")
|
|
||||||
}, onDismissAction = {
|
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ -200,28 +141,29 @@ fun DataListItemButton(label: String, backgroundColor: Color, textColor: Color,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MailListItem(item: Mail, navController: NavHostController,
|
fun MailListScroll(navController: NavHostController){
|
||||||
userViewModel: UserViewModel = viewModel(
|
val mailSingleton = MailSingleton()
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
LazyColumn(
|
||||||
val context = LocalContext.current
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
){
|
||||||
|
item {
|
||||||
|
addNewListItem(navController, "editmail")
|
||||||
|
}
|
||||||
|
items(mailSingleton.getMailList()){ item ->
|
||||||
|
MailListItem(item = item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun MailListItem(item: Mail){
|
||||||
val isExpanded = remember {
|
val isExpanded = remember {
|
||||||
mutableStateOf(false)
|
mutableStateOf(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
val userPhoto = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.post)) }
|
|
||||||
val userName = remember { mutableStateOf("UserName") }
|
|
||||||
|
|
||||||
LaunchedEffect(Unit){
|
|
||||||
val user = userViewModel.getUser(item.userId)
|
|
||||||
if (user != null) {
|
|
||||||
userName.value = user.email
|
|
||||||
if (user.photo != null){
|
|
||||||
userPhoto.value = user.photo
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@ -243,18 +185,17 @@ fun MailListItem(item: Mail, navController: NavHostController,
|
|||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.Top
|
verticalAlignment = Alignment.Top
|
||||||
){
|
){
|
||||||
Image(bitmap = userPhoto.value.asImageBitmap(),
|
Image(painter = painterResource(id = R.drawable.post),
|
||||||
contentDescription = "message",
|
contentDescription = "message",
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(96.dp)
|
.size(96.dp)
|
||||||
.padding(8.dp)
|
.padding(8.dp))
|
||||||
.clip(RoundedCornerShape(16.dp)))
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.padding(8.dp)
|
modifier = Modifier.padding(8.dp)
|
||||||
){
|
){
|
||||||
Text(
|
Text(
|
||||||
text = "${userName.value} | ${dateFormat.format(Date(item.postdate!!))}",
|
text = item.username,
|
||||||
fontSize = 20.sp,
|
fontSize = 20.sp,
|
||||||
fontWeight = FontWeight.Bold)
|
fontWeight = FontWeight.Bold)
|
||||||
Text(text = item.message)
|
Text(text = item.message)
|
||||||
@ -264,10 +205,22 @@ fun MailListItem(item: Mail, navController: NavHostController,
|
|||||||
visible = isExpanded.value,
|
visible = isExpanded.value,
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
) {
|
) {
|
||||||
DataListItemButton(label = "Подробнее", backgroundColor = ButtonColor2,
|
Button(
|
||||||
textColor = Color.White, onClickAction = {
|
onClick = { /* Действие при нажатии кнопки */ },
|
||||||
navController.navigate("viewmail/${item.id}")
|
modifier = Modifier
|
||||||
})
|
.requiredHeight(64.dp)
|
||||||
|
.fillMaxWidth(),
|
||||||
|
shape = RoundedCornerShape(10.dp),
|
||||||
|
colors = ButtonDefaults.buttonColors(
|
||||||
|
containerColor = ButtonColor2
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = "Подробнее",
|
||||||
|
color = Color.White,
|
||||||
|
fontSize = 18.sp,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -284,7 +237,7 @@ fun addNewListItem(navController: NavHostController, destination: String){
|
|||||||
},
|
},
|
||||||
shape = RoundedCornerShape(15.dp),
|
shape = RoundedCornerShape(15.dp),
|
||||||
colors = CardDefaults.cardColors(
|
colors = CardDefaults.cardColors(
|
||||||
containerColor = Color.White
|
containerColor = BackgroundItem2
|
||||||
),
|
),
|
||||||
elevation = CardDefaults.cardElevation(
|
elevation = CardDefaults.cardElevation(
|
||||||
defaultElevation = 8.dp
|
defaultElevation = 8.dp
|
||||||
@ -307,48 +260,8 @@ fun addNewListItem(navController: NavHostController, destination: String){
|
|||||||
text = "Добавить",
|
text = "Добавить",
|
||||||
fontSize = 28.sp,
|
fontSize = 28.sp,
|
||||||
fontWeight = FontWeight.Bold,
|
fontWeight = FontWeight.Bold,
|
||||||
modifier = Modifier.padding(start = 32.dp),
|
modifier = Modifier.padding(start = 32.dp))
|
||||||
color = Color.Black
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun DialogWindow(label: String, message: String, onConfirmAction: () -> Unit, onDismissAction: () -> Unit){
|
|
||||||
AlertDialog(onDismissRequest = onDismissAction,
|
|
||||||
title = {
|
|
||||||
Text(text = label,
|
|
||||||
fontSize = 20.sp,
|
|
||||||
fontWeight = FontWeight.Bold)
|
|
||||||
},
|
|
||||||
text = {
|
|
||||||
Text(text = message)
|
|
||||||
},
|
|
||||||
confirmButton = {
|
|
||||||
Button(
|
|
||||||
onClick = onConfirmAction,
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = ButtonColor2
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(5.dp)
|
|
||||||
) {
|
|
||||||
Text(text = "Подтвердить",
|
|
||||||
color = Color.White)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
dismissButton = {
|
|
||||||
Button(
|
|
||||||
onClick = onDismissAction,
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = ButtonColor1
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(5.dp)
|
|
||||||
) {
|
|
||||||
Text(text = "Отмена",
|
|
||||||
color = Color.Black)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package com.example.mobileapp.components
|
|
||||||
|
|
||||||
import androidx.annotation.DrawableRes
|
|
||||||
|
|
||||||
class NavBarItem(
|
|
||||||
val route: String,
|
|
||||||
val label: String,
|
|
||||||
@DrawableRes
|
|
||||||
val icon: Int
|
|
||||||
) {
|
|
||||||
}
|
|
@ -1,11 +1,9 @@
|
|||||||
package com.example.mobileapp.components
|
package com.example.mobileapp.components
|
||||||
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
|
||||||
import androidx.compose.animation.slideInVertically
|
|
||||||
import androidx.compose.animation.slideOutVertically
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.requiredHeight
|
import androidx.compose.foundation.layout.requiredHeight
|
||||||
@ -14,22 +12,16 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
|||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.NavigationBar
|
|
||||||
import androidx.compose.material3.NavigationBarItem
|
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TopAppBar
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.ColorFilter
|
||||||
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.res.painterResource
|
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.font.FontWeight
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
@ -38,184 +30,40 @@ import androidx.navigation.compose.NavHost
|
|||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.navArgument
|
import androidx.navigation.navArgument
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
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.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.ReportScreen
|
|
||||||
import com.example.mobileapp.screens.SettingsScreen
|
|
||||||
import com.example.mobileapp.screens.StoryViewScreen
|
|
||||||
|
|
||||||
val navBarItems = listOf(
|
|
||||||
NavBarItem(route = "main", label = "Главная", icon = R.drawable.home),
|
|
||||||
NavBarItem(route = "story", label = "Создание", icon = R.drawable.edit),
|
|
||||||
NavBarItem(route = "mail", label = "Почта", icon = R.drawable.mail),
|
|
||||||
NavBarItem(route = "report", label = "Отчёт", icon = R.drawable.report),
|
|
||||||
NavBarItem(route = "settings", label = "Настройки", icon = R.drawable.settings)
|
|
||||||
)
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NavBar(navController: NavHostController) {
|
fun NavBar(navController: NavHostController, itemColorFilter: Color) {
|
||||||
val topBarState = rememberSaveable { (mutableStateOf(false)) }
|
Row(
|
||||||
val bottomBarState = rememberSaveable { (mutableStateOf(false)) }
|
modifier = Modifier
|
||||||
|
.fillMaxWidth(),
|
||||||
Scaffold(
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
topBar = {
|
verticalAlignment = Alignment.CenterVertically
|
||||||
AnimatedVisibility(
|
) {
|
||||||
visible = topBarState.value,
|
NavItem(navController = navController, imageId = R.drawable.home,
|
||||||
enter = slideInVertically(initialOffsetY = { it }),
|
description = "homeButton", destination = "main", itemColorFilter = itemColorFilter)
|
||||||
exit = slideOutVertically(targetOffsetY = { it }),
|
NavItem(navController = navController, imageId = R.drawable.edit,
|
||||||
|
description = "editButton", destination = "listdata", itemColorFilter = itemColorFilter)
|
||||||
content = {
|
NavItem(navController = navController, imageId = R.drawable.mail,
|
||||||
TopAppBar(
|
description = "mailButton", destination = "mail", itemColorFilter = itemColorFilter)
|
||||||
modifier = Modifier.fillMaxWidth(),
|
NavItem(navController = navController, imageId = R.drawable.settings,
|
||||||
title = {
|
description = "settingsButton", destination = "settings", itemColorFilter = itemColorFilter)
|
||||||
Text(
|
|
||||||
text = "Storyteller!",
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
fontFamily = FontFamily(
|
|
||||||
Font(
|
|
||||||
R.font.roboto_regular, FontWeight.Bold
|
|
||||||
)
|
|
||||||
),
|
|
||||||
color = Color.Black
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
},
|
|
||||||
bottomBar = {
|
|
||||||
AnimatedVisibility(
|
|
||||||
visible = bottomBarState.value,
|
|
||||||
enter = slideInVertically(initialOffsetY = { it }),
|
|
||||||
exit = slideOutVertically(targetOffsetY = { it }),
|
|
||||||
content = {
|
|
||||||
NavigationBar {
|
|
||||||
navBarItems.forEach{item ->
|
|
||||||
NavigationBarItem(
|
|
||||||
icon = {
|
|
||||||
Image(
|
|
||||||
painter = painterResource(item.icon),
|
|
||||||
contentDescription = null,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(40.dp)
|
|
||||||
.padding(bottom = 6.dp)
|
|
||||||
)
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
text = item.label
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
navController.navigate(item.route)
|
|
||||||
},
|
|
||||||
selected = false,
|
|
||||||
modifier = Modifier.fillMaxSize()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
},
|
|
||||||
modifier = Modifier.background(Color.White)
|
|
||||||
) {innerPaddings ->
|
|
||||||
NavHost(
|
|
||||||
navController = navController,
|
|
||||||
startDestination = "authorization",
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
composable(
|
|
||||||
"editstory/{id}",
|
|
||||||
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"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
composable("report"){
|
|
||||||
topBarState.value = false
|
|
||||||
bottomBarState.value = true
|
|
||||||
ReportScreen(navController = navController)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun NavItem(navController: NavHostController, imageId: Int,
|
||||||
|
description: String, destination: String, itemColorFilter: Color){
|
||||||
|
Image(painter = painterResource(id = imageId),
|
||||||
|
contentDescription = description,
|
||||||
|
contentScale = ContentScale.Crop,
|
||||||
|
modifier = Modifier
|
||||||
|
.size(64.dp)
|
||||||
|
.clickable {
|
||||||
|
navController.navigate(destination)
|
||||||
|
},
|
||||||
|
colorFilter = ColorFilter.tint(itemColorFilter))
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NavigationButton(navController: NavHostController,
|
fun NavigationButton(navController: NavHostController,
|
||||||
destination: String, label: String,
|
destination: String, label: String,
|
||||||
@ -226,7 +74,7 @@ fun NavigationButton(navController: NavHostController,
|
|||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.requiredHeight(72.dp)
|
.requiredHeight(64.dp)
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = backgroundColor
|
containerColor = backgroundColor
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
package com.example.mobileapp.database
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import androidx.room.Database
|
|
||||||
import androidx.room.Room
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
import androidx.room.TypeConverters
|
|
||||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
|
||||||
import com.example.mobileapp.R
|
|
||||||
import com.example.mobileapp.database.dao.MailDao
|
|
||||||
import com.example.mobileapp.database.dao.RemoteKeysDao
|
|
||||||
import com.example.mobileapp.database.dao.StoryDao
|
|
||||||
import com.example.mobileapp.database.dao.UserDao
|
|
||||||
import com.example.mobileapp.database.entities.Converters
|
|
||||||
import com.example.mobileapp.database.entities.Mail
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeys
|
|
||||||
import com.example.mobileapp.database.entities.Story
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
@Database(entities = [User::class, Story::class, Mail::class, RemoteKeys::class], version = 10, exportSchema = false)
|
|
||||||
@TypeConverters(Converters::class)
|
|
||||||
abstract class MobileAppDataBase : RoomDatabase() {
|
|
||||||
abstract fun userDao(): UserDao
|
|
||||||
abstract fun storyDao(): StoryDao
|
|
||||||
abstract fun mailDao(): MailDao
|
|
||||||
abstract fun remoteKeysDao(): RemoteKeysDao
|
|
||||||
|
|
||||||
companion object{
|
|
||||||
private const val DB_NAME: String = "my-db"
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
private var INSTANCE: MobileAppDataBase? = null
|
|
||||||
|
|
||||||
suspend fun initialDataBase(appContext: Context){
|
|
||||||
/*INSTANCE?.let { database ->
|
|
||||||
val userDao = database.userDao()
|
|
||||||
userDao.insert(User(id = 1, login = "Дзюнзи Ито", password = "1234", email = "ito@gmail.com"))
|
|
||||||
userDao.insert(User(id = 2, login = "Стивен Кинг", password = "4321", email = "king@gmail.com"))
|
|
||||||
|
|
||||||
val storyDao = database.storyDao()
|
|
||||||
storyDao.insert(Story(title = "Переулок", description = "История ужасов от Дзюнзи Ито",
|
|
||||||
cover = BitmapFactory.decodeResource(appContext.resources, R.drawable.dzun), userId = 1))
|
|
||||||
storyDao.insert(Story(title = "Чужак", description = "Знаменитая книга стивена кинга",
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getInstance(appContext: Context): MobileAppDataBase {
|
|
||||||
return INSTANCE ?: synchronized(this) {
|
|
||||||
Room.databaseBuilder(
|
|
||||||
appContext,
|
|
||||||
MobileAppDataBase::class.java,
|
|
||||||
DB_NAME
|
|
||||||
)
|
|
||||||
.addCallback(object : Callback() {
|
|
||||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
|
||||||
super.onCreate(db)
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
|
||||||
initialDataBase(appContext)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.fallbackToDestructiveMigration()
|
|
||||||
.build()
|
|
||||||
.also { INSTANCE = it }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package com.example.mobileapp.database.dao
|
|
||||||
|
|
||||||
import androidx.paging.PagingSource
|
|
||||||
import androidx.room.Dao
|
|
||||||
import androidx.room.Delete
|
|
||||||
import androidx.room.Insert
|
|
||||||
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 where mails.id = :id")
|
|
||||||
suspend fun getById(id: Int): Mail?
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
|
||||||
suspend fun insert(mail: Mail)
|
|
||||||
|
|
||||||
@Update
|
|
||||||
suspend fun update(mail: Mail)
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
suspend fun delete(mail: Mail)
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
package com.example.mobileapp.database.dao
|
|
||||||
|
|
||||||
import androidx.room.Dao
|
|
||||||
import androidx.room.Insert
|
|
||||||
import androidx.room.OnConflictStrategy
|
|
||||||
import androidx.room.Query
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeyType
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeys
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
interface RemoteKeysDao {
|
|
||||||
@Query("SELECT * FROM remote_keys WHERE entityId = :entityId AND type = :type")
|
|
||||||
suspend fun getRemoteKeys(entityId: Int, type: RemoteKeyType): RemoteKeys?
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
||||||
suspend fun insertAll(remoteKey: List<RemoteKeys>)
|
|
||||||
@Query("DELETE FROM remote_keys WHERE type = :type")
|
|
||||||
suspend fun clearRemoteKeys(type: RemoteKeyType)
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package com.example.mobileapp.database.dao
|
|
||||||
|
|
||||||
import androidx.paging.PagingSource
|
|
||||||
import androidx.room.Dao
|
|
||||||
import androidx.room.Delete
|
|
||||||
import androidx.room.Insert
|
|
||||||
import androidx.room.OnConflictStrategy
|
|
||||||
import androidx.room.Query
|
|
||||||
import androidx.room.Update
|
|
||||||
import com.example.mobileapp.database.entities.Story
|
|
||||||
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 where stories.id = :id")
|
|
||||||
suspend 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>
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
|
||||||
suspend fun insert(vararg story: Story)
|
|
||||||
|
|
||||||
@Update
|
|
||||||
suspend fun update(story: Story)
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
suspend fun delete(story: Story)
|
|
||||||
|
|
||||||
@Query("delete from stories")
|
|
||||||
suspend fun deleteAll()
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package com.example.mobileapp.database.dao
|
|
||||||
|
|
||||||
import androidx.room.Dao
|
|
||||||
import androidx.room.Delete
|
|
||||||
import androidx.room.Insert
|
|
||||||
import androidx.room.OnConflictStrategy
|
|
||||||
import androidx.room.Query
|
|
||||||
import androidx.room.Update
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
interface UserDao {
|
|
||||||
@Query("select * from users")
|
|
||||||
fun getAll(): Flow<List<User>>
|
|
||||||
|
|
||||||
@Query("select * from users where users.id = :id")
|
|
||||||
suspend fun getById(id: Int): User?
|
|
||||||
|
|
||||||
@Query("select * from users where users.login = :login")
|
|
||||||
suspend fun getByLogin(login: String): User?
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
|
||||||
suspend fun insert(vararg user: User)
|
|
||||||
|
|
||||||
@Update
|
|
||||||
suspend fun update(user: User)
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
suspend fun delete(user: User)
|
|
||||||
|
|
||||||
@Query("delete from stories")
|
|
||||||
suspend fun deleteAll()
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.example.mobileapp.database.entities
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import androidx.room.TypeConverter
|
|
||||||
import java.io.ByteArrayOutputStream
|
|
||||||
|
|
||||||
class Converters {
|
|
||||||
@TypeConverter
|
|
||||||
fun fromBitmap(bitmap: Bitmap) : ByteArray {
|
|
||||||
val outputStream = ByteArrayOutputStream();
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
|
||||||
return outputStream.toByteArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
@TypeConverter
|
|
||||||
fun toBitmap(byteArray: ByteArray): Bitmap {
|
|
||||||
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package com.example.mobileapp.database.entities
|
|
||||||
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.ForeignKey
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Entity(
|
|
||||||
tableName = "mails",
|
|
||||||
foreignKeys = [
|
|
||||||
ForeignKey(
|
|
||||||
entity = User::class,
|
|
||||||
parentColumns = ["id"],
|
|
||||||
childColumns = ["user_id"],
|
|
||||||
onDelete = ForeignKey.RESTRICT,
|
|
||||||
onUpdate = ForeignKey.RESTRICT
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
data class Mail(
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
|
||||||
val id: Int? = null,
|
|
||||||
@ColumnInfo(name = "message")
|
|
||||||
val message: String,
|
|
||||||
@ColumnInfo(name = "postdate")
|
|
||||||
val postdate: Long? = Date().time,
|
|
||||||
@ColumnInfo(name="user_id")
|
|
||||||
val userId: Int
|
|
||||||
){
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return id ?: -1
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package com.example.mobileapp.database.entities
|
|
||||||
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
import androidx.room.TypeConverter
|
|
||||||
import androidx.room.TypeConverters
|
|
||||||
|
|
||||||
enum class RemoteKeyType(private val type: String) {
|
|
||||||
STORY(Story::class.simpleName ?: "Story");
|
|
||||||
@TypeConverter
|
|
||||||
fun toRemoteKeyType(value: String) = RemoteKeyType.values().first { it.type == value }
|
|
||||||
@TypeConverter
|
|
||||||
fun fromRemoteKeyType(value: RemoteKeyType) = value.type
|
|
||||||
}
|
|
||||||
|
|
||||||
@Entity(tableName = "remote_keys")
|
|
||||||
data class RemoteKeys(
|
|
||||||
@PrimaryKey val entityId: Int,
|
|
||||||
@TypeConverters(RemoteKeyType::class)
|
|
||||||
val type: RemoteKeyType,
|
|
||||||
val prevKey: Int?,
|
|
||||||
val nextKey: Int?
|
|
||||||
)
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.example.mobileapp.database.entities
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.ForeignKey
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Entity(
|
|
||||||
tableName = "stories",
|
|
||||||
foreignKeys = [
|
|
||||||
ForeignKey(
|
|
||||||
entity = User::class,
|
|
||||||
parentColumns = ["id"],
|
|
||||||
childColumns = ["user_id"],
|
|
||||||
onDelete = ForeignKey.RESTRICT,
|
|
||||||
onUpdate = ForeignKey.RESTRICT
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
data class Story(
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
|
||||||
val id: Int? = null,
|
|
||||||
@ColumnInfo(name = "title")
|
|
||||||
val title: String,
|
|
||||||
@ColumnInfo(name = "description")
|
|
||||||
val description: String,
|
|
||||||
@ColumnInfo(name = "cover")
|
|
||||||
val cover: Bitmap,
|
|
||||||
@ColumnInfo(name = "postdate")
|
|
||||||
val postdate: Long? = Date().time,
|
|
||||||
@ColumnInfo(name="user_id")
|
|
||||||
val userId: Int
|
|
||||||
){
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return id ?: -1
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package com.example.mobileapp.database.entities
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
|
|
||||||
@Entity(tableName = "users")
|
|
||||||
data class User(
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
|
||||||
val id: Int? = null,
|
|
||||||
@ColumnInfo(name = "login")
|
|
||||||
val login: String,
|
|
||||||
@ColumnInfo(name = "password")
|
|
||||||
val password: String,
|
|
||||||
@ColumnInfo(name = "email")
|
|
||||||
val email: String,
|
|
||||||
@ColumnInfo(name = "photo")
|
|
||||||
val photo: Bitmap? = null
|
|
||||||
){
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return id ?: -1
|
|
||||||
}
|
|
||||||
}
|
|
@ -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>>
|
|
||||||
|
|
||||||
suspend fun getMail(id: Int): 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 suspend fun getMail(id: Int): 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,57 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import androidx.paging.Pager
|
|
||||||
import androidx.paging.PagingConfig
|
|
||||||
import androidx.paging.PagingData
|
|
||||||
import androidx.paging.PagingSource
|
|
||||||
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 suspend fun getStoryById(id: Int): 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)
|
|
||||||
|
|
||||||
suspend fun clearStories() = storyDao.deleteAll()
|
|
||||||
suspend fun insertStories(stories: List<Story>) =
|
|
||||||
storyDao.insert(*stories.toTypedArray())
|
|
||||||
|
|
||||||
fun getAllStoriesPagingSource(): PagingSource<Int, Story> = storyDao.getAll()
|
|
||||||
|
|
||||||
fun getUserStoriesPagingSource(userId: Int): PagingSource<Int, Story> = storyDao.getByUserId(userId)
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.model.UserRemoteSignIn
|
|
||||||
import com.example.mobileapp.database.dao.UserDao
|
|
||||||
import com.example.mobileapp.database.entities.Story
|
|
||||||
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 suspend fun getUser(id: Int): User? = userDao.getById(id)
|
|
||||||
|
|
||||||
override suspend fun getUserByLogin(user: UserRemoteSignIn): User? = userDao.getByLogin(user.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)
|
|
||||||
|
|
||||||
suspend fun clearUsers() = userDao.deleteAll()
|
|
||||||
suspend fun insertUsers(users: List<User>) =
|
|
||||||
userDao.insert(*users.toTypedArray())
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeyType
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeys
|
|
||||||
|
|
||||||
interface RemoteKeyRepository {
|
|
||||||
suspend fun getAllRemoteKeys(id: Int, type: RemoteKeyType): RemoteKeys?
|
|
||||||
suspend fun createRemoteKeys(remoteKeys: List<RemoteKeys>)
|
|
||||||
suspend fun deleteRemoteKey(type: RemoteKeyType)
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import com.example.mobileapp.database.dao.RemoteKeysDao
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeyType
|
|
||||||
import com.example.mobileapp.database.entities.RemoteKeys
|
|
||||||
|
|
||||||
class RemoteKeysRepositoryImpl(private val remoteKeysDao: RemoteKeysDao): RemoteKeyRepository {
|
|
||||||
override suspend fun getAllRemoteKeys(id: Int, type: RemoteKeyType) =
|
|
||||||
remoteKeysDao.getRemoteKeys(id, type)
|
|
||||||
|
|
||||||
override suspend fun createRemoteKeys(remoteKeys: List<RemoteKeys>) =
|
|
||||||
remoteKeysDao.insertAll(remoteKeys)
|
|
||||||
|
|
||||||
override suspend fun deleteRemoteKey(type: RemoteKeyType) =
|
|
||||||
remoteKeysDao.clearRemoteKeys(type)
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.model.ReportRemote
|
|
||||||
|
|
||||||
interface ReportRepository {
|
|
||||||
suspend fun createReport(dateFrom: Long, dateTo: Long): ReportRemote
|
|
||||||
}
|
|
@ -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>>
|
|
||||||
|
|
||||||
suspend fun getStoryById(id: Int): Story?
|
|
||||||
|
|
||||||
suspend fun insertStory(story: Story)
|
|
||||||
|
|
||||||
suspend fun updateStory(story: Story)
|
|
||||||
|
|
||||||
suspend fun deleteStory(story: Story)
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.example.mobileapp.database.repositories
|
|
||||||
|
|
||||||
import com.example.mobileapp.api.model.UserRemoteSignIn
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
interface UserRepository {
|
|
||||||
fun getAllUsers(): Flow<List<User>>
|
|
||||||
|
|
||||||
suspend fun getUser(id: Int): User?
|
|
||||||
|
|
||||||
suspend fun getUserByLogin(user: UserRemoteSignIn): User?
|
|
||||||
|
|
||||||
suspend fun insertUser(user: User)
|
|
||||||
|
|
||||||
suspend fun updateUser(user: User)
|
|
||||||
|
|
||||||
suspend fun deleteUser(user: User)
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
package com.example.mobileapp.database.viewmodels
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.example.mobileapp.api.ApiStatus
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import retrofit2.HttpException
|
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
open class CustomViewModel : ViewModel() {
|
|
||||||
var apiStatus by mutableStateOf(ApiStatus.NONE)
|
|
||||||
private set
|
|
||||||
|
|
||||||
var apiError by mutableStateOf("")
|
|
||||||
private set
|
|
||||||
|
|
||||||
fun runInScope(
|
|
||||||
actionSuccess: suspend () -> Unit,
|
|
||||||
actionError: suspend () -> Unit
|
|
||||||
) {
|
|
||||||
viewModelScope.launch {
|
|
||||||
apiStatus = ApiStatus.LOADING
|
|
||||||
runCatching {
|
|
||||||
actionSuccess()
|
|
||||||
apiStatus = ApiStatus.DONE
|
|
||||||
apiError = ""
|
|
||||||
}.onFailure { e: Throwable ->
|
|
||||||
when (e) {
|
|
||||||
is IOException,
|
|
||||||
is HttpException -> {
|
|
||||||
actionError()
|
|
||||||
apiStatus = ApiStatus.ERROR
|
|
||||||
apiError = e.localizedMessage ?: e.toString()
|
|
||||||
}
|
|
||||||
else -> throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun runInScope(actionSuccess: suspend () -> Unit) {
|
|
||||||
runInScope(actionSuccess, actionError = {})
|
|
||||||
}
|
|
||||||
|
|
||||||
fun clearStatus(){
|
|
||||||
apiStatus = ApiStatus.NONE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun LoadingScreen(color: Color) {
|
|
||||||
Box(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(16.dp),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
CircularProgressIndicator(
|
|
||||||
color = color,
|
|
||||||
strokeWidth = 6.dp
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -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)
|
|
||||||
|
|
||||||
suspend fun getMail(id: Int): 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,27 +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)
|
|
||||||
}
|
|
||||||
initializer {
|
|
||||||
ReportViewModel(app().container.reportRepository)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun CreationExtras.app(): MobileApp =
|
|
||||||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MobileApp)
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.example.mobileapp.database.viewmodels
|
|
||||||
|
|
||||||
import androidx.compose.runtime.MutableState
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.example.mobileapp.api.model.ReportRemote
|
|
||||||
import com.example.mobileapp.database.repositories.ReportRepository
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class ReportViewModel(private val reportRepository: ReportRepository): ViewModel() {
|
|
||||||
private var _report = mutableStateOf<ReportRemote?>(null)
|
|
||||||
val report: MutableState<ReportRemote?> get() = _report
|
|
||||||
|
|
||||||
fun createReport(dateFrom: Long, dateTo: Long) = viewModelScope.launch {
|
|
||||||
_report.value = reportRepository.createReport(dateFrom, dateTo)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun clearReport(){
|
|
||||||
_report.value = null
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +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.GlobalUser
|
|
||||||
import com.example.mobileapp.database.entities.Story
|
|
||||||
import com.example.mobileapp.database.repositories.StoryRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class StoryViewModel(private val storyRepository: StoryRepository): ViewModel() {
|
|
||||||
|
|
||||||
val getAllStories: Flow<PagingData<Story>> = storyRepository.getAllStories().cachedIn(viewModelScope)
|
|
||||||
|
|
||||||
suspend fun getStoryById(id: Int): Story? = storyRepository.getStoryById(id)
|
|
||||||
|
|
||||||
val getStoriesByUserId: Flow<PagingData<Story>> = GlobalUser.getInstance().getUser()?.id?.let {
|
|
||||||
storyRepository.getStoriesByUserId(it).cachedIn(viewModelScope)
|
|
||||||
} ?: flowOf(PagingData.empty())
|
|
||||||
|
|
||||||
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,88 +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.api.model.UserRemoteSignIn
|
|
||||||
import com.example.mobileapp.api.model.toUserRemote
|
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import com.example.mobileapp.database.repositories.OfflineUserRepository
|
|
||||||
import com.example.mobileapp.database.repositories.UserRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class UserViewModel(private val userRepository: UserRepository): CustomViewModel() {
|
|
||||||
//val getAllUsers = userRepository.getAllUsers()
|
|
||||||
|
|
||||||
suspend fun getUser(id: Int): 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
|
|
||||||
}
|
|
||||||
runInScope(
|
|
||||||
actionSuccess = {
|
|
||||||
userRepository.updateUser(user)
|
|
||||||
GlobalUser.getInstance().setUser(user)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteUser(user: User) = viewModelScope.launch {
|
|
||||||
userRepository.deleteUser(user)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun regUser(user: User) = viewModelScope.launch {
|
|
||||||
if(user.password.isEmpty()){
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
if(user.email.isEmpty() || !isValidEmail(user.email)){
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
runInScope(
|
|
||||||
actionSuccess = {
|
|
||||||
userRepository.insertUser(user)
|
|
||||||
GlobalUser.getInstance().setUser(userRepository.getUserByLogin(
|
|
||||||
UserRemoteSignIn(user.login, user.password)))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
/*userRepository.insertUser(user)
|
|
||||||
GlobalUser.getInstance().setUser(userRepository.getUserByLogin(
|
|
||||||
UserRemoteSignIn(user.login, user.password)))*/
|
|
||||||
}
|
|
||||||
|
|
||||||
fun authUser(user: User) = viewModelScope.launch {
|
|
||||||
runInScope(
|
|
||||||
actionSuccess = {
|
|
||||||
val globalUser = userRepository.getUserByLogin(UserRemoteSignIn(user.login, user.password))
|
|
||||||
globalUser?.let {
|
|
||||||
if (user.password.isNotEmpty() && user.password == globalUser.password){
|
|
||||||
GlobalUser.getInstance().setUser(globalUser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actionError = {
|
|
||||||
GlobalUser.getInstance().setUser(null)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
/*val globalUser = userRepository.getUserByLogin(UserRemoteSignIn(user.login, user.password))
|
|
||||||
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
app/src/main/java/com/example/mobileapp/entities/Mail.kt
Normal file
8
app/src/main/java/com/example/mobileapp/entities/Mail.kt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mobileapp.entities
|
||||||
|
|
||||||
|
data class Mail(
|
||||||
|
val id: Int,
|
||||||
|
val userId: Int,
|
||||||
|
val username: String,
|
||||||
|
val message: String
|
||||||
|
)
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.mobileapp.entities
|
||||||
|
|
||||||
|
class MailSingleton {
|
||||||
|
companion object {
|
||||||
|
val mailList: MutableList<Mail> = mutableListOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addMail(mail: Mail) {
|
||||||
|
mailList.add(mail)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMailList(): List<Mail> {
|
||||||
|
return mailList.toList()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mobileapp.entities
|
||||||
|
|
||||||
|
data class Story(
|
||||||
|
val id: Int,
|
||||||
|
val title: String,
|
||||||
|
val description: String,
|
||||||
|
val cover: Int
|
||||||
|
)
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.mobileapp.entities
|
||||||
|
|
||||||
|
class StorySingleton {
|
||||||
|
companion object {
|
||||||
|
val storyList: MutableList<Story> = mutableListOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addStory(story: Story) {
|
||||||
|
storyList.add(story)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getStoryList(): List<Story> {
|
||||||
|
return storyList.toList()
|
||||||
|
}
|
||||||
|
}
|
7
app/src/main/java/com/example/mobileapp/entities/User.kt
Normal file
7
app/src/main/java/com/example/mobileapp/entities/User.kt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.mobileapp.entities
|
||||||
|
|
||||||
|
data class User(
|
||||||
|
val email: String,
|
||||||
|
val login: String,
|
||||||
|
val password: String
|
||||||
|
)
|
@ -1,6 +1,5 @@
|
|||||||
package com.example.mobileapp.screens
|
package com.example.mobileapp.screens
|
||||||
|
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
@ -8,99 +7,41 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.runtime.Composable
|
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.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.mobileapp.GlobalUser
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.api.ApiStatus
|
|
||||||
import com.example.mobileapp.components.ActiveButton
|
|
||||||
import com.example.mobileapp.components.NavigationButton
|
import com.example.mobileapp.components.NavigationButton
|
||||||
import com.example.mobileapp.components.PasswordInputField
|
import com.example.mobileapp.components.PasswordInputField
|
||||||
import com.example.mobileapp.components.PlaceholderInputField
|
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.LoadingScreen
|
|
||||||
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.ButtonColor1
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Authorization(navController: NavHostController,
|
fun Authorization(navController: NavHostController){
|
||||||
userViewModel: UserViewModel = viewModel(
|
Column(
|
||||||
factory = MobileAppViewModelProvider.Factory
|
modifier = Modifier
|
||||||
)) {
|
.fillMaxSize()
|
||||||
val context = LocalContext.current
|
.padding(bottom = 8.dp),
|
||||||
|
verticalArrangement = Arrangement.Bottom
|
||||||
when(userViewModel.apiStatus){
|
) {
|
||||||
ApiStatus.DONE -> {
|
Image(
|
||||||
navController.navigate("main")
|
painter = painterResource(id = R.drawable.login),
|
||||||
userViewModel.clearStatus()
|
contentDescription = "login",
|
||||||
}
|
contentScale = ContentScale.Crop,
|
||||||
ApiStatus.LOADING -> LoadingScreen(ButtonColor2)
|
|
||||||
ApiStatus.ERROR -> Toast.makeText(context, "Не верные данные или пользователя не существует: "
|
|
||||||
+ userViewModel.apiError, Toast.LENGTH_SHORT).show()
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
if(userViewModel.apiStatus != ApiStatus.LOADING) {
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.size(512.dp)
|
||||||
.padding(bottom = 8.dp),
|
.padding(8.dp)
|
||||||
verticalArrangement = Arrangement.Bottom
|
.align(Alignment.CenterHorizontally))
|
||||||
) {
|
PlaceholderInputField(label = "Логин")
|
||||||
Image(
|
PasswordInputField(label = "Пароль")
|
||||||
painter = painterResource(id = R.drawable.login),
|
NavigationButton(navController = navController, destination = "main", label = "Вход",
|
||||||
contentDescription = "login",
|
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||||
contentScale = ContentScale.Crop,
|
NavigationButton(navController = navController, destination = "registration", label = "Регистрация",
|
||||||
modifier = Modifier
|
backgroundColor = ButtonColor1, textColor = Color.Black)
|
||||||
.size(448.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()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
NavigationButton(
|
|
||||||
navController = navController, destination = "registration", label = "Регистрация",
|
|
||||||
backgroundColor = ButtonColor1, textColor = Color.Black
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,178 +1,53 @@
|
|||||||
package com.example.mobileapp.screens
|
package com.example.mobileapp.screens
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.graphics.ImageDecoder
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import android.provider.MediaStore
|
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.border
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.shape.CircleShape
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.runtime.Composable
|
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.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.blur
|
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.ColorFilter
|
|
||||||
import androidx.compose.ui.graphics.ColorMatrix
|
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.mobileapp.GlobalUser
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.api.ApiStatus
|
|
||||||
import com.example.mobileapp.components.ActiveButton
|
import com.example.mobileapp.components.ActiveButton
|
||||||
import com.example.mobileapp.components.NavigationButton
|
import com.example.mobileapp.components.NavigationButton
|
||||||
|
import com.example.mobileapp.components.PasswordInputField
|
||||||
import com.example.mobileapp.components.PlaceholderInputField
|
import com.example.mobileapp.components.PlaceholderInputField
|
||||||
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.LoadingScreen
|
|
||||||
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.ButtonColor1
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
|
fun EditStoryScreen(navController: NavHostController) {
|
||||||
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 imageData = remember { mutableStateOf<Uri?>(null) }
|
|
||||||
val launcher =
|
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {uri: Uri? ->
|
|
||||||
imageData.value = uri
|
|
||||||
}
|
|
||||||
imageData.value?.let {
|
|
||||||
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!!)
|
|
||||||
cover.value = ImageDecoder.decodeBitmap(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
storyId?.let {
|
|
||||||
val story = storyViewModel.getStoryById(storyId)
|
|
||||||
if (story != null) {
|
|
||||||
cover.value = story.cover
|
|
||||||
title.value = story.title
|
|
||||||
description.value = story.description
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(bottom = 8.dp)
|
.padding(bottom = 8.dp),
|
||||||
.verticalScroll(rememberScrollState()),
|
verticalArrangement = Arrangement.Bottom
|
||||||
verticalArrangement = Arrangement.Bottom,
|
|
||||||
) {
|
) {
|
||||||
Box(
|
Image(
|
||||||
|
painter = painterResource(id = R.drawable.editplaceholder),
|
||||||
|
contentDescription = "editplaceholder",
|
||||||
|
contentScale = ContentScale.Crop,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(512.dp),
|
.size(512.dp)
|
||||||
contentAlignment = Alignment.Center
|
.padding(8.dp)
|
||||||
) {
|
.align(Alignment.CenterHorizontally))
|
||||||
Image(
|
PlaceholderInputField(label = "Название")
|
||||||
bitmap = cover.value.asImageBitmap(),
|
ActiveButton(label = "Выбрать обложку", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {})
|
||||||
contentDescription = "Background Image",
|
PlaceholderInputField(label = "Описание")
|
||||||
contentScale = ContentScale.Crop,
|
NavigationButton(navController = navController, destination = "listdata", label = "Назад",
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp)
|
|
||||||
.blur(12.dp),
|
|
||||||
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
|
|
||||||
Image(
|
|
||||||
bitmap = cover.value.asImageBitmap(),
|
|
||||||
contentDescription = "cover",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(8.dp)
|
|
||||||
.size(384.dp)
|
|
||||||
.clip(RoundedCornerShape(16.dp)))
|
|
||||||
}
|
|
||||||
ActiveButton(label = "Выбрать обложку", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
|
||||||
launcher.launch("image/*")
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Название", isSingleLine = true,
|
|
||||||
startValue = title.value, onTextChanged = { newName ->
|
|
||||||
title.value = newName
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Описание", isSingleLine = true,
|
|
||||||
startValue = description.value, onTextChanged = { newDescription ->
|
|
||||||
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")
|
|
||||||
})
|
|
||||||
NavigationButton(navController = navController, destination = "story", label = "Назад",
|
|
||||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditMailScreen(navController: NavHostController,
|
fun EditMailScreen(navController: NavHostController) {
|
||||||
mailViewModel: MailViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val message = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
@ -187,143 +62,9 @@ fun EditMailScreen(navController: NavHostController,
|
|||||||
.size(512.dp)
|
.size(512.dp)
|
||||||
.padding(8.dp)
|
.padding(8.dp)
|
||||||
.align(Alignment.CenterHorizontally))
|
.align(Alignment.CenterHorizontally))
|
||||||
PlaceholderInputField(label = "Текс поста", isSingleLine = false, onTextChanged = { newmessage ->
|
PlaceholderInputField(label = "Заголовок")
|
||||||
message.value = newmessage
|
PlaceholderInputField(label = "Текс поста")
|
||||||
})
|
|
||||||
ActiveButton(label = "Сохранить", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
|
||||||
mailViewModel.insertMail(
|
|
||||||
Mail(
|
|
||||||
message = message.value,
|
|
||||||
userId = GlobalUser.getInstance().getUser()?.id!!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
navController.navigate("mail")
|
|
||||||
})
|
|
||||||
NavigationButton(navController = navController, destination = "mail", label = "Назад",
|
NavigationButton(navController = navController, destination = "mail", label = "Назад",
|
||||||
backgroundColor = ButtonColor2, textColor = Color.White)
|
backgroundColor = ButtonColor2, textColor = Color.White)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun EditUserScreen(navController: NavHostController,
|
|
||||||
userViewModel: UserViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
when(userViewModel.apiStatus){
|
|
||||||
ApiStatus.DONE -> {
|
|
||||||
navController.navigate("settings")
|
|
||||||
userViewModel.clearStatus()
|
|
||||||
}
|
|
||||||
ApiStatus.LOADING -> LoadingScreen(ButtonColor2)
|
|
||||||
ApiStatus.ERROR -> Toast.makeText(context, "Не удалось обновить данные пользователя: "
|
|
||||||
+ userViewModel.apiError, Toast.LENGTH_SHORT).show()
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
var userId = remember { mutableStateOf(0) }
|
|
||||||
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
val email = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
val imageData = remember { mutableStateOf<Uri?>(null) }
|
|
||||||
val launcher =
|
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {uri: Uri? ->
|
|
||||||
imageData.value = uri
|
|
||||||
}
|
|
||||||
imageData.value?.let {
|
|
||||||
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!!)
|
|
||||||
photo.value = ImageDecoder.decodeBitmap(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(userViewModel.apiStatus != ApiStatus.LOADING) {
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(bottom = 8.dp)
|
|
||||||
.verticalScroll(rememberScrollState()),
|
|
||||||
verticalArrangement = Arrangement.Bottom
|
|
||||||
) {
|
|
||||||
Box(
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
Image(
|
|
||||||
bitmap = photo.value.asImageBitmap(),
|
|
||||||
contentDescription = "Background Image",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp)
|
|
||||||
.blur(12.dp),
|
|
||||||
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) })
|
|
||||||
)
|
|
||||||
Image(
|
|
||||||
bitmap = photo.value.asImageBitmap(),
|
|
||||||
contentDescription = "editplaceholder",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(8.dp)
|
|
||||||
.clip(CircleShape)
|
|
||||||
.size(384.dp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ActiveButton(
|
|
||||||
label = "Выбрать фото",
|
|
||||||
backgroundColor = ButtonColor1,
|
|
||||||
textColor = Color.Black,
|
|
||||||
onClickAction = {
|
|
||||||
launcher.launch("image/*")
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Никнейм", isSingleLine = true,
|
|
||||||
startValue = login.value, onTextChanged = { newLogin ->
|
|
||||||
login.value = newLogin
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Пароль", isSingleLine = true,
|
|
||||||
startValue = password.value, onTextChanged = { newPassword ->
|
|
||||||
password.value = newPassword
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Почта", isSingleLine = true,
|
|
||||||
startValue = email.value, onTextChanged = { newEmail ->
|
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
ActiveButton(label = "Назад", backgroundColor = ButtonColor2, textColor = Color.White,
|
|
||||||
onClickAction = {
|
|
||||||
navController.navigate("settings")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,40 @@
|
|||||||
|
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.ui.Modifier
|
||||||
|
import androidx.navigation.NavHostController
|
||||||
|
import com.example.mobileapp.components.DataListScroll
|
||||||
|
import com.example.mobileapp.components.NavBar
|
||||||
|
import com.example.mobileapp.ui.theme.BackgroundItem1
|
||||||
|
import com.example.mobileapp.ui.theme.BackgroundItem2
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ListDataScreen(navController: NavHostController){
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.background(BackgroundItem1)
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.fillMaxHeight(0.9f)
|
||||||
|
){
|
||||||
|
DataListScroll(navController)
|
||||||
|
}
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize(),
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
NavBar(navController = navController, itemColorFilter = BackgroundItem2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +1,40 @@
|
|||||||
package com.example.mobileapp.screens
|
package com.example.mobileapp.screens
|
||||||
|
|
||||||
import androidx.compose.foundation.background
|
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.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.lazy.grid.GridCells
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
|
||||||
import androidx.compose.runtime.Composable
|
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.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.paging.compose.collectAsLazyPagingItems
|
import com.example.mobileapp.components.MailListScroll
|
||||||
import androidx.paging.compose.itemKey
|
import com.example.mobileapp.components.NavBar
|
||||||
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 com.example.mobileapp.ui.theme.BackgroundItem1
|
||||||
import kotlinx.coroutines.Dispatchers
|
import com.example.mobileapp.ui.theme.BackgroundItem2
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ListMailScreen(navController: NavHostController,
|
fun ListMailScreen(navController: NavHostController){
|
||||||
mailViewModel: MailViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val mails = mailViewModel.getAllMails.collectAsLazyPagingItems()
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.background(BackgroundItem1)
|
.background(BackgroundItem1)
|
||||||
) {
|
) {
|
||||||
LazyVerticalGrid(
|
Box(
|
||||||
columns = GridCells.Fixed(1)
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.fillMaxHeight(0.9f)
|
||||||
|
){
|
||||||
|
MailListScroll(navController)
|
||||||
|
}
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize(),
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
) {
|
) {
|
||||||
item {
|
NavBar(navController = navController, itemColorFilter = BackgroundItem2)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,62 +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.layout.fillMaxWidth
|
|
||||||
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.itemsIndexed
|
|
||||||
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.Alignment
|
|
||||||
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
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ListStoryScreen(navController: NavHostController,
|
|
||||||
storyViewModel: StoryViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val stories = storyViewModel.getStoriesByUserId.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,12 +8,8 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
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.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
@ -22,69 +18,45 @@ import androidx.compose.ui.res.painterResource
|
|||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.paging.compose.collectAsLazyPagingItems
|
|
||||||
import androidx.paging.compose.itemKey
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.components.NavBar
|
import com.example.mobileapp.components.NavBar
|
||||||
import com.example.mobileapp.components.SearchInputField
|
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
|
@Composable
|
||||||
fun MainScreen(navController: NavHostController,
|
fun MainScreen(navController: NavHostController) {
|
||||||
storyViewModel: StoryViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory
|
|
||||||
)) {
|
|
||||||
val stories = storyViewModel.getAllStories.collectAsLazyPagingItems()
|
|
||||||
|
|
||||||
val search = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
SearchInputField(onTextChanged = {newsearch ->
|
SearchInputField()
|
||||||
search.value = newsearch
|
Column(
|
||||||
})
|
modifier = Modifier
|
||||||
if (stories.itemCount > 0){
|
.fillMaxWidth()
|
||||||
LazyVerticalGrid(
|
.fillMaxHeight(0.89f),
|
||||||
columns = GridCells.Fixed(1)
|
verticalArrangement = Arrangement.Center,
|
||||||
) {
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
items(
|
) {
|
||||||
count = stories.itemCount,
|
Image(
|
||||||
key = stories.itemKey { item -> item.id!! }
|
painter = painterResource(id = R.drawable.main),
|
||||||
) { index: Int ->
|
contentDescription = "main",
|
||||||
val story: Story? = stories[index]
|
contentScale = ContentScale.Crop,
|
||||||
if (story != null && (search.value.isEmpty() || story.title.contains(search.value, ignoreCase = true))) {
|
modifier = Modifier
|
||||||
StoryListItem(item = story, navController = navController, isReadOnly = true)
|
.size(512.dp)
|
||||||
}
|
.padding(8.dp)
|
||||||
}
|
)
|
||||||
}
|
Text(
|
||||||
|
text = "Здесь будут посты авторов",
|
||||||
|
fontSize = 20.sp,
|
||||||
|
fontWeight = FontWeight.Bold
|
||||||
|
)
|
||||||
}
|
}
|
||||||
else {
|
Column(
|
||||||
Column(
|
modifier = Modifier
|
||||||
verticalArrangement = Arrangement.Center,
|
.fillMaxSize(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
verticalArrangement = Arrangement.Center
|
||||||
) {
|
) {
|
||||||
Image(
|
NavBar(navController = navController, itemColorFilter = Color.Black)
|
||||||
painter = painterResource(id = R.drawable.main),
|
|
||||||
contentDescription = "main",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp)
|
|
||||||
.padding(8.dp)
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
text = "Здесь будут посты авторов",
|
|
||||||
fontSize = 20.sp,
|
|
||||||
fontWeight = FontWeight.Bold
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package com.example.mobileapp.screens
|
package com.example.mobileapp.screens
|
||||||
|
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
@ -8,102 +7,43 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.mobileapp.GlobalUser
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.api.ApiStatus
|
|
||||||
import com.example.mobileapp.components.ActiveButton
|
|
||||||
import com.example.mobileapp.components.NavigationButton
|
import com.example.mobileapp.components.NavigationButton
|
||||||
import com.example.mobileapp.components.PasswordInputField
|
import com.example.mobileapp.components.PasswordInputField
|
||||||
import com.example.mobileapp.components.PlaceholderInputField
|
import com.example.mobileapp.components.PlaceholderInputField
|
||||||
import com.example.mobileapp.database.entities.User
|
|
||||||
import com.example.mobileapp.database.viewmodels.LoadingScreen
|
|
||||||
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.ButtonColor1
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
import com.example.mobileapp.ui.theme.ButtonColor2
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Registration(navController: NavHostController,
|
fun Registration(navController: NavHostController){
|
||||||
userViewModel: UserViewModel = viewModel(
|
Column(
|
||||||
factory = MobileAppViewModelProvider.Factory
|
modifier = Modifier
|
||||||
)) {
|
.fillMaxSize()
|
||||||
val context = LocalContext.current
|
.padding(bottom = 8.dp),
|
||||||
when(userViewModel.apiStatus){
|
verticalArrangement = Arrangement.Bottom
|
||||||
ApiStatus.DONE -> {
|
) {
|
||||||
navController.navigate("main")
|
Image(
|
||||||
userViewModel.clearStatus()
|
painter = painterResource(id = R.drawable.registration),
|
||||||
}
|
contentDescription = "registration",
|
||||||
ApiStatus.LOADING -> LoadingScreen(ButtonColor2)
|
contentScale = ContentScale.Crop,
|
||||||
ApiStatus.ERROR -> Toast.makeText(context, "Не удалось создать пользователя: " + userViewModel.apiError, Toast.LENGTH_SHORT).show()
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val email = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
val repeatepassword = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
if(userViewModel.apiStatus != ApiStatus.LOADING) {
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.size(384.dp)
|
||||||
.padding(bottom = 8.dp),
|
.padding(8.dp)
|
||||||
verticalArrangement = Arrangement.Bottom
|
.align(Alignment.CenterHorizontally))
|
||||||
) {
|
PlaceholderInputField(label = "Логин")
|
||||||
Image(
|
PlaceholderInputField(label = "Email")
|
||||||
painter = painterResource(id = R.drawable.registration),
|
PasswordInputField(label = "Пароль")
|
||||||
contentDescription = "registration",
|
PasswordInputField(label = "Пароль ещё раз")
|
||||||
contentScale = ContentScale.Crop,
|
NavigationButton(navController = navController, destination = "main",
|
||||||
modifier = Modifier
|
label = "Зарегистрироваться", backgroundColor = ButtonColor2, textColor = Color.White)
|
||||||
.size(320.dp)
|
NavigationButton(navController = navController, destination = "authorization",
|
||||||
.padding(8.dp)
|
label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black)
|
||||||
.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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
NavigationButton(
|
|
||||||
navController = navController, destination = "authorization",
|
|
||||||
label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,146 +0,0 @@
|
|||||||
package com.example.mobileapp.screens
|
|
||||||
|
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
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.lazy.items
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
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.draw.clip
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
|
||||||
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.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavController
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import com.example.mobileapp.api.model.toStory
|
|
||||||
import com.example.mobileapp.api.model.toUser
|
|
||||||
import com.example.mobileapp.components.ActiveButton
|
|
||||||
import com.example.mobileapp.components.DatePicker
|
|
||||||
import com.example.mobileapp.components.MailListItem
|
|
||||||
import com.example.mobileapp.components.StoryListItem
|
|
||||||
import com.example.mobileapp.components.addNewListItem
|
|
||||||
import com.example.mobileapp.components.isListOf
|
|
||||||
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.ReportViewModel
|
|
||||||
import com.example.mobileapp.ui.theme.ButtonColor2
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ReportScreen(navController: NavHostController,
|
|
||||||
reportViewModel: ReportViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)){
|
|
||||||
val dateFormat = SimpleDateFormat("dd.MM.yyyy")
|
|
||||||
|
|
||||||
val dateFrom = remember { mutableStateOf(Date().time) }
|
|
||||||
val dateTo = remember { mutableStateOf(Date().time) }
|
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
verticalArrangement = Arrangement.Center
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
|
||||||
) {
|
|
||||||
Text(text = "Отчёт по публикациям:",
|
|
||||||
fontSize = 28.sp,
|
|
||||||
fontWeight = FontWeight.Bold,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(top = 16.dp, bottom = 16.dp, start = 8.dp, end = 8.dp))
|
|
||||||
}
|
|
||||||
if(reportViewModel.report.value == null) {
|
|
||||||
DatePicker(startValue = dateFrom.value, onDateSelected = { newDateFrom ->
|
|
||||||
dateFrom.value = newDateFrom
|
|
||||||
})
|
|
||||||
DatePicker(startValue = dateTo.value, onDateSelected = { newDateTo ->
|
|
||||||
dateTo.value = newDateTo
|
|
||||||
})
|
|
||||||
ActiveButton(label = "Сформировать", backgroundColor = ButtonColor2,
|
|
||||||
textColor = Color.White, onClickAction = {
|
|
||||||
reportViewModel.createReport(dateFrom.value, dateTo.value)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Text(text = "Дата с ${dateFormat.format(reportViewModel.report.value?.dateFrom?.let { Date(it) })}",
|
|
||||||
fontSize = 20.sp,
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
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))
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.Top
|
|
||||||
){
|
|
||||||
Image(bitmap = reportViewModel.report.value?.mostPostAuthor?.toUser()?.photo!!.asImageBitmap(),
|
|
||||||
contentDescription = "message",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(96.dp)
|
|
||||||
.padding(8.dp)
|
|
||||||
.clip(RoundedCornerShape(16.dp)))
|
|
||||||
Column(
|
|
||||||
modifier = Modifier.padding(8.dp)
|
|
||||||
){
|
|
||||||
Text(
|
|
||||||
text = "${reportViewModel.report.value?.mostPostAuthor?.toUser()?.login}",
|
|
||||||
fontSize = 20.sp,
|
|
||||||
fontWeight = FontWeight.Bold)
|
|
||||||
Text(text = "Кол-во публикаций у пользователя:${reportViewModel.report.value?.mostPostCount}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LazyColumn(
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
){
|
|
||||||
val list = reportViewModel.report.value?.listPostAuthor!!.map {
|
|
||||||
it.toStory()
|
|
||||||
}
|
|
||||||
items(list){ item ->
|
|
||||||
StoryListItem(item = item, navController = navController, isReadOnly = true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*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))*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,7 +27,6 @@ import androidx.compose.ui.text.font.FontWeight
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.mobileapp.GlobalUser
|
|
||||||
import com.example.mobileapp.R
|
import com.example.mobileapp.R
|
||||||
import com.example.mobileapp.components.IconButton
|
import com.example.mobileapp.components.IconButton
|
||||||
import com.example.mobileapp.components.NavBar
|
import com.example.mobileapp.components.NavBar
|
||||||
@ -38,37 +37,39 @@ import com.example.mobileapp.ui.theme.ButtonColor2
|
|||||||
fun SettingsScreen(navController: NavHostController){
|
fun SettingsScreen(navController: NavHostController){
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize(),
|
.fillMaxSize()
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
|
||||||
) {
|
) {
|
||||||
Image(
|
Column(
|
||||||
painter = painterResource(id = R.drawable.settingsplaceholder),
|
|
||||||
contentDescription = "settings",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(320.dp)
|
.fillMaxWidth()
|
||||||
.padding(8.dp))
|
.fillMaxHeight(0.9f),
|
||||||
IconButton(iconLeft = Icons.Default.AccountCircle, label = "Учётная запись",
|
verticalArrangement = Arrangement.Top,
|
||||||
backgroundColor = ButtonColor2, textColor = Color.White, onClickAction = {
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
navController.navigate("edituser")
|
){
|
||||||
})
|
Image(
|
||||||
IconButton(iconLeft = Icons.Default.Face, label = "Внешний вид",
|
painter = painterResource(id = R.drawable.settingsplaceholder),
|
||||||
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
contentDescription = "settings",
|
||||||
|
contentScale = ContentScale.Crop,
|
||||||
})
|
modifier = Modifier
|
||||||
IconButton(iconLeft = Icons.Default.Share, label = "Контакты",
|
.size(384.dp)
|
||||||
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
.padding(8.dp))
|
||||||
|
IconButton(iconLeft = Icons.Default.AccountCircle, label = "Учётная запись",
|
||||||
})
|
backgroundColor = ButtonColor2, textColor = Color.White, onClickAction = { })
|
||||||
IconButton(iconLeft = Icons.Default.Info, label = "О приложении",
|
IconButton(iconLeft = Icons.Default.Face, label = "Внешний вид",
|
||||||
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
|
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { })
|
||||||
|
IconButton(iconLeft = Icons.Default.Share, label = "Контакты",
|
||||||
})
|
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { })
|
||||||
IconButton(iconLeft = Icons.Default.ExitToApp, label = "Выйти",
|
IconButton(iconLeft = Icons.Default.Info, label = "О приложении",
|
||||||
backgroundColor = Color.Red, textColor = Color.White, onClickAction = {
|
backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { })
|
||||||
GlobalUser.getInstance().setUser(null)
|
IconButton(iconLeft = Icons.Default.ExitToApp, label = "Выйти",
|
||||||
navController.navigate("authorization")
|
backgroundColor = Color.Red, textColor = Color.White, onClickAction = { })
|
||||||
})
|
}
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize(),
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
NavBar(navController = navController, itemColorFilter = Color.Black)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,196 +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.Box
|
|
||||||
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.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
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.blur
|
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.graphics.ColorFilter
|
|
||||||
import androidx.compose.ui.graphics.ColorMatrix
|
|
||||||
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) }
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
storyId?.let {
|
|
||||||
val story = storyViewModel.getStoryById(storyId)
|
|
||||||
if (story != null) {
|
|
||||||
cover.value = story.cover
|
|
||||||
title.value = story.title
|
|
||||||
description.value = story.description
|
|
||||||
postdate.value = story.postdate!!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(bottom = 8.dp),
|
|
||||||
verticalArrangement = Arrangement.Center
|
|
||||||
) {
|
|
||||||
Box(
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
Image(
|
|
||||||
bitmap = cover.value.asImageBitmap(),
|
|
||||||
contentDescription = "Background Image",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp)
|
|
||||||
.blur(12.dp),
|
|
||||||
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
|
|
||||||
Image(
|
|
||||||
bitmap = cover.value.asImageBitmap(),
|
|
||||||
contentDescription = "cover",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(8.dp)
|
|
||||||
.size(384.dp)
|
|
||||||
.clip(RoundedCornerShape(16.dp)))
|
|
||||||
}
|
|
||||||
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){
|
|
||||||
val mail = mailViewModel.getMail(mailId)
|
|
||||||
if (mail != null) {
|
|
||||||
message.value = mail.message
|
|
||||||
postdate.value = mail.postdate!!
|
|
||||||
}
|
|
||||||
val user = mail?.let { userViewModel.getUser(it.userId) }
|
|
||||||
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
|
|
||||||
) {
|
|
||||||
Box(
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
Image(
|
|
||||||
bitmap = photo.value.asImageBitmap(),
|
|
||||||
contentDescription = "Background Image",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(512.dp)
|
|
||||||
.blur(12.dp),
|
|
||||||
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
|
|
||||||
Image(
|
|
||||||
bitmap = photo.value.asImageBitmap(),
|
|
||||||
contentDescription = "editplaceholder",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(8.dp)
|
|
||||||
.clip(CircleShape)
|
|
||||||
.size(384.dp))
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
@ -2,5 +2,4 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("com.android.application") version "8.1.1" apply false
|
id("com.android.application") version "8.1.1" apply false
|
||||||
id("org.jetbrains.kotlin.android") version "1.8.10" apply false
|
id("org.jetbrains.kotlin.android") version "1.8.10" apply false
|
||||||
id("com.google.devtools.ksp") version "1.8.20-1.0.11" apply false
|
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user