Начало положено

This commit is contained in:
Артём Батылкин 2023-11-13 18:49:43 +04:00
parent 83aabb521f
commit d9a2c3aefe
21 changed files with 348 additions and 377 deletions

View File

@ -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.10" /> <option name="version" value="1.8.20" />
</component> </component>
</project> </project>

View File

@ -1,9 +1,6 @@
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component> </component>
</project> </project>

View File

@ -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="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>
</project> </project>

View File

@ -1,16 +1,17 @@
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.todolisttask" namespace = "com.example.todolisttask"
compileSdk = 34 compileSdk = 33
defaultConfig { defaultConfig {
applicationId = "com.example.todolisttask" applicationId = "com.example.todolisttask"
minSdk = 24 minSdk = 24
targetSdk = 34 targetSdk = 33
versionCode = 1 versionCode = 1
versionName = "1.0" versionName = "1.0"
@ -30,17 +31,17 @@ android {
} }
} }
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_17
} }
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "17"
} }
buildFeatures { buildFeatures {
compose = true compose = true
} }
composeOptions { composeOptions {
kotlinCompilerExtensionVersion = "1.4.3" kotlinCompilerExtensionVersion = "1.4.5"
} }
packaging { packaging {
resources { resources {
@ -48,11 +49,16 @@ android {
} }
} }
} }
kotlin {
jvmToolchain(17)
}
dependencies { dependencies {
implementation ("io.coil-kt:coil-compose:1.4.0") // Core
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")
// UI
implementation("androidx.activity:activity-compose:1.7.2") 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.navigation:navigation-compose:2.6.0") implementation("androidx.navigation:navigation-compose:2.6.0")
@ -60,9 +66,16 @@ dependencies {
implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3") implementation("androidx.compose.material3:material3")
implementation("com.google.android.engage:engage-core:1.3.0")
implementation("androidx.appcompat:appcompat:1.6.1") // Room
implementation("androidx.compose.ui:ui-graphics-android:1.5.3") 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")
// Tests
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

View File

@ -12,7 +12,6 @@ import androidx.compose.ui.Modifier
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.navigation.NavController import androidx.navigation.NavController
import com.example.todolisttask.models.model.AuthViewModel
import com.example.todolisttask.models.model.Task import com.example.todolisttask.models.model.Task
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@ -49,19 +48,19 @@ fun CreateTask(navController: NavController, onSaveClick: (Task) -> Unit) {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( // Button(
onClick = { // onClick = {
if (taskName.isNotEmpty()) { // if (taskName.isNotEmpty()) {
val newTask = Task(0, taskName, taskDescription) // val newTask = Task(0, taskName, taskDescription)
onSaveClick(newTask) // onSaveClick(newTask)
taskName = "" // taskName = ""
//
navController.popBackStack() // navController.popBackStack()
} // }
}, // },
modifier = Modifier.fillMaxWidth() // modifier = Modifier.fillMaxWidth()
) { // ) {
Text("Сохранить") // Text("Сохранить")
} // }
} }
} }

View File

@ -6,16 +6,12 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavController import androidx.navigation.NavController
import com.example.todolisttask.composeui.navigation.Screen
import com.example.todolisttask.models.composeui.TaskList
import com.example.todolisttask.models.model.AuthViewModel import com.example.todolisttask.models.model.AuthViewModel
import com.example.todolisttask.models.model.TaskViewModel import com.example.todolisttask.models.model.TaskViewModel
import com.example.todolisttask.models.model.UserViewModel import com.example.todolisttask.models.model.UserViewModel

View File

@ -1,36 +1,23 @@
package com.example.todolisttask.composeui package com.example.todolisttask.composeui
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
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
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextField
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.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavController import androidx.navigation.NavController
import coil.compose.rememberImagePainter
import com.example.todolisttask.composeui.navigation.Screen import com.example.todolisttask.composeui.navigation.Screen
import com.example.todolisttask.models.composeui.TaskList
import com.example.todolisttask.models.model.AuthViewModel import com.example.todolisttask.models.model.AuthViewModel
import com.example.todolisttask.models.model.TaskViewModel import com.example.todolisttask.models.model.TaskViewModel
import com.example.todolisttask.models.model.User
import com.example.todolisttask.models.model.UserViewModel import com.example.todolisttask.models.model.UserViewModel
import com.example.todolisttask.models.composeui.TaskList
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("RememberReturnType") @SuppressLint("RememberReturnType")

