БД работает

This commit is contained in:
maxnes3 2023-11-24 21:39:43 +04:00
parent 1d1a0e9be7
commit f4fa24f50a
5 changed files with 37 additions and 21 deletions

View File

@ -41,22 +41,6 @@ class MainActivity : ComponentActivity() {
color = MaterialTheme.colorScheme.background
) {
val navController = rememberNavController()
/*val mailSingleton = MailSingleton()
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
mailSingleton.addMail(Mail(0, 0, "Дзюнзи Ито", "Выложил новый"))
mailSingleton.addMail(Mail(1, 1, "Стивен Кинг", "Меня отменили в Твиттере"))
val storySingleton = StorySingleton()
storySingleton.addStory(Story(0, "Чужак", "Знаменитая книга стивена кинга", R.drawable.king))
storySingleton.addStory(Story(1, "Переулок", "История ужасов от Дзюнзи Ито", R.drawable.dzun))
storySingleton.addStory(Story(2, "Чужак", "Знаменитая книга стивена кинга", R.drawable.king))
storySingleton.addStory(Story(3, "Переулок", "История ужасов от Дзюнзи Ито", R.drawable.dzun))*/
NavBar(navController = navController)
}
}

View File

@ -40,6 +40,7 @@ import com.example.mobileapp.database.entities.Story
import com.example.mobileapp.ui.theme.BackgroundItem2
import com.example.mobileapp.ui.theme.ButtonColor1
import com.example.mobileapp.ui.theme.ButtonColor2
import kotlin.reflect.typeOf
@Composable
fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>){
@ -49,9 +50,9 @@ fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>
.fillMaxWidth()
){
item {
when(dataList[0]){
is Story -> addNewListItem(navController, "editstory")
is Mail -> addNewListItem(navController, "editmail")
when {
dataList.isListOf<Story>() -> addNewListItem(navController, "editstory")
dataList.isListOf<Mail>() -> addNewListItem(navController, "editmail")
}
}
items(dataList){ item ->
@ -63,6 +64,10 @@ fun <T : Any> DataListScroll(navController: NavHostController, dataList: List<T>
}
}
inline fun <reified T> List<*>.isListOf(): Boolean {
return isNotEmpty() && all { it is T }
}
@Composable
fun StoryListItem(item: Story, navController: NavHostController){
val isExpanded = remember {

View File

@ -118,11 +118,19 @@ fun NavBar(navController: NavHostController) {
SettingsScreen(navController = navController)
bottomBarState.value = true
}
composable("editstory"){ // Без аргумента
EditStoryScreen(navController = navController)
bottomBarState.value = false
}
composable("editstory/{storyId}"){ navBackStackEntry ->
val storyId = navBackStackEntry.arguments?.getInt("storyId")
EditStoryScreen(navController = navController, storyId = storyId)
bottomBarState.value = false
}
composable("editmail"){ // Без аргумента
EditMailScreen(navController = navController)
bottomBarState.value = false
}
composable("editmail/{mailId}"){ navBackStackEntry ->
val mailId = navBackStackEntry.arguments?.getInt("mailId")
EditMailScreen(navController = navController, mailId = mailId)

View File

@ -38,11 +38,11 @@ abstract class MobileAppDataBase : RoomDatabase() {
userDao.insert(User(id = 1, login = "Дзюнзи Ито", password = "1234", email = "ito@gmail.com"))
userDao.insert(User(id = 2, login = "Стивен Кинг", password = "4321", email = "king@gmail.com"))
val storyDao = database.storyDao()
/*val storyDao = database.storyDao()
storyDao.insert(Story(title = "Переулок", description = "История ужасов от Дзюнзи Ито",
cover = BitmapFactory.decodeResource(null, R.drawable.dzun), userId = 1))
storyDao.insert(Story(title = "Чужак", description = "Знаменитая книга стивена кинга",
cover = BitmapFactory.decodeResource(null, R.drawable.king), userId = 2))
cover = BitmapFactory.decodeResource(null, R.drawable.king), userId = 2))*/
val mailDao = database.mailDao()
mailDao.insert(Mail(message = "Выложил новые страницы", userId = 1))

View File

@ -7,10 +7,14 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
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.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
@ -18,11 +22,26 @@ import com.example.mobileapp.R
import com.example.mobileapp.components.NavigationButton
import com.example.mobileapp.components.PasswordInputField
import com.example.mobileapp.components.PlaceholderInputField
import com.example.mobileapp.database.MobileAppDataBase
import com.example.mobileapp.database.entities.User
import com.example.mobileapp.ui.theme.ButtonColor1
import com.example.mobileapp.ui.theme.ButtonColor2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun Authorization(navController: NavHostController){
val context = LocalContext.current
val users = remember { mutableStateListOf<User>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
MobileAppDataBase.getInstance(context).userDao().getAll().collect { data ->
users.clear()
users.addAll(data)
}
}
}
Column(
modifier = Modifier
.fillMaxSize()