-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
73 lines (60 loc) · 2.03 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.example.finalcoursework2
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.room.Room
import kotlinx.coroutines.launch
lateinit var db: AppDatabase
lateinit var leagueDao: LeagueDao
class MainActivity : ComponentActivity() {
private companion object {
const val CURRENT_SCREEN_KEY = "current_screen"
}
private var currentScreen: String = "Homepage" // Default screen
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
db = Room.databaseBuilder(this, AppDatabase::class.java, "LeagueDatabase").build()
leagueDao = db.leagueDao()
savedInstanceState?.getString(CURRENT_SCREEN_KEY)?.let {
currentScreen = it
}
setContent {
Nav()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(CURRENT_SCREEN_KEY, currentScreen)
}
}
@Composable
fun Nav() {
val navController = rememberNavController()
val scope = rememberCoroutineScope()
fun saveToDatabase(data: String) {
scope.launch {
leagueDao.saveDataToDatabase(data)
}
}
NavHost(navController = navController, startDestination = "Homepage") {
composable(route = "Homepage") {
Homepage(navController)
}
composable(route = "AddLeagues") {
AddLeagues(navController)
}
composable(route = "SearchClubsByLeague") {
SearchClubsByLeague(navController, onSaveToDatabase = { data ->
saveToDatabase(data)
})
}
composable(route = "SearchClubs") {
SearchClubs(navController)
}
}
}