aboutsummaryrefslogtreecommitdiff
path: root/about/src/main/kotlin/ca/allanwang/kau/about/AboutPanelDelegate.kt
blob: b6ea16b0fd730cf689f3ec7438a2589aba07ce10 (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
package ca.allanwang.kau.about

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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.*
import ca.allanwang.kau.xml.kauParseFaq
import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.fastadapter.IItem
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * 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<IItem<*, *>>?
    /**
     * The adapter, will be late initialized as it depends on configs
     */
    var adapter: FastItemThemedAdapter<IItem<*, *>>
    /**
     * 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<IItem<*, *>>? = null

    override lateinit var adapter: FastItemThemedAdapter<IItem<*, *>>

    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 == null) 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) {
        doAsync {
            with(activity) {
                items = getLibraries(if (rClass == null) Libs(activity) else Libs(this, Libs.toStringArray(rClass.fields)))
                        .map(::LibraryIItem)
                if (pageStatus[position] == 1)
                    uiThread { addItems(activity, position) }
            }
        }
    }

    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) {
            kauParseFaq(configs.faqXmlRes, configs.faqParseNewLine) {
                items = it.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)
        }
    }

}