aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/fragments/RecyclerFragmentBase.kt
blob: 51beab933e6d4b26301978a6c7ca27f0fefca900 (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
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.fragments

import ca.allanwang.kau.adapters.fastAdapter
import ca.allanwang.kau.utils.withMainContext
import com.mikepenz.fastadapter.GenericFastAdapter
import com.mikepenz.fastadapter.GenericItem
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.fastadapter.adapters.ModelAdapter
import com.pitchedapps.frost.R
import com.pitchedapps.frost.facebook.parsers.FrostParser
import com.pitchedapps.frost.facebook.parsers.ParseData
import com.pitchedapps.frost.facebook.parsers.ParseResponse
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.frostJsoup
import com.pitchedapps.frost.views.FrostRecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * Created by Allan Wang on 27/12/17.
 */
abstract class RecyclerFragment<T, Item : GenericItem> : BaseFragment(), RecyclerContentContract {

    override val layoutRes: Int = R.layout.view_content_recycler

    abstract val adapter: ModelAdapter<T, Item>

    override fun firstLoadRequest() {
        val core = core ?: return
        if (firstLoad) {
            core.reloadBase(true)
            firstLoad = false
        }
    }

    final override suspend fun reload(progress: (Int) -> Unit): Boolean =
        withContext(Dispatchers.IO) {
            val data = try {
                reloadImpl(progress)
            } catch (e: Exception) {
                L.e(e) { "Recycler reload fail $baseUrl" }
                null
            }
            withMainContext {
                if (data == null) {
                    valid = false
                    false
                } else {
                    adapter.setNewList(data)
                    true
                }
            }
        }

    protected abstract suspend fun reloadImpl(progress: (Int) -> Unit): List<T>?
}

abstract class GenericRecyclerFragment<T, Item : GenericItem> : RecyclerFragment<T, Item>() {

    abstract fun mapper(data: T): Item

    override val adapter: ModelAdapter<T, Item> = ModelAdapter { this.mapper(it) }

    final override fun bind(recyclerView: FrostRecyclerView) {
        recyclerView.adapter = getAdapter()
        recyclerView.onReloadClear = { adapter.clear() }
        bindImpl(recyclerView)
    }

    /**
     * Anything to call for one time bindings
     * At this stage, all adapters will have FastAdapter references
     */
    open fun bindImpl(recyclerView: FrostRecyclerView) = Unit

    /**
     * Create the fast adapter to bind to the recyclerview
     */
    open fun getAdapter(): GenericFastAdapter = fastAdapter(this.adapter)
}

abstract class FrostParserFragment<T : ParseData, Item : GenericItem> :
    RecyclerFragment<Item, Item>() {

    /**
     * The parser to make this all happen
     */
    abstract val parser: FrostParser<T>

    open fun getDoc(cookie: String?) = frostJsoup(cookie, parser.url)

    abstract fun toItems(response: ParseResponse<T>): List<Item>

    override val adapter: ItemAdapter<Item> = ItemAdapter()

    final override fun bind(recyclerView: FrostRecyclerView) {
        recyclerView.adapter = getAdapter()
        recyclerView.onReloadClear = { adapter.clear() }
        bindImpl(recyclerView)
    }

    /**
     * Anything to call for one time bindings
     * At this stage, all adapters will have FastAdapter references
     */
    open fun bindImpl(recyclerView: FrostRecyclerView) = Unit

    /**
     * Create the fast adapter to bind to the recyclerview
     */
    open fun getAdapter(): GenericFastAdapter = fastAdapter(this.adapter)

    override suspend fun reloadImpl(progress: (Int) -> Unit): List<Item>? =
        withContext(Dispatchers.IO) {
            progress(10)
            val cookie = fbCookie.webCookie
            val doc = getDoc(cookie)
            progress(60)
            val response = try {
                parser.parse(cookie, doc)
            } catch (ignored: Exception) {
                null
            }
            if (response == null) {
                L.i { "RecyclerFragment failed for ${baseEnum.name}" }
                L._d { "Cookie used: $cookie" }
                return@withContext null
            }
            progress(80)
            val items = toItems(response)
            progress(97)
            return@withContext items
        }
}