From a1108f8162d08fcf782d5f4fe6aa700861f174ac Mon Sep 17 00:00:00 2001 From: Zara28 Date: Sat, 25 Nov 2023 10:55:49 +0400 Subject: [PATCH] third labwork --- .idea/kotlinc.xml | 2 +- app/build.gradle.kts | 38 ++++++++- .../example/pmu_dozorova/room/dao/DairyDao.kt | 27 ++++++ .../example/pmu_dozorova/room/dao/EventDao.kt | 27 ++++++ .../example/pmu_dozorova/room/dao/NoteDao.kt | 26 ++++++ .../pmu_dozorova/room/dao/ReviewDao.kt | 27 ++++++ .../example/pmu_dozorova/room/dao/TaskDao.kt | 28 +++++++ .../example/pmu_dozorova/room/dao/UserDao.kt | 31 +++++++ .../room/database/DairyDatabase.kt | 82 +++++++++++++++++++ .../example/pmu_dozorova/room/entity/Dairy.kt | 27 ++++++ .../example/pmu_dozorova/room/entity/Event.kt | 27 ++++++ .../example/pmu_dozorova/room/entity/Note.kt | 25 ++++++ .../pmu_dozorova/room/entity/Review.kt | 36 ++++++++ .../example/pmu_dozorova/room/entity/Task.kt | 37 +++++++++ .../example/pmu_dozorova/room/entity/User.kt | 31 +++++++ .../pmu_dozorova/uiComponents/LoginView.kt | 67 ++++++++++++--- .../pmu_dozorova/uiComponents/MonthView.kt | 22 ++++- .../pmu_dozorova/uiComponents/NotesView.kt | 19 ++++- .../uiComponents/RegistrationView.kt | 76 ++++++++++++++--- 19 files changed, 623 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/DairyDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/EventDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/NoteDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/ReviewDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/TaskDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/dao/UserDao.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/database/DairyDatabase.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/Dairy.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/Event.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/Note.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/Review.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/Task.kt create mode 100644 app/src/main/java/com/example/pmu_dozorova/room/entity/User.kt diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 0fc3113..7e340a7 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ac97728..6749910 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,6 +1,8 @@ plugins { id("com.android.application") id("org.jetbrains.kotlin.android") + id("com.google.devtools.ksp") version "1.6.21-1.0.6" + kotlin("kapt") } android { @@ -30,11 +32,11 @@ android { } } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { - jvmTarget = "1.8" + jvmTarget = "17" } buildFeatures { compose = true @@ -74,4 +76,34 @@ dependencies { implementation ("androidx.compose.material3:material3:1.1.2") implementation ("io.github.boguszpawlowski.composecalendar:composecalendar:1.1.1") + + 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") + + implementation("androidx.room:room-runtime:$room_version") + kapt("androidx.room:room-compiler:$room_version") + + // To use Kotlin annotation processing tool (kapt) + //kapt("androidx.room:room-compiler:$room_version") + // To use Kotlin Symbol Processing (KSP) + ksp("androidx.room:room-compiler:$room_version") + + // optional - Kotlin Extensions and Coroutines support for Room + implementation("androidx.room:room-ktx:$room_version") + + // optional - RxJava2 support for Room + //implementation("androidx.room:room-rxjava2:$room_version") + + // optional - RxJava3 support for Room + implementation("androidx.room:room-rxjava3:$room_version") + + // optional - Guava support for Room, including Optional and ListenableFuture + // implementation("androidx.room:room-guava:$room_version") + + // optional - Paging 3 Integration + implementation("androidx.room:room-paging:$room_version") } \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/DairyDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/DairyDao.kt new file mode 100644 index 0000000..a49721a --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/DairyDao.kt @@ -0,0 +1,27 @@ +package com.example.pmu_dozorova.room.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Dairy +import kotlinx.coroutines.flow.Flow + +@Dao +interface DairyDao { + @Query("select * from dairy") + fun getAll(): Flow> + + @Query("select * from dairy where dairy.date = :date") + fun getDairyByDate(date: String): Flow + + @Insert + suspend fun insert(dairy: Dairy) + + @Update + suspend fun update(dairy: Dairy) + + @Delete + suspend fun delete(dairy: Dairy) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/EventDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/EventDao.kt new file mode 100644 index 0000000..d0fb0d6 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/EventDao.kt @@ -0,0 +1,27 @@ +package com.example.pmu_dozorova.room.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Event +import kotlinx.coroutines.flow.Flow + +@Dao +interface EventDao { + @Query("select * from event") + fun getAll(): Flow> + + @Query("select * from event where event.date = :date") + fun getEventByDate(date: String): Flow + + @Insert + suspend fun insert(event: Event) + + @Update + suspend fun update(event: Event) + + @Delete + suspend fun delete(event: Event) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/NoteDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/NoteDao.kt new file mode 100644 index 0000000..44decfe --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/NoteDao.kt @@ -0,0 +1,26 @@ +package com.example.pmu_dozorova.room.dao +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Note +import kotlinx.coroutines.flow.Flow + +@Dao +interface NoteDao { + @Query("select * from note") + fun getAll(): Flow> + + @Query("select * from note where note.id = :id") + fun getNoteById(id: Int): Flow + + @Insert + suspend fun insert(note: Note) + + @Update + suspend fun update(note: Note) + + @Delete + suspend fun delete(note: Note) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/ReviewDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/ReviewDao.kt new file mode 100644 index 0000000..bcd221c --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/ReviewDao.kt @@ -0,0 +1,27 @@ +package com.example.pmu_dozorova.room.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Review +import kotlinx.coroutines.flow.Flow + +@Dao +interface ReviewDao { + @Query("select * from review") + fun getAll(): Flow> + + @Query("select * from review where review.date = :date") + fun getReviewByDate(date: String): Flow + + @Insert + suspend fun insert(review: Review) + + @Update + suspend fun update(review: Review) + + @Delete + suspend fun delete(review: Review) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/TaskDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/TaskDao.kt new file mode 100644 index 0000000..32409c2 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/TaskDao.kt @@ -0,0 +1,28 @@ +package com.example.pmu_dozorova.room.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Review +import com.example.pmu_dozorova.room.entity.Task +import kotlinx.coroutines.flow.Flow + +@Dao +interface TaskDao { + @Query("select * from task") + fun getAll(): Flow> + + @Query("select * from task where :date BETWEEN dateStart AND dateEnd") + fun getTaskByDate(date: String): Flow + + @Insert + suspend fun insert(review: Review) + + @Update + suspend fun update(review: Review) + + @Delete + suspend fun delete(review: Review) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/dao/UserDao.kt b/app/src/main/java/com/example/pmu_dozorova/room/dao/UserDao.kt new file mode 100644 index 0000000..174750d --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/dao/UserDao.kt @@ -0,0 +1,31 @@ +package com.example.pmu_dozorova.room.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.example.pmu_dozorova.room.entity.Review +import com.example.pmu_dozorova.room.entity.User +import kotlinx.coroutines.flow.Flow + +@Dao +interface UserDao { + @Query("select * from user") + fun getAll(): Flow> + + @Query("select * from user where user.id = :idUser") + fun getUserById(idUser: Int): Flow + + @Query("select * from review WHERE review.userId= :idUser") + fun getUserReviews(idUser: Int): Flow> + + @Insert + suspend fun insert(user: User) + + @Update + suspend fun update(user: User) + + @Delete + suspend fun delete(user: User) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/database/DairyDatabase.kt b/app/src/main/java/com/example/pmu_dozorova/room/database/DairyDatabase.kt new file mode 100644 index 0000000..14534a0 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/database/DairyDatabase.kt @@ -0,0 +1,82 @@ +package com.example.pmu_dozorova.room.database + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.sqlite.db.SupportSQLiteDatabase +import com.example.pmu_dozorova.room.dao.DairyDao +import com.example.pmu_dozorova.room.dao.EventDao +import com.example.pmu_dozorova.room.dao.NoteDao +import com.example.pmu_dozorova.room.dao.ReviewDao +import com.example.pmu_dozorova.room.dao.TaskDao +import com.example.pmu_dozorova.room.dao.UserDao +import com.example.pmu_dozorova.room.entity.Dairy +import com.example.pmu_dozorova.room.entity.Event +import com.example.pmu_dozorova.room.entity.Note +import com.example.pmu_dozorova.room.entity.Review +import com.example.pmu_dozorova.room.entity.Task +import com.example.pmu_dozorova.room.entity.User +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + + +@Database(entities = [User::class, + Dairy::class, + Event::class, + Note::class, + Review::class, + Task::class], version = 5, exportSchema = false) +abstract class DairyDatabase : RoomDatabase() { + abstract fun userDao(): UserDao + abstract fun dairyDao(): DairyDao + abstract fun eventDao(): EventDao + abstract fun noteDao(): NoteDao + abstract fun reviewDao(): ReviewDao + abstract fun taskDao(): TaskDao + + companion object { + private const val DB_NAME: String = "news-portal" + + @Volatile + private var INSTANCE: DairyDatabase? = null + private suspend fun populateDatabase() { + INSTANCE?.let { database -> + val userDao = database.userDao() + val user1 = User(1, "user","user") + userDao.insert(user1) + + val noteDao = database.noteDao() + val note1 = Note(1, "Города") + val note2 = Note(2, "Планы") + val note3 = Note(3, "Начнем с начала") + + noteDao.insert(note1) + noteDao.insert(note2) + noteDao.insert(note3) + + } + } + + fun getInstance(appContext: Context): DairyDatabase { + return INSTANCE ?: synchronized(this) { + Room.databaseBuilder( + appContext, + DairyDatabase::class.java, + DB_NAME + ) + .addCallback(object : Callback() { + override fun onCreate(db: SupportSQLiteDatabase) { + super.onCreate(db) + CoroutineScope(Dispatchers.IO).launch { + populateDatabase() + } + } + }) + .build() + .also { INSTANCE = it } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/Dairy.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/Dairy.kt new file mode 100644 index 0000000..2f01176 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/Dairy.kt @@ -0,0 +1,27 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "dairy") +data class Dairy( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "text") + var Text: String, + @ColumnInfo(name = "date") + var date: String + ) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Dairy + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/Event.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/Event.kt new file mode 100644 index 0000000..1f475d6 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/Event.kt @@ -0,0 +1,27 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "event") +data class Event( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "name") + var Name: String, + @ColumnInfo(name = "date") + var date: String +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Event + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/Note.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/Note.kt new file mode 100644 index 0000000..764837b --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/Note.kt @@ -0,0 +1,25 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "note") +data class Note( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "text") + var Text: String, +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Note + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/Review.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/Review.kt new file mode 100644 index 0000000..f147952 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/Review.kt @@ -0,0 +1,36 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.ForeignKey +import androidx.room.PrimaryKey +@Entity(tableName = "review", + foreignKeys = [ForeignKey(entity = User::class, + parentColumns = ["id"], childColumns = ["userId"], + onDelete = ForeignKey.CASCADE, + onUpdate = ForeignKey.CASCADE)]) +data class Review( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "date") + var Date: String, + @ColumnInfo(name = "text") + var Text: String, + @ColumnInfo(name = "rating") + var Rating: String, + @ColumnInfo(name = "userId") + var userID: Int, +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Review + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} + diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/Task.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/Task.kt new file mode 100644 index 0000000..ab24f14 --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/Task.kt @@ -0,0 +1,37 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.example.pmu_dozorova.models.Priority + +@Entity(tableName = "task") +data class Task( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "header") + var header: String, + @ColumnInfo(name = "discription") + var discription: String, + @ColumnInfo(name = "dateStart") + var dateStart: String, + @ColumnInfo(name = "dateEnd") + var dateEnd: String, + @ColumnInfo(name = "priority") + var priority: Priority, + @ColumnInfo(name = "status") + var status: Boolean, + +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as Task + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/room/entity/User.kt b/app/src/main/java/com/example/pmu_dozorova/room/entity/User.kt new file mode 100644 index 0000000..a426b4a --- /dev/null +++ b/app/src/main/java/com/example/pmu_dozorova/room/entity/User.kt @@ -0,0 +1,31 @@ +package com.example.pmu_dozorova.room.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey + +@Entity(tableName = "user", + indices = [Index(value = ["Login"], unique = true), + Index(value = ["password"], unique = true) + ]) +data class User( + @PrimaryKey(autoGenerate = true) + val id: Int?, + @ColumnInfo(name = "Login") + var Login: String, + @ColumnInfo(name = "password") + var password: String, +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + other as User + if (id != other.id) return false + return true + } + + override fun hashCode(): Int { + return id ?: -1 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pmu_dozorova/uiComponents/LoginView.kt b/app/src/main/java/com/example/pmu_dozorova/uiComponents/LoginView.kt index 11bcd86..74038ea 100644 --- a/app/src/main/java/com/example/pmu_dozorova/uiComponents/LoginView.kt +++ b/app/src/main/java/com/example/pmu_dozorova/uiComponents/LoginView.kt @@ -1,6 +1,7 @@ package com.example.pmu_dozorova.uiComponents import android.content.Context +import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -16,9 +17,12 @@ import androidx.compose.material3.TextButton import androidx.compose.material3.TextField import androidx.compose.material3.TextFieldDefaults import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -29,17 +33,33 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle +import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.navigation.NavController import com.example.pmu_dozorova.InputType +import com.example.pmu_dozorova.room.database.DairyDatabase +import com.example.pmu_dozorova.room.entity.User import com.google.accompanist.insets.ProvideWindowInsets import com.google.accompanist.insets.navigationBarsWithImePadding +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext - @Composable +@Composable fun Login(navController: NavController) { + var loginValue by rememberSaveable { mutableStateOf("") } + var passwordValue by rememberSaveable { mutableStateOf("") } val context = LocalContext.current + val users = remember { mutableStateListOf() } + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + DairyDatabase.getInstance(context).userDao().getAll().collect { data: Collection -> + users.clear() + users.addAll(data) + } + } + } val passwordFocusRequester = FocusRequester() val focusManager = LocalFocusManager.current ProvideWindowInsets { @@ -58,17 +78,26 @@ import com.google.accompanist.insets.navigationBarsWithImePadding fontSize=15.em ) } - TextInput(InputType.Name, keyboardActions = KeyboardActions(onNext = { - passwordFocusRequester.requestFocus() - }), modifierNew = null) - TextInput(InputType.Password, keyboardActions = KeyboardActions(onDone = { - focusManager.clearFocus() - context.doLogin(navController) - }), focusRequester = passwordFocusRequester, modifierNew = null) +// TextInput(InputType.Name, +// keyboardActions = KeyboardActions(onNext = { +// passwordFocusRequester.requestFocus() +// }), modifierNew = null) +// TextInput(InputType.Password, keyboardActions = KeyboardActions(onDone = { +// focusManager.clearFocus() +// context.doLogin(navController) +// }), focusRequester = passwordFocusRequester, modifierNew = null) + TextField( + value = loginValue, + onValueChange = { loginValue = it }) + TextField( + value = passwordValue, + onValueChange = { passwordValue = it }, + visualTransformation = PasswordVisualTransformation(),) Button(onClick = { - context.doLogin(navController) + context.doLogin(navController, users, loginValue, passwordValue) }, modifier = Modifier.fillMaxWidth()) { - Text("Войти", Modifier.padding(vertical = 8.dp), textAlign = TextAlign.Center ) + Text("Войти", Modifier.padding(vertical = 8.dp), + textAlign = TextAlign.Center ) } Divider( color = Color.White.copy(alpha = 0.3f), @@ -119,8 +148,22 @@ import com.google.accompanist.insets.navigationBarsWithImePadding ) } } - private fun Context.doLogin(navController:NavController) { - navController?.navigate(Screen.Home.route) + private fun Context.doLogin(navController:NavController, users:List, login:String,password:String) { + if (password.isNotEmpty()) { + users.forEach { user -> + if (user.password == password && user.Login == login) { + navController?.navigate(Screen.Home.route) + }else{ + Toast.makeText( + this, + "Неправильно указан логин или пароль", + Toast.LENGTH_SHORT + ).show() + } + } + + } + } fun navRegistration(navController:NavController) { navController?.navigate(Screen.Registration.route) diff --git a/app/src/main/java/com/example/pmu_dozorova/uiComponents/MonthView.kt b/app/src/main/java/com/example/pmu_dozorova/uiComponents/MonthView.kt index f57eaf5..a7a34c5 100644 --- a/app/src/main/java/com/example/pmu_dozorova/uiComponents/MonthView.kt +++ b/app/src/main/java/com/example/pmu_dozorova/uiComponents/MonthView.kt @@ -9,14 +9,22 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateListOf +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.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.navigation.NavHostController -import com.example.pmu_dozorova.models.Event +import com.example.pmu_dozorova.room.entity.Event import com.example.pmu_dozorova.models.getEvents +import com.example.pmu_dozorova.room.database.DairyDatabase +import com.example.pmu_dozorova.room.entity.User import io.github.boguszpawlowski.composecalendar.StaticCalendar +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.time.LocalDate import java.time.format.DateTimeFormatter @@ -34,7 +42,17 @@ fun MounthView(navController: NavHostController){ val localDate = LocalDate.now() //For reference val formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy") val formattedString = localDate.format(formatter) - var list = getEvents() + //var list = getEvents() + val context = LocalContext.current + val list = remember { mutableStateListOf() } + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + DairyDatabase.getInstance(context).eventDao().getAll().collect { data: Collection -> + list.clear() + list.addAll(data) + } + } + } Row { for (l: Event in list) { diff --git a/app/src/main/java/com/example/pmu_dozorova/uiComponents/NotesView.kt b/app/src/main/java/com/example/pmu_dozorova/uiComponents/NotesView.kt index ed48b36..5e613ff 100644 --- a/app/src/main/java/com/example/pmu_dozorova/uiComponents/NotesView.kt +++ b/app/src/main/java/com/example/pmu_dozorova/uiComponents/NotesView.kt @@ -13,6 +13,9 @@ import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -21,13 +24,23 @@ import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.navigation.NavHostController -import com.example.pmu_dozorova.models.Note -import com.example.pmu_dozorova.models.getNotes +import com.example.pmu_dozorova.room.database.DairyDatabase +import com.example.pmu_dozorova.room.entity.Note +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @Composable fun ListNote(navController: NavHostController) { val context = LocalContext.current - val list = getNotes() + val list = remember { mutableStateListOf() } + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + DairyDatabase.getInstance(context).noteDao().getAll().collect { data: Collection -> + list.clear() + list.addAll(data) + } + } + } Scaffold(bottomBar = { Navbar(navController, currentDestination = null, modifier = Modifier.padding(1.dp)) }) { innerPadding -> diff --git a/app/src/main/java/com/example/pmu_dozorova/uiComponents/RegistrationView.kt b/app/src/main/java/com/example/pmu_dozorova/uiComponents/RegistrationView.kt index e53a97e..8d2b1dc 100644 --- a/app/src/main/java/com/example/pmu_dozorova/uiComponents/RegistrationView.kt +++ b/app/src/main/java/com/example/pmu_dozorova/uiComponents/RegistrationView.kt @@ -1,16 +1,24 @@ package com.example.pmu_dozorova.uiComponents import android.content.Context +import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.text.KeyboardActions import androidx.compose.material3.Button import androidx.compose.material3.Text +import androidx.compose.material3.TextField import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester @@ -19,19 +27,36 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle +import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.navigation.NavHostController -import com.example.pmu_dozorova.InputType +import com.example.pmu_dozorova.room.database.DairyDatabase +import com.example.pmu_dozorova.room.entity.User import com.google.accompanist.insets.ProvideWindowInsets import com.google.accompanist.insets.navigationBarsWithImePadding +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun Registration(navController: NavHostController) { val context = LocalContext.current val passwordFocusRequester = FocusRequester() val focusManager = LocalFocusManager.current + var loginValue by rememberSaveable { mutableStateOf("") } + var passwordValue by rememberSaveable { mutableStateOf("") } + val users = remember { mutableStateListOf() } + LaunchedEffect(Unit) { + withContext(Dispatchers.IO) { + DairyDatabase.getInstance(context).userDao().getAll().collect { data: Collection -> + users.clear() + users.addAll(data) + } + } + } ProvideWindowInsets { Column( Modifier @@ -47,15 +72,23 @@ fun Registration(navController: NavHostController) { fontSize=15.em ) } - TextInput(InputType.Name, keyboardActions = KeyboardActions(onNext = { - passwordFocusRequester.requestFocus() - }), modifierNew = null) - TextInput(InputType.Password, keyboardActions = KeyboardActions(onDone = { - focusManager.clearFocus() - context.Registration() - }), focusRequester = passwordFocusRequester, modifierNew = null) +// TextInput(InputType.Name, keyboardActions = KeyboardActions(onNext = { +// passwordFocusRequester.requestFocus() +// }), modifierNew = null) +// TextInput(InputType.Password, keyboardActions = KeyboardActions(onDone = { +// focusManager.clearFocus() +// context.Registration() +// }), focusRequester = passwordFocusRequester, modifierNew = null) + TextField( + value = loginValue, + onValueChange = { loginValue = it }) + TextField( + value = passwordValue, + onValueChange = { passwordValue = it }, + visualTransformation = PasswordVisualTransformation(),) Button(onClick = { - navController.navigate(Screen.Login.route) + context.Registration(navController, users, loginValue, passwordValue, context) + //navController.navigate(Screen.Login.route) }, modifier = Modifier.fillMaxWidth()) { Text("Зарегистрироваться", Modifier.padding(vertical = 8.dp), textAlign = TextAlign.Center ) } @@ -63,6 +96,27 @@ fun Registration(navController: NavHostController) { } } -private fun Context.Registration() { +private fun Context.Registration(navController:NavHostController, users:List, login:String, password:String, context:Context) { + if (password.isNotEmpty()) { + var exit = false + users.forEach { user -> + if (user.password == password && user.Login == login) { + exit = true + Toast.makeText( + this, + "Такой пользователь уже зарегистрирован в системе", + Toast.LENGTH_SHORT + ).show() + } + } + if(!exit) + { + val newUser = User(null, login, password) + CoroutineScope(Dispatchers.IO).launch { + DairyDatabase.getInstance(context).userDao().insert(newUser) + } + navController?.navigate(Screen.Home.route) + } + } } \ No newline at end of file