Skip to content

Commit d28efd2

Browse files
committed
chore: Add basic shared logic
Bug: tkuitocc#68 Change-Id: Iac7b7e3222411f47ce6df969d1507774b20572bd
1 parent f28ea27 commit d28efd2

13 files changed

Lines changed: 206 additions & 12 deletions

File tree

composeApp/src/androidMain/kotlin/app/icampuspass/AndroidApp.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class AndroidApp : Application() {
1414
extraModules = listOf(
1515
module {
1616
factory<GreetingScreenViewModel> {
17-
GreetingScreenViewModel()
17+
GreetingScreenViewModel(
18+
userRepository = get()
19+
)
1820
}
1921

2022
factory<MainScreenViewModel> {

composeApp/src/androidMain/kotlin/app/icampuspass/views/screens/GreetingScreen.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.compose.ui.tooling.preview.Wallpapers.YELLOW_DOMINATED_EXAMPLE
2929
import app.icampuspass.models.Greeting
3030
import app.icampuspass.composeapp.generated.resources.Res
3131
import app.icampuspass.composeapp.generated.resources.compose_multiplatform
32+
import app.icampuspass.models.getPlatform
3233
import app.icampuspass.viewmodels.GreetingScreenViewModel
3334
import app.icampuspass.views.theme.Theme
3435
import org.jetbrains.compose.resources.painterResource
@@ -38,11 +39,15 @@ import org.koin.androidx.compose.koinViewModel
3839
fun GreetingScreen(
3940
viewModel: GreetingScreenViewModel = koinViewModel()
4041
) {
41-
GreetingScreenContent()
42+
GreetingScreenContent(
43+
greeting = remember { viewModel.getGreeting().greet() }
44+
)
4245
}
4346

4447
@Composable
45-
private fun GreetingScreenContent() {
48+
private fun GreetingScreenContent(
49+
greeting: String
50+
) {
4651
var showContent by remember { mutableStateOf(value = false) }
4752

4853
Column(
@@ -57,8 +62,6 @@ private fun GreetingScreenContent() {
5762
}
5863

5964
AnimatedVisibility(visible = showContent) {
60-
val greeting = remember { Greeting().greet() }
61-
6265
Column(
6366
modifier = Modifier.fillMaxWidth(),
6467
horizontalAlignment = Alignment.CenterHorizontally
@@ -102,6 +105,8 @@ private fun GreetingScreenContent() {
102105
@Composable
103106
private fun GreetingScreenPreview() {
104107
Theme {
105-
GreetingScreenContent()
108+
GreetingScreenContent(
109+
greeting = remember { Greeting(platform = getPlatform()).greet() }
110+
)
106111
}
107112
}

shared/src/commonMain/kotlin/app/icampuspass/Koin.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package app.icampuspass
22

3+
import app.icampuspass.models.Greeting
4+
import app.icampuspass.models.Platform
35
import app.icampuspass.models.UserAccountRepository
46
import app.icampuspass.models.UserRepository
57
import app.icampuspass.models.database.DatabaseHelper
8+
import io.ktor.client.HttpClient
9+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
10+
import io.ktor.client.plugins.cookies.HttpCookies
11+
import io.ktor.http.ContentType
12+
import io.ktor.serialization.kotlinx.json.json
13+
import kotlinx.serialization.json.Json
614
import org.koin.core.context.startKoin
715
import org.koin.core.module.Module
816
import org.koin.dsl.module
@@ -23,6 +31,33 @@ val commonModule = module {
2331
init()
2432
}
2533
}
34+
35+
single<Greeting> {
36+
Greeting(platform = get())
37+
}
38+
39+
single<HttpClient> {
40+
HttpClient {
41+
install(plugin = ContentNegotiation) {
42+
json(
43+
json = get(),
44+
contentType = ContentType.Any
45+
)
46+
}
47+
48+
install(plugin = HttpCookies)
49+
}
50+
}
51+
52+
single<Json> {
53+
Json {
54+
ignoreUnknownKeys = true
55+
}
56+
}
57+
58+
single<Platform> {
59+
getPlatform()
60+
}
2661
}
2762

2863
expect val platformModule: Module

shared/src/commonMain/kotlin/app/icampuspass/models/Greeting.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package app.icampuspass.models
22

3-
class Greeting {
4-
private val platform = getPlatform()
5-
3+
class Greeting(
4+
private val platform: Platform
5+
) {
66
fun greet(): String {
77
return "Hello, ${platform.name}!"
88
}

shared/src/commonMain/kotlin/app/icampuspass/models/UserRepository.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,40 @@ import kotlinx.coroutines.CoroutineScope
44
import kotlinx.coroutines.SupervisorJob
55
import kotlinx.coroutines.launch
66

7-
class UserRepository() {
7+
class UserRepository(
8+
private val greeting: Greeting
9+
) {
810
private val scope = CoroutineScope(context = SupervisorJob())
911

1012
fun init() {
1113
scope.launch {}
1214
}
15+
16+
fun getGreeting(): Greeting {
17+
return greeting
18+
}
19+
20+
fun getEducationUnits() {}
21+
22+
fun getEducationUnitById(id: Int) {}
23+
24+
fun getCurrentEducationUnit() {}
25+
26+
fun getUsers() {}
27+
28+
fun getUserById(id: Int) {}
29+
30+
fun getCurrentUser() {}
31+
32+
fun getAccounts() {}
33+
34+
fun getAccountById(id: Int) {}
35+
36+
fun getCurrentAccounts() {}
37+
38+
fun getClassSchedules() {}
39+
40+
fun getClassScheduleById(id: Int) {}
41+
42+
fun getCurrentClassSchedule() {}
1343
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.icampuspass.models.data
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Account(
7+
val id: Int
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.icampuspass.models.data
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Schedule(
7+
val account: Account
8+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package app.icampuspass.models.data
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Session(
7+
val id: String,
8+
val courseSession: String,
9+
val courseName: String,
10+
val instructorName: String,
11+
val location: String,
12+
val gradeNumber: String,
13+
val examSeatNumber: String?
14+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.icampuspass.models.data
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class User(
7+
val id: Int
8+
)

shared/src/commonMain/kotlin/app/icampuspass/models/database/DatabaseHelper.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.icampuspass.models.database
22

3+
import app.cash.sqldelight.db.SqlDriver
4+
35
class DatabaseHelper(
46
private val driverFactory: DriverFactory
57
) {
@@ -9,5 +11,7 @@ class DatabaseHelper(
911

1012
private val driver = driverFactory.createDriver()
1113

12-
private val database = Database(driver)
14+
private val database: Database = Database(driver)
15+
16+
private val databaseQueries: DatabaseQueries = database.databaseQueries
1317
}

0 commit comments

Comments
 (0)