-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update movie list, movie detail in compose
- Loading branch information
1 parent
70bb3fe
commit d73e182
Showing
10 changed files
with
417 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/example/moviedb/compose/ComposeActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.moviedb.compose | ||
|
||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import com.example.moviedb.compose.theme.ComposeAppTheme | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class ComposeActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
ComposeApp() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun ComposeApp() { | ||
ComposeAppTheme { | ||
Surface( | ||
modifier = Modifier.fillMaxSize(), | ||
color = MaterialTheme.colorScheme.background | ||
) { | ||
MainNavigation() | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/example/moviedb/compose/Navigation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.example.moviedb.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navArgument | ||
import com.example.moviedb.compose.ui.detail.DetailScreen | ||
import com.example.moviedb.compose.ui.home.HomeScreen | ||
import okio.Path.Companion.toPath | ||
|
||
@Composable | ||
fun MainNavigation() { | ||
val navController = rememberNavController() | ||
NavHost(navController = navController, startDestination = Route.MAIN) { | ||
composable(route = Route.MAIN) { | ||
HomeScreen(navController = navController) | ||
} | ||
composable( | ||
route = Route.MOVIE_DETAIL, | ||
arguments = listOf(navArgument(Route.Param.MOVIE_ID) { | ||
type = NavType.Companion.StringType | ||
}) | ||
) { backStackEntry -> | ||
DetailScreen( | ||
navController = navController, | ||
movieId = backStackEntry.arguments?.getString(Route.Param.MOVIE_ID) | ||
) | ||
} | ||
} | ||
} | ||
|
||
object Route { | ||
const val MAIN = "main" | ||
const val MOVIE_DETAIL = "movieDetail/{${Param.MOVIE_ID}}" | ||
|
||
object Param { | ||
const val MOVIE_ID = "movieId" | ||
|
||
fun toPath(param: String) = "{${param}}" | ||
} | ||
} | ||
|
||
fun NavController.toMovieDetail(movieId: String?) { | ||
navigate( | ||
route = Route.MOVIE_DETAIL.replace( | ||
Route.Param.toPath(Route.Param.MOVIE_ID), | ||
movieId ?: "" | ||
) | ||
) | ||
} |
44 changes: 0 additions & 44 deletions
44
app/src/main/java/com/example/moviedb/compose/main/ComposeActivity.kt
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
app/src/main/java/com/example/moviedb/compose/ui/detail/DetailScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.example.moviedb.compose.ui.detail | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Error | ||
import androidx.compose.material.icons.filled.Image | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.navigation.NavController | ||
import com.example.moviedb.R | ||
import com.example.moviedb.data.model.Movie | ||
import com.example.moviedb.data.repository.UserRepository | ||
import com.example.moviedb.ui.base.BaseViewModel | ||
import com.skydoves.landscapist.ImageOptions | ||
import com.skydoves.landscapist.components.rememberImageComponent | ||
import com.skydoves.landscapist.glide.GlideImage | ||
import com.skydoves.landscapist.placeholder.placeholder.PlaceholderPlugin | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@Composable | ||
fun DetailScreen( | ||
navController: NavController, | ||
movieId: String?, | ||
) { | ||
val viewModel: DetailViewModel = hiltViewModel() | ||
viewModel.getMovieDetail(movieId = movieId) | ||
val movie by viewModel.movie.collectAsState() | ||
MovieDetailBody(movie = movie, onClickBack = { navController.popBackStack() }) | ||
} | ||
|
||
@Composable | ||
fun MovieDetailBody( | ||
movie: Movie?, | ||
onClickBack: () -> Unit | ||
) { | ||
if (movie != null) { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(Color.Black) | ||
) { | ||
Box(modifier = Modifier.fillMaxWidth()) { | ||
GlideImage( | ||
imageModel = { movie.getFullBackdropPath() ?: "" }, | ||
modifier = Modifier.fillMaxWidth(), | ||
component = rememberImageComponent { | ||
+PlaceholderPlugin.Loading(Icons.Filled.Image) | ||
+PlaceholderPlugin.Failure(Icons.Filled.Error) | ||
}, | ||
imageOptions = ImageOptions(), | ||
) | ||
Image( | ||
painterResource(R.drawable.ic_arrow_back_white_24dp), | ||
contentDescription = "", | ||
contentScale = ContentScale.Crop, | ||
modifier = Modifier | ||
.size(48.dp) | ||
.clip(CircleShape) | ||
.clickable { | ||
onClickBack.invoke() | ||
} | ||
.padding(12.dp), | ||
) | ||
} | ||
Text( | ||
movie.title ?: "", | ||
color = Color.White, | ||
modifier = Modifier.padding(16.dp), | ||
fontSize = 20.sp, | ||
) | ||
Text(movie.releaseDate ?: "", color = Color.White) | ||
Text(movie.overview ?: "", color = Color.White) | ||
} | ||
} else { | ||
|
||
} | ||
} | ||
|
||
@HiltViewModel | ||
class DetailViewModel @Inject constructor( | ||
private val userRepository: UserRepository | ||
) : BaseViewModel() { | ||
private val _movie = MutableStateFlow<Movie?>(null) | ||
val movie: StateFlow<Movie?> = _movie | ||
|
||
fun getMovieDetail(movieId: String?) { | ||
if (movieId.isNullOrBlank()) return | ||
viewModelScope.launch { | ||
try { | ||
_movie.value = userRepository.getMovieById(movieId) | ||
} catch (e: Throwable) { | ||
onError(e) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.