Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz committed Feb 15, 2024
1 parent d5d8531 commit 5edc0c3
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.receiveAsFlow

@Composable
fun LaunchDetailScreen(
viewModel: LaunchDetailViewModel = hiltViewModel(),
launchId: String,
) {
fun LaunchDetailScreen(viewModel: LaunchDetailViewModel = hiltViewModel(), launchId: String) {
val snackbarHostState = remember { SnackbarHostState() }
val state by viewModel.uiState.collectAsStateWithLifecycle()

Expand All @@ -37,11 +34,13 @@ fun LaunchDetailScreen(

when (state) {
LaunchDetailUiState.Loading -> LoadingComponent()
is LaunchDetailUiState.Error -> viewModel.showError((state as LaunchDetailUiState.Error).message)
is LaunchDetailUiState.Error -> viewModel.showError(
(state as LaunchDetailUiState.Error).message
)
is LaunchDetailUiState.Success ->
LaunchDetailSuccessComponent(
state = state as LaunchDetailUiState.Success,
snackbarHostState = snackbarHostState,
snackbarHostState = snackbarHostState
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,54 @@ import com.nisrulz.example.spacexapi.presentation.features.launchdetail.LaunchDe
import com.nisrulz.example.spacexapi.presentation.features.launchdetail.LaunchDetailViewModel.LaunchDetailUiState.Loading
import com.nisrulz.example.spacexapi.presentation.features.launchdetail.LaunchDetailViewModel.LaunchDetailUiState.Success
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LaunchDetailViewModel
@Inject
constructor(
private val coroutineDispatcher: CoroutineDispatcher,
private val getLaunchDetail: GetLaunchDetail,
private val bookmarkLaunchInfo: ToggleBookmarkLaunchInfo,
) : ViewModel() {
var uiState: MutableStateFlow<LaunchDetailUiState> = MutableStateFlow(Loading)
private set

var eventFlow: Channel<LaunchDetailUiEvent> = Channel(Channel.CONFLATED)
private set

fun getLaunchInfoDetails(launchId: String?) =
viewModelScope.launch(coroutineDispatcher) {
if (launchId.isNullOrBlank()) {
uiState.update { Error("No Data") }
} else {
val launchInfo = getLaunchDetail(launchId)
uiState.update { Success(launchInfo) }
}
}
@Inject
constructor(
private val coroutineDispatcher: CoroutineDispatcher,
private val getLaunchDetail: GetLaunchDetail,
private val bookmarkLaunchInfo: ToggleBookmarkLaunchInfo
) : ViewModel() {
var uiState: MutableStateFlow<LaunchDetailUiState> = MutableStateFlow(Loading)
private set

var eventFlow: Channel<LaunchDetailUiEvent> = Channel(Channel.CONFLATED)
private set

fun getLaunchInfoDetails(launchId: String?) = viewModelScope.launch(coroutineDispatcher) {
if (launchId.isNullOrBlank()) {
uiState.update { Error("No Data") }
} else {
val launchInfo = getLaunchDetail(launchId)
uiState.update { Success(launchInfo) }
}
}

fun bookmark(launchInfo: LaunchInfo) =
viewModelScope.launch(coroutineDispatcher) {
uiState.update { Success(launchInfo) }
bookmarkLaunchInfo(launchInfo)
}
fun bookmark(launchInfo: LaunchInfo) = viewModelScope.launch(coroutineDispatcher) {
uiState.update { Success(launchInfo) }
bookmarkLaunchInfo(launchInfo)
}

fun showError(message: String) =
viewModelScope.launch(coroutineDispatcher) {
eventFlow.send(ShowSnackBar(message))
}
fun showError(message: String) = viewModelScope.launch(coroutineDispatcher) {
eventFlow.send(ShowSnackBar(message))
}

sealed interface LaunchDetailUiState {
data object Loading : LaunchDetailUiState
sealed interface LaunchDetailUiState {
data object Loading : LaunchDetailUiState

data class Error(val message: String) : LaunchDetailUiState
data class Error(val message: String) : LaunchDetailUiState

data class Success(val data: LaunchInfo?) : LaunchDetailUiState
}
data class Success(val data: LaunchInfo?) : LaunchDetailUiState
}

sealed interface LaunchDetailUiEvent {
data class ShowSnackBar(val message: String) : LaunchDetailUiEvent
}
sealed interface LaunchDetailUiEvent {
data class ShowSnackBar(val message: String) : LaunchDetailUiEvent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,62 +40,62 @@ import com.nisrulz.example.spacexapi.presentation.theme.dimens
fun LaunchInfoItem(
launchInfo: LaunchInfo,
onBookmark: (LaunchInfo) -> Unit,
onClick: (String) -> Unit,
onClick: (String) -> Unit
) {
ElevatedCard(
modifier =
Modifier
.fillMaxWidth()
.height(200.dp)
.padding(MaterialTheme.dimens.small),
Modifier
.fillMaxWidth()
.height(200.dp)
.padding(MaterialTheme.dimens.small),
colors =
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant,
),
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
),
onClick = {
onClick(launchInfo.id)
},
}
) {
Row {
AsyncImage(
model =
ImageRequest.Builder(LocalContext.current)
.data(launchInfo.logo)
.crossfade(true)
.build(),
ImageRequest.Builder(LocalContext.current)
.data(launchInfo.logo)
.crossfade(true)
.build(),
placeholder = painterResource(R.drawable.placeholder),
contentDescription = stringResource(R.string.logo_description),
contentScale = ContentScale.Crop,
modifier =
Modifier
.padding(MaterialTheme.dimens.medium)
.fillMaxWidth(0.4f),
Modifier
.padding(MaterialTheme.dimens.medium)
.fillMaxWidth(0.4f)
)

Column(
modifier =
Modifier
.padding(MaterialTheme.dimens.medium)
.fillMaxSize(),
Modifier
.padding(MaterialTheme.dimens.medium)
.fillMaxSize()
) {
Text(
text = "LAUNCH ${launchInfo.flight_number}",
style =
TextStyle(
color = MaterialTheme.colorScheme.primary,
fontSize = 16.sp,
fontStyle = FontStyle.Italic,
fontWeight = FontWeight.Bold,
),
TextStyle(
color = MaterialTheme.colorScheme.primary,
fontSize = 16.sp,
fontStyle = FontStyle.Italic,
fontWeight = FontWeight.Bold
)
)
Text(
text = launchInfo.name,
style =
TextStyle(
color = MaterialTheme.colorScheme.secondary,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
),
TextStyle(
color = MaterialTheme.colorScheme.secondary,
fontSize = 24.sp,
fontWeight = FontWeight.Bold
)
)

Text(text = launchInfo.getFormattedDate())
Expand All @@ -112,11 +112,11 @@ fun LaunchInfoItem(
painter = painterResource(id = drawableIdForBookmark),
contentDescription = stringResource(id = R.string.bookmark),
modifier =
Modifier
.align(Alignment.End)
.clickable {
onBookmark(launchInfo)
},
Modifier
.align(Alignment.End)
.clickable {
onBookmark(launchInfo)
}
)
}
}
Expand All @@ -136,12 +136,12 @@ private fun Preview() {
logo = "",
name = "Name 1",
success = false,
isBookmarked = false,
isBookmarked = false
)
LaunchInfoItem(
launchInfo = testLaunchInfo,
onClick = {},
onBookmark = { },
onBookmark = { }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.flow.receiveAsFlow
@Composable
fun ListOfLaunchesScreen(
viewModel: ListOfLaunchesViewModel = hiltViewModel(),
navigateToDetails: (String) -> Unit = {},
navigateToDetails: (String) -> Unit = {}
) {
val snackbarHostState = remember { SnackbarHostState() }
val state by viewModel.uiState.collectAsStateWithLifecycle()
Expand All @@ -36,7 +36,7 @@ fun ListOfLaunchesScreen(
when (event) {
is ShowSnackBar -> {
snackbarHostState.showSnackbar(
message = event.message,
message = event.message
)
}

Expand All @@ -63,7 +63,7 @@ fun ListOfLaunchesScreen(
},
toggleBookmarkList = {
viewModel.onClickBookmarkToolbarIcon(it)
},
}
)
}
}
Loading

0 comments on commit 5edc0c3

Please sign in to comment.