third labwork

This commit is contained in:
Zara28 2023-11-25 10:55:49 +04:00
parent 5eed50644d
commit a1108f8162
19 changed files with 623 additions and 32 deletions

2
.idea/kotlinc.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.8.10" />
<option name="version" value="1.6.21" />
</component>
</project>

View File

@ -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")
}

View File

@ -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<List<Dairy>>
@Query("select * from dairy where dairy.date = :date")
fun getDairyByDate(date: String): Flow<Dairy>
@Insert
suspend fun insert(dairy: Dairy)
@Update
suspend fun update(dairy: Dairy)
@Delete
suspend fun delete(dairy: Dairy)
}

View File

@ -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<List<Event>>
@Query("select * from event where event.date = :date")
fun getEventByDate(date: String): Flow<Event>
@Insert
suspend fun insert(event: Event)
@Update
suspend fun update(event: Event)
@Delete
suspend fun delete(event: Event)
}

View File

@ -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<List<Note>>
@Query("select * from note where note.id = :id")
fun getNoteById(id: Int): Flow<Note>
@Insert
suspend fun insert(note: Note)
@Update
suspend fun update(note: Note)
@Delete
suspend fun delete(note: Note)
}

View File

@ -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<List<Review>>
@Query("select * from review where review.date = :date")
fun getReviewByDate(date: String): Flow<Review>
@Insert
suspend fun insert(review: Review)
@Update
suspend fun update(review: Review)
@Delete
suspend fun delete(review: Review)
}

View File

@ -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<List<Task>>
@Query("select * from task where :date BETWEEN dateStart AND dateEnd")
fun getTaskByDate(date: String): Flow<Task>
@Insert
suspend fun insert(review: Review)
@Update
suspend fun update(review: Review)
@Delete
suspend fun delete(review: Review)
}

View File

@ -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<List<User>>
@Query("select * from user where user.id = :idUser")
fun getUserById(idUser: Int): Flow<User>
@Query("select * from review WHERE review.userId= :idUser")
fun getUserReviews(idUser: Int): Flow<List<Review>>
@Insert
suspend fun insert(user: User)
@Update
suspend fun update(user: User)
@Delete
suspend fun delete(user: User)
}

View File

@ -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 }
}
}
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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<User>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
DairyDatabase.getInstance(context).userDao().getAll().collect { data: Collection<User> ->
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<User>, 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)

View File

@ -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<Event>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
DairyDatabase.getInstance(context).eventDao().getAll().collect { data: Collection<Event> ->
list.clear()
list.addAll(data)
}
}
}
Row {
for (l: Event in list) {

View File

@ -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<Note>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
DairyDatabase.getInstance(context).noteDao().getAll().collect { data: Collection<com.example.pmu_dozorova.room.entity.Note> ->
list.clear()
list.addAll(data)
}
}
}
Scaffold(bottomBar = {
Navbar(navController, currentDestination = null, modifier = Modifier.padding(1.dp))
}) { innerPadding ->

View File

@ -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<User>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
DairyDatabase.getInstance(context).userDao().getAll().collect { data: Collection<User> ->
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<User>, 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)
}
}
}