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
package com.fedetorres.beziercurves | |
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.view.View | |
/** | |
* Federico Torres |
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
data class MoviesState( | |
val movies: List<Movie> = emptyList(), | |
val selectedMovie: Movie? = null, | |
) { | |
init { | |
freeze() | |
} | |
} |
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
sealed class MoviesActions { | |
object FetchMovies : MoviesActions() | |
data class SelectMovie(val movie: Movie) : MoviesActions() | |
data class SaveMovies(val movies: List<Movie>) : MoviesActions() | |
} |
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
sealed class MoviesEffects( | |
override val dispatcher: CoroutineDispatcher = ..., | |
override val coroutineScope: CoroutineScope? = null) : SideEffectInterface { | |
object LoadMovies : MoviesEffects() | |
} |
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
typealias NextResult = Next<MoviesState, MoviesEffects, MovieEvents> | |
class MoviesUpdater : Updater<MoviesActions, MoviesState, MoviesEffects, MovieEvents> { | |
override fun onNewAction( | |
action: MoviesActions, | |
currentState: MoviesState | |
): Next<MoviesState, MoviesEffects, MovieEvents> { | |
return when (action) { | |
is MoviesActions.FetchMovies -> fetchMovies(action, currentState) | |
is MoviesActions.SaveMovies -> saveMovies(action, currentState) |
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
class MoviesProcessor() : Processor<MoviesEffects, MoviesActions> { | |
private val api : TMDApi = TMDApi() | |
override suspend fun dispatchSideEffect(effect: MoviesEffects): MoviesActions { | |
return when(effect){ | |
is MoviesEffects.LoadMovies -> getMovies(effect) | |
} | |
} |
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
class MoviesViewModel : | |
ArchViewModel<MoviesActions, MoviesState, MoviesEffects, MovieEvents, Nothing>( | |
updater = MoviesUpdater(), | |
initialState = MoviesState(), | |
initialEffects = setOf(MoviesEffects.LoadMovies()), | |
processor = MoviesProcessor(threadInfo), | |
) { | |
} |
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
class FlowWrapper<T>(private val source: Flow<T>) : Flow<T> by source { | |
init { | |
freeze() | |
} | |
fun collect(onEach: (T) -> Unit, onCompletion: (cause: Throwable?) -> Unit): Cancellable { | |
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main) | |
scope.launch { | |
try { |
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
class MoviesFragment : Fragment() { | |
private val viewModel: MoviesViewModel by activityViewModels {...} | |
override fun onCreateView(inflater: LayoutInflater): View = ComposeView(inflater.context).apply { | |
layoutParams = ... | |
setContent { | |
val state by viewModel.observeState().collectAsState() | |
MoviesListView(state = state, onMovieSelected = { movie -> |
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
struct MoviesListScreenView: View { | |
@State var state: MoviesState = MoviesState(movies: [], selectedMovie: nil) | |
let viewModel = MoviesViewModel() | |
var body: some View { | |
List() { | |
ForEach(state.movies, id: \.self) { movie in | |
MovieView(movie, ...) | |
} | |
} |
OlderNewer