Compare commits
No commits in common. "LabWork04" and "master" have entirely different histories.
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,19 +1,16 @@
|
|||||||
import org.jetbrains.kotlin.kapt3.base.Kapt.kapt
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "com.example.myapplication"
|
namespace = "com.example.myapplication"
|
||||||
compileSdk = 34
|
compileSdk = 33
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "com.example.myapplication"
|
applicationId = "com.example.myapplication"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 34
|
targetSdk = 33
|
||||||
versionCode = 1
|
versionCode = 1
|
||||||
versionName = "1.0"
|
versionName = "1.0"
|
||||||
|
|
||||||
@ -33,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 {
|
||||||
@ -52,12 +49,11 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
implementation("androidx.core:core-ktx:1.9.0")
|
implementation("androidx.core:core-ktx:1.9.0")
|
||||||
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
|
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
|
||||||
implementation("androidx.activity:activity-compose:1.8.1")
|
implementation("androidx.activity:activity-compose:1.7.2")
|
||||||
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
|
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
|
||||||
implementation("androidx.compose.ui:ui")
|
implementation("androidx.compose.ui:ui")
|
||||||
implementation("androidx.compose.ui:ui-graphics")
|
implementation("androidx.compose.ui:ui-graphics")
|
||||||
@ -71,17 +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")
|
||||||
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
}
|
}
|
@ -3,7 +3,6 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<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"
|
||||||
|
@ -19,14 +19,12 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
|||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material3.Card
|
import androidx.compose.material3.Card
|
||||||
import androidx.compose.material3.CardDefaults
|
import androidx.compose.material3.CardDefaults
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
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.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
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
|
||||||
@ -43,15 +41,11 @@ import androidx.compose.ui.tooling.preview.Preview
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.navigation.NavType
|
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import androidx.navigation.navArgument
|
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.screens.Authorization
|
import com.example.myapplication.screens.Authorization
|
||||||
import com.example.myapplication.screens.EditCardScreen
|
import com.example.myapplication.screens.CreateCard
|
||||||
import com.example.myapplication.screens.EditUserScreen
|
import com.example.myapplication.screens.EditCard
|
||||||
import com.example.myapplication.ui.theme.MyApplicationTheme
|
import com.example.myapplication.ui.theme.MyApplicationTheme
|
||||||
import com.example.myapplication.screens.MainScreen
|
import com.example.myapplication.screens.MainScreen
|
||||||
import com.example.myapplication.screens.Registration
|
import com.example.myapplication.screens.Registration
|
||||||
@ -75,13 +69,11 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AppNavigation(navController: NavHostController){
|
fun AppNavigation(navController: NavHostController){
|
||||||
NavHost(
|
NavHost(
|
||||||
navController = navController, startDestination = "authorization"
|
navController = navController, startDestination = "authorization"
|
||||||
) {
|
) {
|
||||||
|
|
||||||
composable("authorization") {
|
composable("authorization") {
|
||||||
Authorization(navController = navController)
|
Authorization(navController = navController)
|
||||||
}
|
}
|
||||||
@ -91,44 +83,14 @@ fun AppNavigation(navController: NavHostController){
|
|||||||
composable("registration") {
|
composable("registration") {
|
||||||
Registration(navController = navController)
|
Registration(navController = navController)
|
||||||
}
|
}
|
||||||
|
composable("createCard") {
|
||||||
|
CreateCard(navController = navController)
|
||||||
|
}
|
||||||
composable("userSettings") {
|
composable("userSettings") {
|
||||||
UserSettings(navController = navController)
|
UserSettings(navController = navController)
|
||||||
}
|
}
|
||||||
composable("editcard") {
|
composable("editCard") {
|
||||||
EditCardScreen(navController = navController)
|
EditCard(navController = navController)
|
||||||
}
|
|
||||||
composable(
|
|
||||||
"editcard/{id}",
|
|
||||||
arguments = listOf(navArgument("id") { type = NavType.IntType }) //С аргументом
|
|
||||||
) { backStackEntry ->
|
|
||||||
backStackEntry.arguments?.let {
|
|
||||||
EditCardScreen(navController = navController, cardId = it.getInt("id"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
composable("edituser"){
|
|
||||||
EditUserScreen(navController = navController)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GlobalUser private constructor() {
|
|
||||||
private var user = mutableStateOf<User?>(null)
|
|
||||||
|
|
||||||
fun setUser(user: User?) {
|
|
||||||
this.user.value = user
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getUser(): User? {
|
|
||||||
return user.value
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private var instance: GlobalUser? = null
|
|
||||||
|
|
||||||
fun getInstance(): GlobalUser {
|
|
||||||
return instance ?: synchronized(this) {
|
|
||||||
instance ?: GlobalUser().also { instance = it }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
package com.example.myapplication
|
|
||||||
|
|
||||||
import android.app.Application
|
|
||||||
|
|
||||||
class MobileApp: Application() {
|
|
||||||
lateinit var container: MobileAppContainer
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
container = MobileAppDataContainer(this)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
package com.example.myapplication
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
|
||||||
import com.example.myapplication.database.repositories.CardRepository
|
|
||||||
import com.example.myapplication.database.repositories.OfflineCardRepository
|
|
||||||
import com.example.myapplication.database.repositories.OfflineUserRepository
|
|
||||||
import com.example.myapplication.database.repositories.UserRepository
|
|
||||||
|
|
||||||
interface MobileAppContainer {
|
|
||||||
val cardRepository: CardRepository
|
|
||||||
val userRepository: UserRepository
|
|
||||||
}
|
|
||||||
|
|
||||||
class MobileAppDataContainer(private val context: Context): MobileAppContainer {
|
|
||||||
override val cardRepository: CardRepository by lazy {
|
|
||||||
OfflineCardRepository(MobileAppDataBase.getInstance(context).cardDao())
|
|
||||||
}
|
|
||||||
|
|
||||||
override val userRepository: UserRepository by lazy {
|
|
||||||
OfflineUserRepository(MobileAppDataBase.getInstance(context).userDao())
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object{
|
|
||||||
const val TIMEOUT = 5000L
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,119 +0,0 @@
|
|||||||
package com.example.myapplication.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.requiredHeight
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.Button
|
|
||||||
import androidx.compose.material3.ButtonDefaults
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
|
||||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.compose.ui.unit.sp
|
|
||||||
|
|
||||||
val buttonHeightStandard = 72.dp
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun PlaceholderInputField(label: String, startValue: String? = null, isSingleLine: Boolean, onTextChanged: (String) -> Unit){
|
|
||||||
var text = remember { mutableStateOf("") }
|
|
||||||
startValue?.let{
|
|
||||||
text.value = startValue
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
modifier = Modifier.padding(horizontal = 8.dp),
|
|
||||||
text = label,
|
|
||||||
fontSize = 20.sp
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
value = text.value,
|
|
||||||
onValueChange = {
|
|
||||||
text.value = it
|
|
||||||
onTextChanged(it)
|
|
||||||
},
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
|
||||||
shape = RoundedCornerShape(10.dp),
|
|
||||||
singleLine = isSingleLine
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ActiveButton(label: String, backgroundColor: Color, textColor: Color, onClickAction: () -> Unit){
|
|
||||||
Button(
|
|
||||||
onClick = onClickAction,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.requiredHeight(buttonHeightStandard)
|
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = backgroundColor
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(10.dp)
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
text = label,
|
|
||||||
fontSize = 20.sp,
|
|
||||||
fontWeight = FontWeight.Bold,
|
|
||||||
color = textColor
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun PlaceholderInputFieldAuth(label: String, startValue: String? = null, isSingleLine: Boolean, onTextChanged: (String) -> Unit){
|
|
||||||
var text = remember { mutableStateOf("") }
|
|
||||||
startValue?.let{
|
|
||||||
text.value = startValue
|
|
||||||
}
|
|
||||||
OutlinedTextField(
|
|
||||||
value = text.value,
|
|
||||||
onValueChange = {
|
|
||||||
text.value = it
|
|
||||||
onTextChanged(it)
|
|
||||||
},
|
|
||||||
placeholder = {
|
|
||||||
Text(label)
|
|
||||||
},
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
|
||||||
shape = RoundedCornerShape(10.dp),
|
|
||||||
singleLine = isSingleLine
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun PasswordInputField(label: String, startValue: String? = null, onPasswordChanged: (String) -> Unit){
|
|
||||||
var text = remember { mutableStateOf("") }
|
|
||||||
startValue?.let{
|
|
||||||
text.value = startValue
|
|
||||||
}
|
|
||||||
|
|
||||||
OutlinedTextField(
|
|
||||||
value = text.value,
|
|
||||||
onValueChange = {
|
|
||||||
text.value = it
|
|
||||||
onPasswordChanged(it)
|
|
||||||
},
|
|
||||||
placeholder = {
|
|
||||||
Text(label)
|
|
||||||
},
|
|
||||||
visualTransformation = PasswordVisualTransformation(),
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(top = 8.dp, start = 16.dp, bottom = 8.dp, end = 16.dp),
|
|
||||||
shape = RoundedCornerShape(10.dp))
|
|
||||||
}
|
|
@ -41,7 +41,7 @@ fun navBar(navController: NavHostController) {
|
|||||||
verticalAlignment = Alignment.CenterVertically
|
verticalAlignment = Alignment.CenterVertically
|
||||||
) {
|
) {
|
||||||
navItem(navController = navController, ImageId = R.drawable.home_image, "mainScreen")
|
navItem(navController = navController, ImageId = R.drawable.home_image, "mainScreen")
|
||||||
navItem(navController = navController, ImageId = R.drawable.note_image, "editcard")
|
navItem(navController = navController, ImageId = R.drawable.note_image, "createCard")
|
||||||
navItem(navController = navController, ImageId = R.drawable.profile_image, "userSettings")
|
navItem(navController = navController, ImageId = R.drawable.profile_image, "userSettings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,89 +0,0 @@
|
|||||||
package com.example.myapplication.database
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.util.Log
|
|
||||||
import androidx.room.Database
|
|
||||||
import androidx.room.Room
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
import androidx.room.TypeConverters
|
|
||||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
|
||||||
import com.example.myapplication.R
|
|
||||||
import com.example.myapplication.database.entities.ImageConverter
|
|
||||||
import com.example.myapplication.database.dao.CardDao
|
|
||||||
import com.example.myapplication.database.dao.UserDao
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
@Database(entities = [User::class, Card::class], version = 1, exportSchema = false)
|
|
||||||
@TypeConverters(ImageConverter::class)
|
|
||||||
abstract class MobileAppDataBase : RoomDatabase() {
|
|
||||||
abstract fun userDao(): UserDao
|
|
||||||
abstract fun cardDao(): CardDao
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val DB_NAME: String = "my-db.db"
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
private var INSTANCE: MobileAppDataBase? = null
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build()
|
|
||||||
.also { INSTANCE = it }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun initialDataBase(appContext: Context) {
|
|
||||||
INSTANCE?.let { database ->
|
|
||||||
val userDao = database.userDao()
|
|
||||||
userDao.insert(User(id = 1, login = "Margen", password = "1234",))
|
|
||||||
userDao.insert(User(id = 2, login = "Leonel Messi", password = "4321",))
|
|
||||||
|
|
||||||
val cardDao = database.cardDao()
|
|
||||||
cardDao.insert(
|
|
||||||
Card(
|
|
||||||
name = "Феррари Имба",
|
|
||||||
location = "г. Ульяновск",
|
|
||||||
image = BitmapFactory.decodeResource(
|
|
||||||
appContext.resources,
|
|
||||||
R.drawable.ferrari_laferrari_car2
|
|
||||||
),
|
|
||||||
mileage = 7322,
|
|
||||||
price = 125000,
|
|
||||||
userId = 1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
cardDao.insert(
|
|
||||||
Card(
|
|
||||||
name = "Феррари Два",
|
|
||||||
location = "г. Ульяновск",
|
|
||||||
image = BitmapFactory.decodeResource(
|
|
||||||
appContext.resources,
|
|
||||||
R.drawable.ferrari_laferrari_car2
|
|
||||||
),
|
|
||||||
mileage = 1233,
|
|
||||||
price = 15000,
|
|
||||||
userId = 2
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package com.example.myapplication.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.myapplication.database.entities.Card
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
interface CardDao {
|
|
||||||
@Query("select * from cards")
|
|
||||||
fun getAll(): PagingSource<Int, Card>
|
|
||||||
|
|
||||||
@Query("select * from cards where cards.id = :id")
|
|
||||||
fun getById(id: Int): Flow<Card?>
|
|
||||||
|
|
||||||
@Query("select * from cards where cards.user_id = :userId")
|
|
||||||
fun getByUserId(userId: Int): PagingSource<Int, Card>
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
|
||||||
suspend fun insert(card: Card)
|
|
||||||
|
|
||||||
@Update
|
|
||||||
suspend fun update(card: Card)
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
suspend fun delete(card: Card)
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.example.myapplication.database.dao
|
|
||||||
|
|
||||||
import androidx.room.Dao
|
|
||||||
import androidx.room.Delete
|
|
||||||
import androidx.room.Embedded
|
|
||||||
import androidx.room.Insert
|
|
||||||
import androidx.room.OnConflictStrategy
|
|
||||||
import androidx.room.Query
|
|
||||||
import androidx.room.Relation
|
|
||||||
import androidx.room.Transaction
|
|
||||||
import androidx.room.Update
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.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")
|
|
||||||
fun getById(id: Int): Flow<User?>
|
|
||||||
|
|
||||||
@Query("select * from users where users.login = :login")
|
|
||||||
suspend fun getByLogin(login: String): User?
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
|
||||||
suspend fun insert(user: User)
|
|
||||||
|
|
||||||
@Update
|
|
||||||
suspend fun update(user: User)
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
suspend fun delete(user: User)
|
|
||||||
|
|
||||||
@Query("SELECT * FROM users WHERE login = :login AND password = :password")
|
|
||||||
fun getUserByLoginAndPassword(login: String, password: String): User?
|
|
||||||
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.example.myapplication.database.entities
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.ForeignKey
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
|
|
||||||
@Entity(
|
|
||||||
tableName = "cards",
|
|
||||||
foreignKeys = [
|
|
||||||
ForeignKey(
|
|
||||||
entity = User::class,
|
|
||||||
parentColumns = ["id"],
|
|
||||||
childColumns = ["user_id"],
|
|
||||||
onDelete = ForeignKey.RESTRICT,
|
|
||||||
onUpdate = ForeignKey.RESTRICT
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
data class Card(
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
|
||||||
val id: Int? = null,
|
|
||||||
@ColumnInfo(name = "name")
|
|
||||||
val name: String,
|
|
||||||
@ColumnInfo(name = "location")
|
|
||||||
val location: String,
|
|
||||||
@ColumnInfo(name = "mileage")
|
|
||||||
val mileage: Int,
|
|
||||||
@ColumnInfo(name = "price")
|
|
||||||
val price: Int,
|
|
||||||
@ColumnInfo(name = "image")
|
|
||||||
val image: Bitmap,
|
|
||||||
@ColumnInfo(name="user_id")
|
|
||||||
val userId: Int
|
|
||||||
){
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return id ?: -1
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.example.myapplication.database.entities
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import androidx.room.TypeConverter
|
|
||||||
import java.io.ByteArrayOutputStream
|
|
||||||
|
|
||||||
class ImageConverter {
|
|
||||||
@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,19 +0,0 @@
|
|||||||
package com.example.myapplication.database.entities
|
|
||||||
|
|
||||||
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,
|
|
||||||
){
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return id ?: -1
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.example.myapplication.database.repositories
|
|
||||||
|
|
||||||
import androidx.paging.PagingData
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
interface CardRepository {
|
|
||||||
fun getAllCards(): Flow<PagingData<Card>>
|
|
||||||
|
|
||||||
fun getCardsByUserId(userId: Int): Flow<PagingData<Card>>
|
|
||||||
|
|
||||||
fun getCardById(id: Int): Flow<Card?>
|
|
||||||
|
|
||||||
suspend fun insertCard(card: Card)
|
|
||||||
|
|
||||||
suspend fun updateCard(card: Card)
|
|
||||||
|
|
||||||
suspend fun deleteCard(card: Card)
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package com.example.myapplication.database.repositories
|
|
||||||
|
|
||||||
import androidx.paging.Pager
|
|
||||||
import androidx.paging.PagingConfig
|
|
||||||
import androidx.paging.PagingData
|
|
||||||
import com.example.myapplication.database.dao.CardDao
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
class OfflineCardRepository(private val cardDao: CardDao): CardRepository {
|
|
||||||
override fun getAllCards(): Flow<PagingData<Card>> {
|
|
||||||
return Pager(
|
|
||||||
config = PagingConfig(
|
|
||||||
pageSize = 5,
|
|
||||||
prefetchDistance = 1,
|
|
||||||
enablePlaceholders = true,
|
|
||||||
initialLoadSize = 10,
|
|
||||||
maxSize = 15
|
|
||||||
),
|
|
||||||
pagingSourceFactory = {
|
|
||||||
cardDao.getAll()
|
|
||||||
}
|
|
||||||
).flow
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getCardsByUserId(UserId: Int):Flow<PagingData<Card>> {
|
|
||||||
return Pager(
|
|
||||||
config = PagingConfig(
|
|
||||||
pageSize = 5,
|
|
||||||
prefetchDistance = 1,
|
|
||||||
enablePlaceholders = true,
|
|
||||||
initialLoadSize = 10,
|
|
||||||
maxSize = 15
|
|
||||||
),
|
|
||||||
pagingSourceFactory = {
|
|
||||||
cardDao.getByUserId(UserId)
|
|
||||||
}
|
|
||||||
).flow
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getCardById(id: Int): Flow<Card?> = cardDao.getById(id)
|
|
||||||
|
|
||||||
override suspend fun insertCard(card: Card) = cardDao.insert(card)
|
|
||||||
|
|
||||||
override suspend fun updateCard(card: Card) = cardDao.update(card)
|
|
||||||
|
|
||||||
override suspend fun deleteCard(card: Card) = cardDao.delete(card)
|
|
||||||
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.myapplication.database.repositories
|
|
||||||
|
|
||||||
import com.example.myapplication.database.dao.UserDao
|
|
||||||
import com.example.myapplication.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 getUserById(id: Int): Flow<User?> = userDao.getById(id)
|
|
||||||
|
|
||||||
override suspend fun getUserByLogin(login: String): User? = userDao.getByLogin(login)
|
|
||||||
|
|
||||||
override suspend fun insertUser(user: User) = userDao.insert(user)
|
|
||||||
|
|
||||||
override suspend fun updateUser(user: User) = userDao.update(user)
|
|
||||||
|
|
||||||
override suspend fun deleteUser(user: User) = userDao.delete(user)
|
|
||||||
|
|
||||||
override suspend fun getUserByLoginAndPassword(login: String, password: String): User? = userDao.getUserByLoginAndPassword(login, password)
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.myapplication.database.repositories
|
|
||||||
|
|
||||||
import androidx.room.Query
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
interface UserRepository {
|
|
||||||
fun getAllUsers(): Flow<List<User>>
|
|
||||||
|
|
||||||
suspend fun getUserById(id: Int): Flow<User?>
|
|
||||||
|
|
||||||
suspend fun getUserByLogin(login: String): User?
|
|
||||||
|
|
||||||
suspend fun insertUser(user: User)
|
|
||||||
|
|
||||||
suspend fun updateUser(user: User)
|
|
||||||
|
|
||||||
suspend fun deleteUser(user: User)
|
|
||||||
|
|
||||||
suspend fun getUserByLoginAndPassword(login: String, password: String): User?
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.example.myapplication.database.viewmodels
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import androidx.paging.PagingData
|
|
||||||
import androidx.paging.cachedIn
|
|
||||||
import com.example.myapplication.database.dao.CardDao
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.database.repositories.CardRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class CardViewModel(private val cardRepository : CardRepository):ViewModel() {
|
|
||||||
val getAllCards: Flow<PagingData<Card>> = cardRepository.getAllCards().cachedIn(viewModelScope)
|
|
||||||
|
|
||||||
fun getCardById(id: Int): Flow<Card?> = cardRepository.getCardById(id)
|
|
||||||
|
|
||||||
fun getCardByUserId(userId: Int): Flow<PagingData<Card>> = cardRepository.getCardsByUserId(userId).cachedIn(viewModelScope)
|
|
||||||
|
|
||||||
fun insertCard(card: Card) = viewModelScope.launch { cardRepository.insertCard(card) }
|
|
||||||
|
|
||||||
fun updateCard(card: Card) = viewModelScope.launch { cardRepository.updateCard(card) }
|
|
||||||
|
|
||||||
fun deleteCard(card: Card) = viewModelScope.launch { cardRepository.deleteCard(card) }
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.myapplication.database.viewmodels
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModelProvider
|
|
||||||
import androidx.lifecycle.viewmodel.CreationExtras
|
|
||||||
import androidx.lifecycle.viewmodel.initializer
|
|
||||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
|
||||||
import com.example.myapplication.MobileApp
|
|
||||||
|
|
||||||
object MobileAppViewModelProvider {
|
|
||||||
val Factory = viewModelFactory {
|
|
||||||
initializer {
|
|
||||||
CardViewModel(app().container.cardRepository)
|
|
||||||
}
|
|
||||||
initializer {
|
|
||||||
UserViewModel(app().container.userRepository)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun CreationExtras.app(): MobileApp =
|
|
||||||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MobileApp)
|
|
@ -1,51 +0,0 @@
|
|||||||
package com.example.myapplication.database.viewmodels
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.example.myapplication.GlobalUser
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.database.repositories.UserRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
class UserViewModel(private val userRepository: UserRepository): ViewModel() {
|
|
||||||
val getAllUsers = userRepository.getAllUsers()
|
|
||||||
|
|
||||||
suspend fun getUser(id: Int): Flow<User?> = userRepository.getUserById(id)
|
|
||||||
|
|
||||||
fun updateUser(user: User) = viewModelScope.launch {
|
|
||||||
userRepository.updateUser(user)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteUser(user: User) = viewModelScope.launch {
|
|
||||||
userRepository.deleteUser(user)
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getUserByLogin(login: String): User? = userRepository.getUserByLogin(login)
|
|
||||||
|
|
||||||
fun regUser(user: User) = viewModelScope.launch {
|
|
||||||
val globalUser = userRepository.getUserByLogin(user.login)
|
|
||||||
globalUser?.let {
|
|
||||||
return@launch
|
|
||||||
} ?: run {
|
|
||||||
if(user.password.isEmpty()){
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
userRepository.insertUser(user)
|
|
||||||
GlobalUser.getInstance().setUser(userRepository.getUserByLogin(user.login))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun authUser(user: User) = viewModelScope.launch {
|
|
||||||
val globalUser = userRepository.getUserByLogin(user.login)
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,5 @@
|
|||||||
package com.example.myapplication.screens
|
package com.example.myapplication.screens
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
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.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
@ -13,57 +12,23 @@ import androidx.compose.foundation.layout.size
|
|||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
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.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.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.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.myapplication.GlobalUser
|
|
||||||
import com.example.myapplication.R
|
import com.example.myapplication.R
|
||||||
import com.example.myapplication.components.ActiveButton
|
|
||||||
import com.example.myapplication.components.LoginField
|
import com.example.myapplication.components.LoginField
|
||||||
import com.example.myapplication.components.PasswordField
|
import com.example.myapplication.components.PasswordField
|
||||||
import com.example.myapplication.components.PasswordInputField
|
|
||||||
import com.example.myapplication.components.PlaceholderInputField
|
|
||||||
import com.example.myapplication.components.PlaceholderInputFieldAuth
|
|
||||||
import com.example.myapplication.components.navButton
|
import com.example.myapplication.components.navButton
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
|
||||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Authorization(navController: NavHostController,
|
fun Authorization(navController: NavHostController){
|
||||||
userViewModel: UserViewModel = viewModel( factory = MobileAppViewModelProvider.Factory )){
|
|
||||||
|
|
||||||
val message = remember { mutableStateOf("") }
|
|
||||||
val isAuthorizated = remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
if(GlobalUser.getInstance().getUser() != null && !isAuthorizated.value) {
|
|
||||||
isAuthorizated.value = !isAuthorizated.value
|
|
||||||
message.value = ""
|
|
||||||
navController.navigate("mainScreen")
|
|
||||||
}
|
|
||||||
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
Column (
|
Column (
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
@ -80,41 +45,16 @@ fun Authorization(navController: NavHostController,
|
|||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(8.dp),
|
.padding(8.dp),
|
||||||
contentAlignment = Alignment.Center) {
|
contentAlignment = Alignment.Center) {
|
||||||
Column {
|
Text("Продажа автомобилей",
|
||||||
Text("Продажа автомобилей",
|
fontWeight = FontWeight.Bold,
|
||||||
fontWeight = FontWeight.Bold,
|
fontSize = 20.sp
|
||||||
fontSize = 20.sp
|
)
|
||||||
)
|
|
||||||
if (message.value.isNotEmpty()) {
|
|
||||||
Text(
|
|
||||||
message.value,
|
|
||||||
fontWeight = FontWeight.Bold,
|
|
||||||
fontSize = 20.sp,
|
|
||||||
color = Color.Red
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = { newlogin ->
|
LoginField(text = "Логин")
|
||||||
login.value = newlogin
|
PasswordField(text = "Пароль")
|
||||||
})
|
navButton(navController = navController, destination = "mainScreen", label = "Вход",
|
||||||
PasswordInputField(label = "Пароль", onPasswordChanged = { newpassword ->
|
backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||||
password.value = newpassword
|
|
||||||
})
|
|
||||||
|
|
||||||
ActiveButton(label = "Вход", backgroundColor = SkyBlue,
|
|
||||||
textColor = Color.Black, onClickAction = {
|
|
||||||
if (login.value.isNotEmpty() && password.value.isNotEmpty()) {
|
|
||||||
userViewModel.authUser(
|
|
||||||
User(
|
|
||||||
login = login.value,
|
|
||||||
password = password.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
message.value = "Неправильный логин или пароль."
|
|
||||||
}
|
|
||||||
})
|
|
||||||
navButton(navController = navController, destination = "registration", label = "Регистрация",
|
navButton(navController = navController, destination = "registration", label = "Регистрация",
|
||||||
backgroundColor = SkyBlue, textColor = Color.Black)
|
backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.example.myapplication.screens
|
||||||
|
|
||||||
|
import android.view.Display.Mode
|
||||||
|
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.material3.Button
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import androidx.navigation.NavHostController
|
||||||
|
import com.example.myapplication.components.LoginField
|
||||||
|
import com.example.myapplication.components.navBar
|
||||||
|
import com.example.myapplication.components.navButton
|
||||||
|
import com.example.myapplication.ui.theme.SkyBlue
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun CreateCard(navController: NavHostController) {
|
||||||
|
Column {
|
||||||
|
Box (modifier = Modifier.fillMaxHeight(0.9f)) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
|
) {
|
||||||
|
Button(onClick = { /*TODO*/ }) {
|
||||||
|
Text("Добавить картинку")
|
||||||
|
}
|
||||||
|
LoginField(text = "Название/Марка")
|
||||||
|
LoginField(text = "Пробег")
|
||||||
|
LoginField(text = "Расположение")
|
||||||
|
LoginField(text = "Цена")
|
||||||
|
Button(onClick = { /*TODO*/ }) {
|
||||||
|
Text("Создать объявление")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Column(modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.background(SkyBlue),
|
||||||
|
verticalArrangement = Arrangement.Center) {
|
||||||
|
navBar(navController = navController)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,220 +1,50 @@
|
|||||||
package com.example.myapplication.screens
|
package com.example.myapplication.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.view.Display.Mode
|
import android.view.Display.Mode
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.gestures.scrollable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
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.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
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.size
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
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.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.asImageBitmap
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.myapplication.GlobalUser
|
|
||||||
import com.example.myapplication.R
|
|
||||||
import com.example.myapplication.components.ActiveButton
|
|
||||||
import com.example.myapplication.components.LoginField
|
import com.example.myapplication.components.LoginField
|
||||||
import com.example.myapplication.components.PlaceholderInputField
|
|
||||||
import com.example.myapplication.components.navBar
|
import com.example.myapplication.components.navBar
|
||||||
import com.example.myapplication.components.navButton
|
import com.example.myapplication.components.navButton
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.database.viewmodels.CardViewModel
|
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
|
||||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
import com.example.myapplication.ui.theme.SkyBlue
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import kotlin.math.log
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditCardScreen(navController: NavHostController,
|
fun EditCard(navController: NavHostController) {
|
||||||
cardViewModel: CardViewModel = viewModel( factory = MobileAppViewModelProvider.Factory),
|
Column {
|
||||||
cardId: Int? = null) {
|
Box (modifier = Modifier.fillMaxHeight(0.9f)) {
|
||||||
val context = LocalContext.current
|
Column(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
val image = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.ferrari_laferrari_car2)) }
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
val name = remember { mutableStateOf("") }
|
) {
|
||||||
val location = remember { mutableStateOf("") }
|
Button(onClick = { /*TODO*/ }) {
|
||||||
val price = remember { mutableStateOf(0) }
|
Text("Добавить картинку")
|
||||||
val mileage = remember { mutableStateOf(0) }
|
|
||||||
|
|
||||||
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) {
|
|
||||||
image.value = MediaStore.Images
|
|
||||||
.Media.getBitmap(context.contentResolver, imageData.value)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
val source = ImageDecoder
|
|
||||||
.createSource(context.contentResolver, imageData.value!!)
|
|
||||||
image.value = ImageDecoder.decodeBitmap(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
cardId?.let {
|
|
||||||
cardViewModel.getCardById(cardId).collect {
|
|
||||||
if (it != null) {
|
|
||||||
image.value = it.image
|
|
||||||
}
|
}
|
||||||
if (it != null) {
|
LoginField(text = "Название/Марка")
|
||||||
name.value = it.name
|
LoginField(text = "Пробег")
|
||||||
}
|
LoginField(text = "Расположение")
|
||||||
if (it != null) {
|
LoginField(text = "Цена")
|
||||||
location.value = it.location
|
Button(onClick = { /*TODO*/ }) {
|
||||||
}
|
Text("Изменить объявление")
|
||||||
if (it != null) {
|
|
||||||
mileage.value = it.mileage
|
|
||||||
}
|
|
||||||
if (it != null) {
|
|
||||||
price.value = it.price
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Column(modifier = Modifier
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(bottom = 8.dp)
|
.background(SkyBlue),
|
||||||
.verticalScroll(rememberScrollState()),
|
verticalArrangement = Arrangement.Center) {
|
||||||
verticalArrangement = Arrangement.Bottom,
|
navBar(navController = navController)
|
||||||
|
|
||||||
) {
|
|
||||||
Image(
|
|
||||||
bitmap = image.value.asImageBitmap(),
|
|
||||||
contentDescription = "editplaceholder",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(384.dp)
|
|
||||||
.padding(8.dp)
|
|
||||||
.align(Alignment.CenterHorizontally))
|
|
||||||
ActiveButton(label = "Выбрать обложку", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
|
||||||
launcher.launch("image/*")
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Название", isSingleLine = true,
|
|
||||||
startValue = name.value, onTextChanged = { newName ->
|
|
||||||
name.value = newName
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Расположение", isSingleLine = true,
|
|
||||||
startValue = location.value, onTextChanged = { newLocation ->
|
|
||||||
location.value = newLocation
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Пробег", isSingleLine = true,
|
|
||||||
startValue = mileage.value.toString(), onTextChanged = { newMileage ->
|
|
||||||
mileage.value = newMileage.toIntOrNull() ?: 500
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Цена", isSingleLine = true,
|
|
||||||
startValue = price.value.toString(), onTextChanged = { newPrice ->
|
|
||||||
price.value = newPrice.toIntOrNull() ?: 10000
|
|
||||||
})
|
|
||||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
|
||||||
cardId?.let {
|
|
||||||
cardViewModel.updateCard(
|
|
||||||
Card(
|
|
||||||
id = cardId,
|
|
||||||
image = image.value,
|
|
||||||
name = name.value,
|
|
||||||
location = location.value,
|
|
||||||
mileage = mileage.value,
|
|
||||||
price = price.value,
|
|
||||||
userId = GlobalUser.getInstance().getUser()?.id ?: 1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} ?: run {
|
|
||||||
cardViewModel.insertCard(
|
|
||||||
Card(
|
|
||||||
image = image.value,
|
|
||||||
name = name.value,
|
|
||||||
location = location.value,
|
|
||||||
mileage = mileage.value,
|
|
||||||
price = price.value,
|
|
||||||
userId = GlobalUser.getInstance().getUser()?.id ?: 1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
navController.navigate("mainScreen")
|
|
||||||
})
|
|
||||||
navButton(navController = navController, destination = "mainScreen", label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun EditUserScreen(navController: NavHostController,
|
|
||||||
userViewModel: UserViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory)){
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
var userId = remember { mutableStateOf(0) }
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
GlobalUser.getInstance().getUser()?.let { user ->
|
|
||||||
userId.value = user!!.id!!
|
|
||||||
login.value = user!!.login
|
|
||||||
password.value = user!!.password
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(bottom = 8.dp),
|
|
||||||
verticalArrangement = Arrangement.Bottom
|
|
||||||
) {
|
|
||||||
PlaceholderInputField(label = "Логин", isSingleLine = true,
|
|
||||||
startValue = login.value, onTextChanged = { newName ->
|
|
||||||
login.value = newName
|
|
||||||
})
|
|
||||||
PlaceholderInputField(label = "Пароль", isSingleLine = true,
|
|
||||||
startValue = password.value, onTextChanged = { newPassword ->
|
|
||||||
password.value = newPassword
|
|
||||||
})
|
|
||||||
ActiveButton(label = "Сохранить", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction = {
|
|
||||||
userViewModel.updateUser(
|
|
||||||
User(
|
|
||||||
id = userId.value,
|
|
||||||
login = login.value,
|
|
||||||
password = password.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
navController.navigate("userSettings")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,30 +1,9 @@
|
|||||||
package com.example.myapplication.screens
|
package com.example.myapplication.screens
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
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.graphics.Color
|
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.layout.ContentScale
|
|
||||||
import androidx.compose.ui.res.painterResource
|
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import com.example.myapplication.components.navBar
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
@ -32,265 +11,132 @@ import androidx.compose.foundation.layout.Row
|
|||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
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.lazy.grid.GridCells
|
|
||||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material3.AlertDialog
|
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
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.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.DisposableEffect
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.paging.compose.collectAsLazyPagingItems
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.paging.compose.itemKey
|
import androidx.compose.ui.graphics.Color
|
||||||
import com.example.myapplication.GlobalUser
|
import androidx.compose.ui.graphics.Shape
|
||||||
import com.example.myapplication.R
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
import androidx.compose.ui.res.painterResource
|
||||||
import com.example.myapplication.database.viewmodels.CardViewModel
|
import androidx.compose.ui.text.font.FontFamily
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
import androidx.compose.ui.unit.dp
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import androidx.navigation.NavHostController
|
||||||
import kotlinx.coroutines.Dispatchers
|
import com.example.myapplication.components.navBar
|
||||||
import kotlinx.coroutines.flow.collect
|
import com.example.myapplication.ui.theme.SkyBlue
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(navController: NavHostController,
|
fun MainScreen(navController: NavHostController) {
|
||||||
cardViewModel: CardViewModel = viewModel(factory = MobileAppViewModelProvider.Factory),
|
|
||||||
userViewModel: UserViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
|
||||||
val cards = cardViewModel.getAllCards.collectAsLazyPagingItems()
|
|
||||||
|
|
||||||
Column {
|
Column {
|
||||||
Box(modifier = Modifier
|
Box(modifier = Modifier
|
||||||
.padding(horizontal = 10.dp)
|
.padding(horizontal = 10.dp)
|
||||||
.fillMaxHeight(0.9f)) {
|
.fillMaxHeight(0.9f)) {
|
||||||
Column()
|
Column(
|
||||||
{
|
modifier = Modifier.verticalScroll(rememberScrollState())
|
||||||
LazyVerticalGrid(
|
) {
|
||||||
columns = GridCells.Fixed(1)
|
ListItem(
|
||||||
) {
|
name = "Ламборджини Авентадор 2010",
|
||||||
item {
|
mileage = 4765,
|
||||||
addNewListItem(navController, "editcard")
|
location = "Россия, г. Ульяновск",
|
||||||
}
|
price = 5000000,
|
||||||
items(
|
imageResourceId = com.example.myapplication.R.drawable.lambo_car1,
|
||||||
count = cards.itemCount,
|
navController = navController
|
||||||
key = cards.itemKey { item -> item.id!! }
|
)
|
||||||
) { index: Int ->
|
ListItem(
|
||||||
val card: Card? = cards[index]
|
name = "Оранжевый Москвич 1983",
|
||||||
if (card != null) {
|
mileage = 522011,
|
||||||
CardListItem(item = card, navController = navController, userViewModel)
|
location = "Россия, г. Москва",
|
||||||
}
|
price = 125000,
|
||||||
}
|
imageResourceId = com.example.myapplication.R.drawable.moscowich_car3,
|
||||||
}
|
navController = navController
|
||||||
|
)
|
||||||
|
ListItem(
|
||||||
|
name = "Феррари Лаферрари 2013",
|
||||||
|
mileage = 17698,
|
||||||
|
location = "Италия, г. Неаполь",
|
||||||
|
price = 1249990,
|
||||||
|
imageResourceId = com.example.myapplication.R.drawable.ferrari_laferrari_car2,
|
||||||
|
navController = navController
|
||||||
|
)
|
||||||
|
ListItem(
|
||||||
|
name = "Оранжевый Москвич 1983",
|
||||||
|
mileage = 522011,
|
||||||
|
location = "Россия, г. Москва",
|
||||||
|
price = 125000,
|
||||||
|
imageResourceId = com.example.myapplication.R.drawable.moscowich_car3,
|
||||||
|
navController = navController
|
||||||
|
)
|
||||||
|
ListItem(
|
||||||
|
name = "Феррари Лаферрари 2013",
|
||||||
|
mileage = 17698,
|
||||||
|
location = "Италия, г. Неаполь",
|
||||||
|
price = 1249990,
|
||||||
|
imageResourceId = com.example.myapplication.R.drawable.ferrari_laferrari_car2,
|
||||||
|
navController = navController
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Column(
|
Column(modifier = Modifier
|
||||||
modifier = Modifier
|
.fillMaxSize()
|
||||||
.fillMaxWidth()
|
.background(SkyBlue),
|
||||||
.background(SkyBlue)
|
verticalArrangement = Arrangement.Center) {
|
||||||
.weight(1f),
|
|
||||||
verticalArrangement = Arrangement.Center
|
|
||||||
) {
|
|
||||||
navBar(navController = navController)
|
navBar(navController = navController)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T> List<*>.isListOf(): Boolean {
|
|
||||||
return isNotEmpty() && all { it is T }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CardListItem(item: Card, navController: NavHostController, userViewModel: UserViewModel){
|
private fun ListItem(name: String,
|
||||||
val context = LocalContext.current
|
mileage: Int,
|
||||||
|
location: String,
|
||||||
val userLogin = remember {
|
price: Int,
|
||||||
mutableStateOf("")
|
imageResourceId: Int,
|
||||||
}
|
navController: NavHostController) {
|
||||||
|
var borderColor = remember { mutableStateOf(Color.Gray) }
|
||||||
val showDialog = remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
val delete = remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit)
|
|
||||||
{
|
|
||||||
userViewModel.getUser(item.userId).collect{
|
|
||||||
if (it != null) {
|
|
||||||
userLogin.value = it.login
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Card(
|
Card(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(10.dp)
|
.padding(10.dp)
|
||||||
.border(2.dp, Color.Gray, RoundedCornerShape(15.dp)),
|
.border(2.dp, borderColor.value, RoundedCornerShape(15.dp))
|
||||||
|
.clickable { borderColor.value = Color.LightGray },
|
||||||
shape = RoundedCornerShape(15.dp),
|
shape = RoundedCornerShape(15.dp),
|
||||||
elevation = CardDefaults.cardElevation(defaultElevation = 5.dp)
|
elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
|
||||||
) {
|
){
|
||||||
Column(
|
Box() {
|
||||||
modifier = Modifier.fillMaxSize()
|
Row(verticalAlignment = Alignment.CenterVertically){
|
||||||
) {
|
Image(
|
||||||
Row(
|
painter = painterResource(id = imageResourceId),
|
||||||
verticalAlignment = Alignment.Top
|
contentDescription = "image",
|
||||||
){
|
|
||||||
Image(bitmap = item.image.asImageBitmap(),
|
|
||||||
contentDescription = item.name,
|
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
modifier = Modifier
|
modifier = Modifier.size(128.dp))
|
||||||
.width(128.dp)
|
|
||||||
.height(256.dp))
|
|
||||||
Column (
|
Column (
|
||||||
modifier = Modifier.padding(8.dp)
|
modifier = Modifier.padding(5.dp)) {
|
||||||
){
|
Text(text = name, fontFamily = FontFamily.SansSerif, fontWeight = FontWeight.Bold)
|
||||||
Text(
|
Text(text = "Пробег: " + mileage)
|
||||||
text = "Название: ${item.name} \nРасположение: ${item.location} \nПробег: ${item.mileage} \nЦена: ${item.price} \nПродавец: ${userLogin.value}",
|
Text(text = "Расположение: " + location)
|
||||||
fontSize = 20.sp,
|
Text(text = "Цена: " + price)
|
||||||
fontWeight = FontWeight.Bold
|
Button(onClick = { navController.navigate("editCard") }) {
|
||||||
)
|
Text("Изменить")
|
||||||
DataListItemButton("Изменить", Color.LightGray, Color.White, onClickAction = {
|
}
|
||||||
navController.navigate("editcard/${item.id}")
|
Button(onClick = { }) {
|
||||||
})
|
Text("Удалить")
|
||||||
DataListItemButton("Удалить", Color.Black, Color.White, onClickAction = {
|
}
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(showDialog.value) {
|
|
||||||
DialogWindow(label = "Подтверждение",
|
|
||||||
message = "Вы уверены что хотите удалить запись?", onConfirmAction = {
|
|
||||||
delete.value = !delete.value
|
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
}, onDismissAction = {
|
|
||||||
showDialog.value = !showDialog.value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if(delete.value) {
|
|
||||||
LaunchedEffect(Unit){
|
|
||||||
withContext(Dispatchers.IO){
|
|
||||||
MobileAppDataBase.getInstance(context).cardDao().delete(item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete.value = !delete.value
|
|
||||||
navController.navigate("mainScreen")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun DataListItemButton(label: String, backgroundColor: Color, textColor: Color, onClickAction: () -> Unit){
|
|
||||||
Button(
|
|
||||||
onClick = onClickAction,
|
|
||||||
modifier = Modifier.requiredHeight(64.dp),
|
|
||||||
shape = RoundedCornerShape(10.dp),
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = backgroundColor
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
text = label,
|
|
||||||
color = textColor,
|
|
||||||
fontSize = 18.sp,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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 = Color.Green
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(5.dp)
|
|
||||||
) {
|
|
||||||
Text(text = "Подтвердить",
|
|
||||||
color = Color.White)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
dismissButton = {
|
|
||||||
Button(
|
|
||||||
onClick = onDismissAction,
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = Color.Red
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(5.dp)
|
|
||||||
) {
|
|
||||||
Text(text = "Отмена",
|
|
||||||
color = Color.Black)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun addNewListItem(navController: NavHostController, destination: String){
|
|
||||||
Card(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(start = 18.dp, top = 8.dp, end = 18.dp, bottom = 8.dp)
|
|
||||||
.clickable {
|
|
||||||
navController.navigate(destination)
|
|
||||||
},
|
|
||||||
shape = RoundedCornerShape(15.dp),
|
|
||||||
colors = CardDefaults.cardColors(
|
|
||||||
containerColor = Color.White
|
|
||||||
),
|
|
||||||
elevation = CardDefaults.cardElevation(
|
|
||||||
defaultElevation = 8.dp
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
modifier = Modifier.fillMaxSize()
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
verticalAlignment = Alignment.CenterVertically
|
|
||||||
){
|
|
||||||
Image(painter = painterResource(id = R.drawable.plus),
|
|
||||||
contentDescription = "additem",
|
|
||||||
contentScale = ContentScale.Crop,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(96.dp)
|
|
||||||
.padding(8.dp))
|
|
||||||
Text(
|
|
||||||
text = "Добавить",
|
|
||||||
fontSize = 28.sp,
|
|
||||||
fontWeight = FontWeight.Bold,
|
|
||||||
modifier = Modifier.padding(start = 32.dp))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,68 +4,31 @@ import androidx.compose.foundation.layout.Column
|
|||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
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
|
||||||
import androidx.compose.ui.layout.ModifierInfo
|
import androidx.compose.ui.layout.ModifierInfo
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.myapplication.GlobalUser
|
|
||||||
import com.example.myapplication.components.ActiveButton
|
|
||||||
import com.example.myapplication.components.LoginField
|
import com.example.myapplication.components.LoginField
|
||||||
import com.example.myapplication.components.PasswordField
|
import com.example.myapplication.components.PasswordField
|
||||||
import com.example.myapplication.components.PasswordInputField
|
|
||||||
import com.example.myapplication.components.PlaceholderInputField
|
|
||||||
import com.example.myapplication.components.PlaceholderInputFieldAuth
|
|
||||||
import com.example.myapplication.components.navButton
|
import com.example.myapplication.components.navButton
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
|
||||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Registration(navController: NavHostController,
|
fun Registration(navController: NavHostController) {
|
||||||
userViewModel: UserViewModel = viewModel(
|
|
||||||
factory = MobileAppViewModelProvider.Factory)) {
|
|
||||||
val isRegistrated = remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
if(GlobalUser.getInstance().getUser() != null && !isRegistrated.value) {
|
|
||||||
isRegistrated.value = !isRegistrated.value
|
|
||||||
navController.navigate("mainScreen")
|
|
||||||
}
|
|
||||||
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
val repeatepassword = remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
Column (modifier = Modifier.fillMaxWidth(),
|
Column (modifier = Modifier.fillMaxWidth(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally) {
|
horizontalAlignment = Alignment.CenterHorizontally) {
|
||||||
PlaceholderInputFieldAuth(label = "Логин", isSingleLine = true, onTextChanged = {newlogin ->
|
Text(text = "Привет!",
|
||||||
login.value = newlogin
|
fontSize = 20.sp,
|
||||||
})
|
fontWeight = FontWeight.Bold)
|
||||||
PasswordInputField(label = "Пароль", onPasswordChanged = {newpassword ->
|
Text(text = "Зарегистрируйся, чтобы начать!",
|
||||||
password.value = newpassword
|
fontSize = 20.sp,
|
||||||
})
|
fontWeight = FontWeight.Bold)
|
||||||
PasswordInputField(label = "Повторите пароль", onPasswordChanged = {newpassword ->
|
LoginField(text = "Логин")
|
||||||
repeatepassword.value = newpassword
|
PasswordField(text = "Пароль")
|
||||||
})
|
PasswordField(text = "Повторите пароль")
|
||||||
ActiveButton(label = "Зарегистрироваться", backgroundColor = SkyBlue,
|
navButton(navController = navController, destination = "authorization", label = "Зарегистрироваться", backgroundColor = Color.Cyan, textColor = Color.Black)
|
||||||
textColor = Color.Black, onClickAction = {
|
|
||||||
if (password.value == repeatepassword.value){
|
|
||||||
userViewModel.regUser(
|
|
||||||
User(
|
|
||||||
login = login.value,
|
|
||||||
password = password.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
navButton(navController = navController, destination = "authorization",
|
|
||||||
label = "Назад", backgroundColor = SkyBlue, textColor = Color.Black)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,64 +12,26 @@ import androidx.compose.foundation.layout.size
|
|||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
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.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
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.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.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.myapplication.GlobalUser
|
|
||||||
import com.example.myapplication.MobileApp
|
|
||||||
import com.example.myapplication.R
|
import com.example.myapplication.R
|
||||||
import com.example.myapplication.components.ActiveButton
|
|
||||||
import com.example.myapplication.components.LoginField
|
import com.example.myapplication.components.LoginField
|
||||||
import com.example.myapplication.components.PlaceholderInputField
|
|
||||||
import com.example.myapplication.components.navBar
|
import com.example.myapplication.components.navBar
|
||||||
import com.example.myapplication.database.MobileAppDataBase
|
|
||||||
import com.example.myapplication.database.entities.Card
|
|
||||||
import com.example.myapplication.database.entities.User
|
|
||||||
import com.example.myapplication.database.viewmodels.MobileAppViewModelProvider
|
|
||||||
import com.example.myapplication.database.viewmodels.UserViewModel
|
|
||||||
import com.example.myapplication.ui.theme.SkyBlue
|
import com.example.myapplication.ui.theme.SkyBlue
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun UserSettings(navController: NavHostController,
|
fun UserSettings(navController: NavHostController) {
|
||||||
userViewModel: UserViewModel = viewModel(factory = MobileAppViewModelProvider.Factory)) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val login = remember { mutableStateOf("") }
|
|
||||||
val password = remember { mutableStateOf("") }
|
|
||||||
val userId = GlobalUser.getInstance().getUser()?.id?: 1
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
userId?.let {
|
|
||||||
userViewModel.getUser(userId).collect {
|
|
||||||
if (it != null) {
|
|
||||||
login.value = it.login
|
|
||||||
}
|
|
||||||
if (it != null) {
|
|
||||||
password.value = it.password
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Column {
|
Column {
|
||||||
Box (modifier = Modifier.fillMaxHeight(0.9f)) {
|
Box (modifier = Modifier.fillMaxHeight(0.9f)) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
) {
|
||||||
Image(
|
Image(
|
||||||
painter = painterResource(id = R.drawable.user),
|
painter = painterResource(id = R.drawable.user),
|
||||||
contentDescription = "login",
|
contentDescription = "login",
|
||||||
@ -77,22 +39,10 @@ fun UserSettings(navController: NavHostController,
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(300.dp)
|
.size(300.dp)
|
||||||
)
|
)
|
||||||
PlaceholderInputField(label = "Логин пользователя: ${login.value}", isSingleLine = true, onTextChanged = {newlogin ->
|
LoginField(text = "Логин пользователя")
|
||||||
login.value = newlogin})
|
LoginField(text = "Пароль пользователя")
|
||||||
PlaceholderInputField(label = "Пароль пользователя: ${password.value}", isSingleLine = true, onTextChanged = {newpassword ->
|
LoginField(text = "Количество объявлений пользователя")
|
||||||
password.value = newpassword})
|
Button(onClick = {}) { Text("Посмотреть мои объявления") }
|
||||||
ActiveButton(label = "Сохранить изменения", backgroundColor = SkyBlue, textColor = Color.Black, onClickAction =
|
|
||||||
{
|
|
||||||
userViewModel.updateUser(User(
|
|
||||||
id = userId,
|
|
||||||
login = login.value,
|
|
||||||
password = password.value
|
|
||||||
))
|
|
||||||
})
|
|
||||||
ActiveButton(label = "Выход из аккаунта", backgroundColor = Color.Red, textColor = Color.White, onClickAction = {
|
|
||||||
GlobalUser.getInstance().setUser(null)
|
|
||||||
navController.navigate("authorization")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Column(modifier = Modifier
|
Column(modifier = Modifier
|
||||||
@ -102,4 +52,5 @@ fun UserSettings(navController: NavHostController,
|
|||||||
navBar(navController = navController)
|
navBar(navController = navController)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 89 KiB |
Binary file not shown.
Before Width: | Height: | Size: 78 KiB |
Binary file not shown.
Before Width: | Height: | Size: 22 KiB |
@ -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.
Loading…
x
Reference in New Issue
Block a user