Skip to content

Commit

Permalink
◀️ code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Jan 10, 2021
1 parent 1206508 commit e1212dc
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 28 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/theapache64/topcorn2/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 14,19 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)
setContentView(R.layout.activity_main)

}

override fun onBackPressed() {
findNavController(R.id.nav_host).navigateUp()
findNavController(R.id.nav_host).let { navController ->
navController.currentDestination.let { currentDestination ->
if (currentDestination?.id == R.id.movies_fragment) {
super.onBackPressed()
} else {
navController.navigateUp()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,7 @@ import androidx.navigation.fragment.findNavController
import com.theapache64.topcorn2.R
import com.theapache64.topcorn2.ui.theme.TopCornTheme
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

/**
* Created by theapache64 : Jan 03 Sun,2021 @ 22:47
Expand All @@ -35,7 36,7 @@ class MoviesFragment : Fragment() {
val navController = findNavController()

navController.navigate(
MoviesFragmentDirections.actionMoviesFragmentToMovieDetailFragment(movieId)
MoviesFragmentDirections.actionMoviesFragmentToMovieDetailFragment(movieId),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
Expand All @@ -17,6 18,7 @@ import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.asLiveData
import com.theapache64.topcorn2.R
import com.theapache64.topcorn2.data.remote.Movie
import com.theapache64.topcorn2.model.Category
Expand All @@ -34,9 36,10 @@ import timber.log.Timber
fun MoviesScreen(
moviesViewModel: MoviesViewModel
) {
val moviesResponseState by moviesViewModel.movies.observeAsState(initial = Resource.Initial())
val sortOrder by moviesViewModel.sortOrder.observeAsState()
val moviesResponseState by moviesViewModel.movies
.asLiveData().observeAsState(initial = Resource.Initial())

val sortOrder by moviesViewModel.sortOrder.collectAsState()
val currentUiMode = AmbientConfiguration.current.uiMode

Scaffold(
Expand All @@ -51,7 54,7 @@ fun MoviesScreen(
},
actions = {
AppBarMenu(
sortOrder = sortOrder!!,
sortOrder = sortOrder,
onSortByStarClicked = {
moviesViewModel.onSortByRatingClicked()
},
Expand Down Expand Up @@ -149,7 152,9 @@ fun BodyContent(
}
is Resource.Success -> {
Timber.d("MoviesScreen: Success")
LazyColumn {
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
itemsIndexed(moviesResponse.data) { _, category ->
CategoryRow(
category = category,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 1,14 @@
package com.theapache64.topcorn2.ui.screen.movies

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.*
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.theapache64.topcorn2.data.remote.Movie
import com.theapache64.topcorn2.data.repositories.movies.MoviesRepo
import com.theapache64.topcorn2.model.Category
import com.theapache64.topcorn2.utils.calladapter.flow.Resource
import com.theapache64.topcorn2.utils.flow.mutableEventFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.*
import timber.log.Timber

/**
Expand Down Expand Up @@ -70,18 70,13 @@ class MoviesViewModel @ViewModelInject constructor(
private val _toggleDarkMode = mutableEventFlow<Boolean>()
val toggleDarkMode: SharedFlow<Boolean> = _toggleDarkMode

/**
* The usage of SingleLiveEvent will be replace when we've an answer for
* https://stackoverflow.com/questions/65633219/sharedflow-maplatest-not-getting-triggered
*/
val sortOrder = MutableLiveData(SORT_ORDER_RATING)

private val _sortOrderToast = mutableEventFlow<Int>()
val sortOrderToast: SharedFlow<Int> = _sortOrderToast

val sortOrder = MutableStateFlow(SORT_ORDER_RATING)

// When ever sortOrder changed, load movies
val movies = sortOrder.switchMap { newSortOrder ->
Timber.d("New sort order is $newSortOrder ")
val movies = sortOrder.flatMapLatest { newSortOrder ->
moviesRepo
.getTop250Movies()
.map {
Expand All @@ -95,17 90,26 @@ class MoviesViewModel @ViewModelInject constructor(
}

is Resource.Success -> {
Timber.d("Shifar: Hit one: ")
_sortOrderToast.tryEmit(newSortOrder)

val movies = it.data
val feedItems = convertToFeed(movies, newSortOrder)

Resource.Success(null, feedItems)
}

is Resource.Error -> {
Resource.Error(it.errorData)
}
}
}.asLiveData(viewModelScope.coroutineContext)
}
}
}.shareIn(
// Converting to hot flow
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
replay = 1
)

private val _goToMovieDetail = mutableEventFlow<Int>()
val goToMovieDetail: SharedFlow<Int> = _goToMovieDetail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.asLiveData
import androidx.navigation.fragment.findNavController
import com.theapache64.topcorn2.R
import com.theapache64.topcorn2.ui.theme.TopCornTheme
import dagger.hilt.android.AndroidEntryPoint

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 4,7 @@ import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import timber.log.Timber

Expand All @@ -18,9 15,9 @@ class TestViewModel @ViewModelInject constructor() : ViewModel() {

private val sortOrder = MutableStateFlow("year")

val myData = sortOrder.map {
val myData = sortOrder.flatMapLatest {
Timber.d("Sort order changed to $it")
"Sort order is $it"
flowOf("Sort order is $it")
}.shareIn(
viewModelScope,
SharingStarted.WhileSubscribed(),
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 10,8 @@
<action
android:id="@ id/action_splash_fragment_to_movies_fragment"
app:destination="@id/movies_fragment"
app:enterAnim="@anim/fragment_fade_enter"
app:popUpTo="@id/nav_graph"
app:launchSingleTop="true"
app:popUpTo="@id/splash_fragment"
app:popUpToInclusive="true" />
</fragment>

Expand Down

0 comments on commit e1212dc

Please sign in to comment.