View File

@ -28,7 +28,6 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavController import androidx.navigation.NavController
import coil.compose.rememberImagePainter
import com.example.todolisttask.composeui.navigation.Screen import com.example.todolisttask.composeui.navigation.Screen
import com.example.todolisttask.models.model.User import com.example.todolisttask.models.model.User
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow

View File

@ -4,9 +4,7 @@ package com.example.todolisttask.composeui.navigation
import CreateTask import CreateTask
import android.os.Build import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowBack
@ -23,7 +21,6 @@ import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
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.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination import androidx.navigation.NavDestination
@ -41,7 +38,6 @@ import com.example.pmuapp.composeui.Profile
import com.example.todolisttask.R import com.example.todolisttask.R
import com.example.todolisttask.composeui.EditTask import com.example.todolisttask.composeui.EditTask
import com.example.todolisttask.composeui.Favorite import com.example.todolisttask.composeui.Favorite
import com.example.todolisttask.models.composeui.TaskList
import com.example.todolisttask.models.model.AuthViewModel import com.example.todolisttask.models.model.AuthViewModel
import com.example.todolisttask.models.model.TaskViewModel import com.example.todolisttask.models.model.TaskViewModel
import com.example.todolisttask.models.model.UserViewModel import com.example.todolisttask.models.model.UserViewModel

View File

@ -1,143 +1,54 @@
package com.example.todolisttask.models.composeui package com.example.todolisttask.models.composeui
import androidx.compose.foundation.ExperimentalFoundationApi import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.FlowColumn
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.IconButton 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.ui.Alignment import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.text.TextStyle
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.navigation.NavController import androidx.navigation.NavController
import com.example.todolisttask.AppDatabase
import com.example.todolisttask.composeui.navigation.Screen import com.example.todolisttask.composeui.navigation.Screen
import com.example.todolisttask.models.model.AuthViewModel import com.example.todolisttask.models.model.Task
import com.example.todolisttask.models.model.TaskViewModel import kotlinx.coroutines.Dispatchers
import com.example.todolisttask.models.model.UserViewModel import kotlinx.coroutines.withContext
@OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class)
@Composable @Composable
fun TaskList(navController: NavController, authViewModel: AuthViewModel, taskViewModel: TaskViewModel, userViewModel: UserViewModel, userId: Int, showFavorites: Boolean = false) { fun TaskList(navController: NavController?) {
val context = LocalContext.current
val tasks = remember { mutableStateListOf<Task>() }
val currentUser = userViewModel.getUser(userId) LaunchedEffect(Unit) {
var tasks = if (showFavorites) currentUser.favoritetask else currentUser.taskId withContext(Dispatchers.IO) {
val taskList = AppDatabase.getInstance(context).taskDao().getAll()
LazyColumn( tasks.clear()
verticalArrangement = Arrangement.Center, tasks.addAll(taskList)
horizontalAlignment = Alignment.CenterHorizontally
) {
if (userId != authViewModel.currentUser?.id ?: -1) {
item {
Text(
text = currentUser?.name + " (" + currentUser.login + ")",
style = TextStyle(
fontSize = 24.sp,
fontWeight = FontWeight.Bold
),
modifier = Modifier.padding(16.dp)
)
}
}
item {
FlowColumn() {
tasks.forEach { task ->
val taskName = task.name
val taskDescription = task.description
FlowRow(
modifier = Modifier
.padding(8.dp)
.border(
1.dp,
Color.Black,
),
horizontalArrangement = Arrangement.SpaceBetween
) {
IconButton(
onClick = {
navController?.navigate(
Screen.EditTask.route.replace(
"{id}",
task.id.toString()
)
)
},
) {
Icon(
imageVector = Icons.Default.Edit,
contentDescription = "Изменить"
)
}
Column(
modifier = Modifier.weight(1f)
.padding(top = 4.dp)
) {
Text(
text = taskName,
style = TextStyle(
fontWeight = FontWeight.Bold
)
)
if (taskDescription.isNotBlank()) {
Text(
text = taskDescription
)
}
}
IconButton(
onClick = {
taskViewModel.deleteTask(task)
userViewModel.deleteTask(
currentUser?.id ?: 0,
task.id
)
authViewModel.currentUser =
userViewModel.getUser(
currentUser?.id ?: 0
)
}
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Удалить"
)
}
IconButton(
onClick = {
userViewModel.addFavoriteTaskToUser(userId, task)
}
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = "Добавить в избранное"
)
}
}
}
}
}
} }
} }
Column(Modifier.padding(all = 10.dp)) {
tasks.forEach { task ->
key(task.uid) {
val taskId = Screen.Home.route.replace("{id}", task.uid.toString())
Button(
modifier = Modifier
.fillMaxWidth()
.padding(all = 10.dp),
onClick = { navController?.navigate(taskId) }) {
Text("${task.name} ${task.description}")
}
}
}
}
}

