Skip to content

Commit

Permalink
feat: Assisted Injection param in HiltViewModel (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4tchkn authored Mar 2, 2024
1 parent e03c8a7 commit 2f03ef2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
16 changes: 10 additions & 6 deletions app/src/main/kotlin/KonnpassApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import io.github.b4tchkn.konnpass.ui.screen.EventDetailScreen
import io.github.b4tchkn.konnpass.ui.screen.HomeScreen
import io.github.b4tchkn.konnpass.ui.screen.eventDetailScreenParamId
import io.github.b4tchkn.konnpass.ui.screen.eventDetailScreenRoute
import io.github.b4tchkn.konnpass.ui.screen.navigateToEventDetailScreen

@Composable
fun KonnpassApp() {
Expand All @@ -21,20 +24,21 @@ fun KonnpassNavHost(
NavHost(navController = navController, startDestination = "home") {
composable("home") {
HomeScreen(
onEventPressed = {
navController.navigate("event_detail/${it.title}")
},
onEventPressed = navController::navigateToEventDetailScreen,
)
}
composable(
"event_detail/{data}",
eventDetailScreenRoute,
arguments = listOf(
navArgument("data") {
navArgument(eventDetailScreenParamId) {
type = NavType.StringType
},
),
) {
EventDetailScreen(it.arguments?.getString("data"))
EventDetailScreen(
eventId = it.arguments?.getString(eventDetailScreenParamId),
onBackPressed = navController::popBackStack,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package io.github.b4tchkn.konnpass.state.event

import androidx.lifecycle.viewModelScope
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.b4tchkn.konnpass.model.EventResponseModel
import io.github.b4tchkn.konnpass.state.AsyncStateViewModel
import io.github.b4tchkn.konnpass.usecase.GetEventsUseCase
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class EventStateViewModel @Inject constructor(
@HiltViewModel(assistedFactory = EventStateViewModelFactory::class)
class EventStateViewModel @AssistedInject constructor(
private val useCase: GetEventsUseCase,
@Assisted private val param: EventStateViewModelParam,
) : AsyncStateViewModel<EventResponseModel>() {

init {
Expand All @@ -20,9 +22,6 @@ class EventStateViewModel @Inject constructor(
}

override suspend fun fetch(): EventResponseModel {
val param = EventStateViewModelParam(
count = LIMIT_LOAD_COUNT,
)
return useCase(
eventId = param.eventId,
keyword = param.keyword,
Expand All @@ -33,7 +32,7 @@ class EventStateViewModel @Inject constructor(
seriesId = param.seriesId,
start = param.start,
order = param.order,
count = param.count,
count = param.count ?: LIMIT_LOAD_COUNT,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.b4tchkn.konnpass.state.event

import dagger.assisted.AssistedFactory

@AssistedFactory
interface EventStateViewModelFactory {
fun create(param: EventStateViewModelParam): EventStateViewModel
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
package io.github.b4tchkn.konnpass.ui.screen

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import io.github.b4tchkn.konnpass.model.EventModel

const val eventDetailScreenParamId = "eventId"
const val eventDetailScreenRoute = "event_detail/{$eventDetailScreenParamId}"

fun NavController.navigateToEventDetailScreen(
event: EventModel,
) {
navigate(
eventDetailScreenRoute.replace(
"{$eventDetailScreenParamId}",
event.eventId.toString(),
),
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EventDetailScreen(data: String?) {
Scaffold {
fun EventDetailScreen(
eventId: String?,
onBackPressed: () -> Unit,
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text(text = eventId ?: "") },
navigationIcon = {
IconButton(onClick = onBackPressed) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "もどる",
)
}
},
)
},
) {
Text(
modifier = Modifier.padding(it),
text = data ?: "None",
text = eventId ?: "None",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ import androidx.hilt.navigation.compose.hiltViewModel
import io.github.b4tchkn.konnpass.model.EventModel
import io.github.b4tchkn.konnpass.state.LoadingStatus
import io.github.b4tchkn.konnpass.state.event.EventStateViewModel
import io.github.b4tchkn.konnpass.state.event.EventStateViewModelFactory
import io.github.b4tchkn.konnpass.state.event.EventStateViewModelParam

@Composable
fun HomeScreen(
eventStateViewModel: EventStateViewModel = hiltViewModel(),
eventStateViewModel: EventStateViewModel = hiltViewModel(
creationCallback = { factory: EventStateViewModelFactory ->
factory.create(EventStateViewModelParam())
},
),
onEventPressed: (event: EventModel) -> Unit,
) {
val eventState by eventStateViewModel.state.collectAsState()
Expand Down

0 comments on commit 2f03ef2

Please sign in to comment.