aboutsummaryrefslogtreecommitdiff
path: root/about/src/main/kotlin/ca/allanwang/kau/about/AboutPanelDelegate.kt
blob: 1d9b2b0793d0484d3210898e13771817fc82ae99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.about

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import ca.allanwang.kau.adapters.FastItemThemedAdapter
import ca.allanwang.kau.animators.FadeScaleAnimatorAdd
import ca.allanwang.kau.animators.KauAnimator
import ca.allanwang.kau.animators.NoAnimatorChange
import ca.allanwang.kau.iitems.HeaderIItem
import ca.allanwang.kau.utils.AnimHolder
import ca.allanwang.kau.utils.KAU_BOTTOM
import ca.allanwang.kau.utils.colorToForeground
import ca.allanwang.kau.utils.drawable
import ca.allanwang.kau.utils.fullLinearRecycler
import ca.allanwang.kau.utils.postDelayed
import ca.allanwang.kau.utils.string
import ca.allanwang.kau.utils.withMarginDecoration
import ca.allanwang.kau.xml.kauParseFaq
import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.fastadapter.GenericItem
import java.lang.reflect.Field
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Created by Allan Wang on 2017-08-02.
 *
 * The core logic for pages used in [AboutActivityBase]
 */
interface AboutPanelContract {
    /**
     * Model list to be added to [adapter]
     */
    var items: List<GenericItem>
    /**
     * The adapter, will be late initialized as it depends on configs
     */
    var adapter: FastItemThemedAdapter<GenericItem>
    /**
     * Reference to the recyclerview, will be used to stop scrolling upon exit
     */
    var recycler: RecyclerView?

    /**
     * The base inflation method that will be called for new pages from the page adapter
     * Keep in mind that when inflating, do NOT add the view to the viewgroup
     * Use layoutInflater.inflate(id, parent, false)
     */
    fun inflatePage(activity: AboutActivityBase, parent: ViewGroup, position: Int): View

    /**
     * Convenience method called during [inflatePage]
     * No return value necessary
     */
    fun onInflatingPage(activity: AboutActivityBase, recycler: RecyclerView, position: Int)

    /**
     * Triggers start of item loading
     * Typically called with [inflatePage]
     */
    fun loadItems(activity: AboutActivityBase, position: Int)

    /**
     * Called when the  [adapter] should take in the items
     * This typically happens once the user has scroll to the page,
     * so they may see a transition
     *
     * [AboutActivityBase.pageStatus] should be updated accordingly,
     * as triggering this does not necessarily mean that the items are added
     */
    fun addItems(activity: AboutActivityBase, position: Int)
}

abstract class AboutPanelRecycler : AboutPanelContract {

    override var items: List<GenericItem> = emptyList()

    override lateinit var adapter: FastItemThemedAdapter<GenericItem>

    override var recycler: RecyclerView? = null

    override fun onInflatingPage(
        activity: AboutActivityBase,
        recycler: RecyclerView,
        position: Int
    ) {
        recycler.adapter = adapter
        recycler.itemAnimator = KauAnimator(
            addAnimator = FadeScaleAnimatorAdd(scaleFactor = 0.7f, itemDelayFactor = 0.2f),
            changeAnimator = NoAnimatorChange
        ).apply {
            addDuration = 300; interpolator = AnimHolder.decelerateInterpolator(recycler.context)
        }
    }

    override fun inflatePage(activity: AboutActivityBase, parent: ViewGroup, position: Int): View {
        val v = LayoutInflater.from(activity)
            .inflate(R.layout.kau_recycler_detached_background, parent, false)
        adapter = FastItemThemedAdapter(activity.configs)
        recycler = v.findViewById(R.id.kau_recycler_detached)
        onInflatingPage(activity, recycler!!, position)
        val background = v.findViewById<View>(R.id.kau_recycler_detached_background)
        if (activity.configs.backgroundColor != null) background.setBackgroundColor(activity.configs.backgroundColor!!.colorToForeground())
        loadItems(activity, position)
        return v
    }