View File

@ -0,0 +1,83 @@
package com.example.todolisttask.models.composeui
import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
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.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.todolisttask.AppDatabase
import com.example.todolisttask.R
import com.example.todolisttask.models.model.TaskWithUser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TaskView(id: Int) {
val context = LocalContext.current
val (taskWithUser, setTaskWithUser) = remember { mutableStateOf<TaskWithUser?>(null) }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
setTaskWithUser(AppDatabase.getInstance(context).userDao().getByUid(id))
}
}
taskWithUser?.let { task ->
Column(
Modifier
.fillMaxWidth()
.padding(all = 10.dp)
) {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = task.user?.name ?: "",
onValueChange = {},
readOnly = true,
label = {
Text(stringResource(id = R.string.user_name))
}
)
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = task.user?.login ?: "",
onValueChange = {},
readOnly = true,
label = {
Text(stringResource(id = R.string.user_login))
}
)
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = task.taskName ?: "",
onValueChange = {},
readOnly = true,
label = {
Text(stringResource(id = R.string.task_name))
}
)
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = task.user?.password ?: "",
onValueChange = {},
readOnly = true,
label = {
Text(stringResource(id = R.string.password_name))
}
)
}
}
}

View File

@ -1,36 +0,0 @@
package com.example.todolisttask.models.composeui
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.todolisttask.composeui.navigation.Screen
import com.example.todolisttask.models.model.UserViewModel
/*
@Composable
fun UserList(navController: NavController?,
userViewModel: UserViewModel
) {
val users = userViewModel.users.value
Column(Modifier.padding(all = 10.dp)) {
users.forEachIndexed() { index, user ->
val userId = Screen.UserViewModel.route.replace("{id}", index.toString())
Button(
modifier = Modifier
.fillMaxWidth()
.padding(all = 10.dp),
onClick = { navController?.navigate(userId) }) {
Text("${user.login}")
}
}
}
}*/

View File

@ -0,0 +1,23 @@
package com.example.todolisttask.models.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.todolisttask.models.model.Task
@Dao
interface TaskDao {
@Query("select * from tasks order by task_name collate nocase asc")
suspend fun getAll(): List<Task>
@Insert
suspend fun insert(group: Task)
@Update
suspend fun update(group: Task)
@Delete
suspend fun delete(group: Task)
}

View File

@ -0,0 +1,28 @@
package com.example.todolisttask.models.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.todolisttask.models.model.TaskWithUser
import com.example.todolisttask.models.model.User
import kotlinx.coroutines.flow.Flow
@Dao
interface UserDao {
@Query("select * from users order by name collate nocase asc")
fun getAll(): Flow<List<User>>
@Query("select * from users left join tasks on users.uid = tasks.user_id where users.uid = :uid")
suspend fun getByUid(uid: Int): TaskWithUser
@Insert
suspend fun insert(user: User)
@Update
suspend fun update(user: User)
@Delete
suspend fun delete(user: User)
}

View File

