<lambda>null1 package com.airbnb.lottie.samples
2
3 import androidx.fragment.app.FragmentActivity
4 import com.airbnb.epoxy.EpoxyController
5 import com.airbnb.lottie.samples.model.AnimationData
6 import com.airbnb.lottie.samples.model.AnimationResponse
7 import com.airbnb.lottie.samples.model.CompositionArgs
8 import com.airbnb.lottie.samples.views.loadingView
9 import com.airbnb.lottie.samples.views.lottiefilesTabBar
10 import com.airbnb.lottie.samples.views.marquee
11 import com.airbnb.lottie.samples.views.searchInputItemView
12 import com.airbnb.mvrx.*
13
14
15 data class LottiefilesState(
16 val mode: LottiefilesMode = LottiefilesMode.Recent,
17 val items: List<AnimationData> = emptyList(),
18 val request: Async<AnimationResponse> = Uninitialized,
19 val query: String = ""
20 ) : MvRxState
21
22 class LottiefilesViewModel(
23 initialState: LottiefilesState,
24 private val service: LottiefilesService) : MvRxViewModel<LottiefilesState>(initialState
25 ) {
26 init {
27 selectSubscribe(LottiefilesState::mode) { fetchMoreItems() }
28 }
29
30 fun fetchMoreItems() = withState { state ->
31 if (state.request is Loading) return@withState
32 val page = (state.request()?.currentPage ?: -1) + 1
33 if (state.request()?.lastPage == page && page > 0) return@withState
34
35 when (state.mode) {
36 LottiefilesMode.Recent -> service.getRecent(page)
37 LottiefilesMode.Popular -> service.getPopular(page)
38 LottiefilesMode.Search -> service.search(state.query)
39 }.execute { copy(request = it, items = items + (it()?.data ?: emptyList())) }
40 }
41
42 fun setMode(mode: LottiefilesMode, query: String = "") = setState {
43 if (this.mode == mode && mode != LottiefilesMode.Search) return@setState this
44 if (this.mode == mode && mode == LottiefilesMode.Search && this.query == query) return@setState this
45
46 copy(mode = mode, request = Uninitialized, items = emptyList(), query = query)
47 }
48
49 companion object : MvRxViewModelFactory<LottiefilesState> {
50 @JvmStatic
51 override fun create(activity: FragmentActivity, state: LottiefilesState): LottiefilesViewModel {
52 val service = (activity.applicationContext as LottieApplication).lottiefilesService
53 return LottiefilesViewModel(state, service)
54 }
55
56 }
57 }
58
59 class LottiefilesFragment : BaseEpoxyFragment() {
60 private val viewModel: LottiefilesViewModel by fragmentViewModel()
61
buildModelsnull62 override fun EpoxyController.buildModels() = withState(viewModel) { state ->
63 marquee {
64 id("lottiefiles")
65 title(R.string.lottiefiles)
66 subtitle(R.string.lottiefiles_airbnb)
67 }
68
69 lottiefilesTabBar {
70 id("mode")
71 mode(state.mode)
72 recentClickListener { _ -> viewModel.setMode(LottiefilesMode.Recent) }
73 popularClickListener { _ -> viewModel.setMode(LottiefilesMode.Popular) }
74 searchClickListener { _ -> viewModel.setMode(LottiefilesMode.Search) }
75 }
76
77 if (state.mode == LottiefilesMode.Search) {
78 searchInputItemView {
79 id("search")
80 searchClickListener { viewModel.setMode(LottiefilesMode.Search, it) }
81 }
82 }
83
84 state.items.forEach {
85 val args = CompositionArgs(animationData = it)
86 animationItemView {
87 id(it.id)
88 animationData(it)
89 clickListener { _ ->
90 startActivity(PlayerActivity.intent(requireContext(), args))
91 }
92 onBind { _, _, _ -> viewModel.fetchMoreItems() }
93 }
94 }
95
96 if (state.request is Loading) {
97 loadingView {
98 id("loading")
99 onBind { _, _, _ -> viewModel.fetchMoreItems() }
100 }
101 }
102 }
103 }