    override fun addItems(activity: AboutActivityBase, position: Int) {
        if (items.isEmpty()) {
            return
        }
        activity.pageStatus[position] = 2
        postDelayed(300) { addItemsImpl(activity, position) }
    }

    abstract fun addItemsImpl(activity: AboutActivityBase, position: Int)
}

/**
 * Panel delegate for the main page
 * The loading is synchronous, so it is done on inflation
 * All other loading and adding methods are overridden to do nothing
 * There is a [AboutActivityBase.postInflateMainPage] hook that can be overridden
 * to update this panel without extending the whole delegate
 */
open class AboutPanelMain : AboutPanelRecycler() {

    override fun onInflatingPage(
        activity: AboutActivityBase,
        recycler: RecyclerView,
        position: Int
    ) {
    }

    override fun inflatePage(activity: AboutActivityBase, parent: ViewGroup, position: Int): View {
        with(activity) {
            adapter = FastItemThemedAdapter(configs)
            recycler = fullLinearRecycler(adapter)
            adapter.add(CutoutIItem {
                with(configs) {
                    text = string(cutoutTextRes, cutoutText)
                    drawable = drawable(cutoutDrawableRes, cutoutDrawable)
                    if (configs.cutoutForeground != null) foregroundColor =
                        configs.cutoutForeground!!
                }
            }.apply {
                themeEnabled = configs.cutoutForeground == null
            })
            postInflateMainPage(adapter)
            return recycler!!
        }
    }

    override fun loadItems(activity: AboutActivityBase, position: Int) {}
    override fun addItems(activity: AboutActivityBase, position: Int) {
        activity.pageStatus[position] = 2
    }

    override fun addItemsImpl(activity: AboutActivityBase, position: Int) {}
}

/**
 * Panel for loading libraries
 * There is a [AboutActivityBase.getLibraries] hook that can be overridden
 * to customize the libraries listed
 */
open class AboutPanelLibs : AboutPanelRecycler() {

    override fun onInflatingPage(
        activity: AboutActivityBase,
        recycler: RecyclerView,
        position: Int
    ) {
        super.onInflatingPage(activity, recycler, position)
        recycler.withMarginDecoration(16, KAU_BOTTOM)
        LibraryIItem.bindEvents(adapter)
    }

    override fun loadItems(activity: AboutActivityBase, position: Int) {
        with(activity) {
            launch {
                items = withContext(Dispatchers.Default) {
                    getLibraries(
                        if (rClass == null) Libs(activity)
                        else Libs(activity, rClass.fields.toStringArray())
                    ).map(::LibraryIItem)
                }
                if (pageStatus[position] == 1)
                    addItems(activity, position)
            }
        }
    }

    // TODO remove and use from AboutLibrary
    // https://github.com/mikepenz/AboutLibraries/issues/449
    private fun Array<Field>.toStringArray(): Array<String> =
        map { it.name }.filter { it.contains("define_") }.toTypedArray()

    override fun addItemsImpl(activity: AboutActivityBase, position: Int) {
        with(activity.configs) {
            adapter.add(HeaderIItem(text = libPageTitle, textRes = libPageTitleRes))
                .add(items)
        }
    }
}

open class AboutPanelFaqs : AboutPanelRecycler() {

    override fun onInflatingPage(
        activity: AboutActivityBase,
        recycler: RecyclerView,
        position: Int
    ) {
        super.onInflatingPage(activity, recycler, position)
        FaqIItem.bindEvents(adapter)
    }

    override fun loadItems(activity: AboutActivityBase, position: Int) {
        with(activity) {
            launch {
                items = withContext(Dispatchers.IO) {
                    kauParseFaq(
                        configs.faqXmlRes,
                        configs.faqParseNewLine
                    )
                }.map(::FaqIItem)
                if (pageStatus[position] == 1)
                    addItems(activity, position)
            }
        }
    }

    override fun addItemsImpl(activity: AboutActivityBase, position: Int) {
        with(activity.configs) {
            adapter.add(HeaderIItem(text = faqPageTitle, textRes = faqPageTitleRes))
                .add(items)
        }
    }
}