@ -0,0 +1,64 @@
package com.example.todolisttask
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.todolisttask.models.dao.TaskDao
import com.example.todolisttask.models.dao.UserDao
import com.example.todolisttask.models.model.Task
import com.example.todolisttask.models.model.User
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Database(entities = [User::class, Task::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
abstract fun taskDao(): TaskDao
companion object {
private const val DB_NAME: String = "pmy-db"
@Volatile
private var INSTANCE: AppDatabase? = null
private suspend fun populateDatabase() {
INSTANCE?.let { database ->
// Groups
val taskDao = database.taskDao()
val task1 = Task(1, "Test1", "TestDesk",1)
val task2 = Task(2, "Test2", "TestDesk",1)
val task3 = Task(3, "Test3", "TestDesk",1)
taskDao.insert(task1)
taskDao.insert(task2)
taskDao.insert(task3)
// Students
val userDao = database.userDao()
val user1 = User("Sergey", "User", "123456")
userDao.insert(user1)
}
}
fun getInstance(appContext: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DB_NAME
)
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch {
populateDatabase()
}
}
})
.build()
.also { INSTANCE = it }
}
}
}
}

View File

@ -1,10 +0,0 @@
package com.example.todolisttask.models.model;
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
class AuthViewModel : ViewModel() {
var currentUser: User? by mutableStateOf(null)
}

View File

@ -1,43 +1,39 @@
package com.example.todolisttask.models.model package com.example.todolisttask.models.model
import android.media.Image import androidx.room.ColumnInfo
import androidx.compose.runtime.MutableState import androidx.room.Entity
import androidx.compose.runtime.mutableStateOf import androidx.room.ForeignKey
import androidx.lifecycle.ViewModel import androidx.room.PrimaryKey
import java.io.Serializable
data class Task(
var id: Int,
val name: String,
val description: String
) : Serializable
class TaskViewModel : ViewModel() { @Entity(tableName = "tasks", foreignKeys = [
var tasks: MutableState<List<Task>> = mutableStateOf( ForeignKey(
emptyList() entity = User::class,
parentColumns = ["uid"],
childColumns = ["user_id"],
onDelete = ForeignKey.RESTRICT,
onUpdate = ForeignKey.RESTRICT
) )
])
fun createTask(newTask: Task): Task { data class Task(
newTask.id = tasks.value.size.toInt() @PrimaryKey(autoGenerate = true)
val updatedTasks = tasks.value.toMutableList() val uid: Int?,
updatedTasks.add(newTask) @ColumnInfo(name = "task_name")
tasks.value = updatedTasks val name: String,
return newTask @ColumnInfo(name = "task_description")
val description: String,
@ColumnInfo(name = "user_id", index = true)
val userId: Int
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Task
if (uid != other.uid) return false
return true
} }
fun deleteTask(task: Task) { override fun hashCode(): Int {
val updatedTasks = tasks.value.toMutableList() return uid ?: -1
updatedTasks.remove(task)
tasks.value = updatedTasks
}
fun updateTask(updatedTask: Task) {
val updatedTasks = tasks.value.toMutableList()
val index = updatedTasks.indexOfFirst { it.id == updatedTask.id }
if (index != -1) {
updatedTasks[index] = updatedTask
tasks.value = updatedTasks
}
} }
} }

View File

@ -0,0 +1,11 @@
package com.example.todolisttask.models.model
import androidx.room.ColumnInfo
import androidx.room.Embedded
data class TaskWithUser(
@Embedded
val user: User,
@ColumnInfo(name = "task_name")
val taskName: String
)

View File

