Skip to content

Commit 60f8280

Browse files
committed
- Created data layer for topic list
- Refactored `TopicListScreen` to `TopicScreen` to fetch data from `TopicViewModel` - Updated Koin module for `TopicViewModel` - Configured navigation to include `TopicScreen`
1 parent 2b084d0 commit 60f8280

File tree

7 files changed

+84
-13
lines changed

7 files changed

+84
-13
lines changed

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/core/KtorHttpClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.developersbreach.kotlindictionarymultiplatform.core
22

3+
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatCompletionRequest
4+
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatCompletionResponse
5+
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatMessage
6+
import com.developersbreach.kotlindictionarymultiplatform.data.detail.FunctionDefinition
7+
import com.developersbreach.kotlindictionarymultiplatform.data.detail.KotlinTopicDetails
38
import io.ktor.client.HttpClient
49
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
510
import io.ktor.client.request.header
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.developersbreach.kotlindictionarymultiplatform.data.topic.model
2+
3+
data class Topic(
4+
val name: String
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.developersbreach.kotlindictionarymultiplatform.data.topic.repository
2+
3+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
4+
5+
object TopicRepository {
6+
fun getTopics(): List<Topic> = listOf(
7+
Topic("Variables"),
8+
Topic("Strings"),
9+
Topic("Functions"),
10+
Topic("Coroutines"),
11+
Topic("Classes"),
12+
Topic("Interfaces"),
13+
Topic("Objects"),
14+
Topic("Collections"),
15+
Topic("Null Safety"),
16+
Topic("Lambdas"),
17+
Topic("Higher-Order Functions"),
18+
Topic("Delegation"),
19+
Topic("Sealed Classes"),
20+
Topic("Generics"),
21+
Topic("Annotations")
22+
)
23+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.developersbreach.kotlindictionarymultiplatform.di
22

3-
import org.koin.core.module.dsl.viewModel
4-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailViewModel
53
import org.koin.dsl.module
4+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.detail.DetailViewModel
5+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicViewModel
6+
import org.koin.core.module.dsl.viewModel
67

78
val appModule = module {
8-
99
viewModel {
1010
DetailViewModel(get())
1111
}
12-
}
12+
viewModel {
13+
TopicViewModel()
14+
}
15+
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/navigation/AppNavigation.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import androidx.compose.runtime.remember
55
import androidx.navigation.compose.NavHost
66
import androidx.navigation.compose.composable
77
import androidx.navigation.compose.rememberNavController
8-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailScreen
9-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailViewModel
10-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.TopicListScreen
8+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.detail.DetailScreen
9+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.detail.DetailViewModel
10+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicScreen
11+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicViewModel
1112
import org.koin.compose.viewmodel.koinViewModel
1213

1314
@Composable
@@ -21,12 +22,13 @@ fun AppNavigation(
2122
navController = navController,
2223
startDestination = startDestination,
2324
) {
24-
2525
composable<AppDestinations.TopicList> {
26-
TopicListScreen(
26+
val viewModel: TopicViewModel = koinViewModel()
27+
TopicScreen(
2728
onTopicClick = { selectedTopicId ->
2829
actions.navigateToDetail(selectedTopicId)
29-
}
30+
},
31+
viewModel = viewModel
3032
)
3133
}
3234

@@ -35,4 +37,4 @@ fun AppNavigation(
3537
DetailScreen(viewModel = viewModel)
3638
}
3739
}
38-
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
6+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicRepository
7+
import kotlinx.coroutines.flow.MutableStateFlow
8+
import kotlinx.coroutines.flow.StateFlow
9+
import kotlinx.coroutines.launch
10+
11+
class TopicViewModel : ViewModel() {
12+
13+
private val _topics = MutableStateFlow<List<Topic>>(emptyList())
14+
val topics: StateFlow<List<Topic>> = _topics
15+
16+
init {
17+
fetchTopicList()
18+
}
19+
20+
private fun fetchTopicList() {
21+
viewModelScope.launch {
22+
try {
23+
val topics = TopicRepository.getTopics()
24+
println("Successfully fetched topics: $topics")
25+
_topics.value = topics
26+
} catch (e: Exception) {
27+
println("Error fetching topics: ${e.message}")
28+
e.printStackTrace()
29+
}
30+
}
31+
}
32+
}

composeApp/src/desktopMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/main.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package com.developersbreach.kotlindictionarymultiplatform
33
import androidx.compose.ui.window.Window
44
import androidx.compose.ui.window.application
55
import com.developersbreach.kotlindictionarymultiplatform.di.appModule
6-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.TopicListScreen
6+
import com.developersbreach.kotlindictionarymultiplatform.ui.navigation.AppNavigation
7+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicScreen
78
import org.koin.core.context.startKoin
89

910
fun main() {
@@ -15,7 +16,7 @@ fun main() {
1516
onCloseRequest = ::exitApplication,
1617
title = "Kotlin Dictionary Multiplatform",
1718
) {
18-
TopicListScreen { }
19+
AppNavigation()
1920
}
2021
}
2122
}

0 commit comments

Comments
 (0)