@ -1,127 +1,38 @@
package com.example.todolisttask.models.model package com.example.todolisttask.models.model
import android.net.Uri import androidx.room.ColumnInfo
import androidx.compose.runtime.MutableState import androidx.room.Entity
import androidx.compose.runtime.mutableStateOf import androidx.room.Ignore
import androidx.lifecycle.ViewModel import androidx.room.PrimaryKey
import java.io.Serializable
@Entity(tableName = "users")
data class User( data class User(
val id: Int, @PrimaryKey(autoGenerate = true)
val uid: Int?,
@ColumnInfo(name = "name")
val name: String, val name: String,
@ColumnInfo(name = "login")
val login: String, val login: String,
val password: String, @ColumnInfo(name = "password")
val taskId: List<Task>, val password: String
val favoritetask: List<Task> ) {
) : Serializable
@Ignore
constructor(
name: String,
login: String,
password: String
) : this(null, name, login, password)
class UserViewModel : ViewModel() { override fun equals(other: Any?): Boolean {
var users: MutableState<List<User>> = mutableStateOf( if (this === other) return true
listOf( if (javaClass != other?.javaClass) return false
User(0, "Иван", "ivan", "111111", emptyList(), emptyList()), other as User
User(1, "Анна", "ann", "111111", emptyList(), emptyList()), if (uid != other.uid) return false
User(2, "Лиза", "liza", "111111", emptyList(), emptyList()) return true
)
)
fun getUsers(): List<User> {
return users.value
}
fun getUser(id:Int): User {
return users.value.get(id)
}
fun getPets(id:Int): List<Task> {
return users.value.get(id).taskId
}
// Function to add a pet to a user
fun addPetToUser(userId: Int, newPet: Task) {
val updatedUsers = users.value.toMutableList()
val user = updatedUsers.find { it.id == userId }
user?.let {
val updatedPets = user.taskId.toMutableList()
updatedPets.add(newPet)
val updatedUser = user.copy(taskId = updatedPets)
val userIndex = updatedUsers.indexOf(user)
updatedUsers[userIndex] = updatedUser
users.value = updatedUsers
}
}
fun deleteTask(userId: Int, taskIdToDelete: Int) {
val updatedUsers = users.value.toMutableList()
// Найдем пользователя по userId
val user = updatedUsers.find { it.id == userId }
user?.let { user ->
val updatedTasks = user.taskId.toMutableList()
// Найдем питомца по petId и удалим его из списка питомцев
val taskToDelete = updatedTasks.find { it.id == taskIdToDelete }
taskToDelete?.let { task ->
updatedTasks.remove(task)
}
// Обновим пользователя с обновленным списком питомцев
val updatedUser = user.copy(taskId = updatedTasks)
// Обновим список пользователей
val userIndex = updatedUsers.indexOf(user)
updatedUsers[userIndex] = updatedUser
users.value = updatedUsers
}
} }
fun addFavoriteTaskToUser(userId: Int, newFavoriteTask: Task) { override fun hashCode(): Int {
val updatedUsers = users.value.toMutableList() return uid ?: -1
val user = updatedUsers.find { it.id == userId }
user?.let {
val updatedFavoriteTasks = user.favoritetask.toMutableList()
updatedFavoriteTasks.add(newFavoriteTask)
val updatedUser = user.copy(favoritetask = updatedFavoriteTasks)
val userIndex = updatedUsers.indexOf(user)
updatedUsers[userIndex] = updatedUser
users.value = updatedUsers
}
} }
}
fun updateTaskOnUser(userId: Int, updatedTask: Task) {
val updatedUsers = users.value.toMutableList()
// Найдем пользователя по userId
val user = updatedUsers.find { it.id == userId }
user?.let { user ->
val updatedTasks = user.taskId.toMutableList()
// Найдем питомца по petId и обновим его
val taskToUpdate = updatedTasks.find { it.id == updatedTask.id }
taskToUpdate?.let { task ->
val taskIndex = updatedTasks.indexOf(task)
updatedTasks[taskIndex] = updatedTask
}
// Обновим пользователя с обновленным списком питомцев
val updatedUser = user.copy(taskId = updatedTasks)
// Обновим список пользователей
val userIndex = updatedUsers.indexOf(user)
updatedUsers[userIndex] = updatedUser
users.value = updatedUsers
}
}
fun updateUser(updatedUser: User) {
val updatedUsers = users.value.toMutableList()
val index = updatedUsers.indexOfFirst { it.login == updatedUser.login }
if (index != -1) {
updatedUsers[index] = updatedUser
users.value = updatedUsers
}
}
}

View File

@ -12,4 +12,6 @@
<string name="create_task_title">Создание задачи</string> <string name="create_task_title">Создание задачи</string>
<string name="edit_task_title">Изменение задачи</string> <string name="edit_task_title">Изменение задачи</string>
<string name="favorite_task_title">Избранные</string> <string name="favorite_task_title">Избранные</string>
<string name="task_name">Название задачи</string>
<string name="password_name">Пароль</string>
</resources> </resources>

View File

@ -2,4 +2,5 @@
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
} }