/*
* 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 .
*/
package com.pitchedapps.frost.fragments
import ca.allanwang.kau.adapters.fastAdapter
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.IItem
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.fastadapter.adapters.ModelAdapter
import com.mikepenz.fastadapter_extensions.items.ProgressItem
import com.pitchedapps.frost.R
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.parsers.FrostParser
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 org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
/**
* Created by Allan Wang on 27/12/17.
*/
abstract class RecyclerFragment : BaseFragment(), RecyclerContentContract {
override val layoutRes: Int = R.layout.view_content_recycler
override fun firstLoadRequest() {
val core = core ?: return
if (firstLoad) {
core.reloadBase(true)
firstLoad = false
}
}
final override fun reload(progress: (Int) -> Unit, callback: (Boolean) -> Unit) {
reloadImpl(progress) {
if (it)
callback(it)
else
valid = false
}
}
protected abstract fun reloadImpl(progress: (Int) -> Unit, callback: (Boolean) -> Unit)
}
abstract class GenericRecyclerFragment> : RecyclerFragment() {
abstract fun mapper(data: T): Item
val adapter: ModelAdapter = 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(): FastAdapter> = fastAdapter(this.adapter)
}
abstract class FrostParserFragment> : RecyclerFragment() {
/**
* The parser to make this all happen
*/
abstract val parser: FrostParser
open fun getDoc(cookie: String?) = frostJsoup(cookie, parser.url)
abstract fun toItems(response: ParseResponse): List-
val adapter: ItemAdapter
- = 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(): FastAdapter> = fastAdapter(this.adapter)
override fun reloadImpl(progress: (Int) -> Unit, callback: (Boolean) -> Unit) {
doAsync {
progress(10)
val cookie = FbCookie.webCookie
val doc = getDoc(cookie)
progress(60)
val response = parser.parse(cookie, doc)
if (response == null) {
L.i { "RecyclerFragment failed for ${baseEnum.name}" }
return@doAsync callback(false)
}
progress(80)
val items = toItems(response)
progress(97)
uiThread { adapter.setNewList(items) }
callback(true)
}
}
}
//abstract class PagedRecyclerFragment> : RecyclerFragment() {
//
// var allowPagedLoading = true
//
// val footerAdapter = ItemAdapter()
//
// val footerScrollListener = object : EndlessRecyclerOnScrollListener(footerAdapter) {
// override fun onLoadMore(currentPage: Int) {
// TODO("not implemented")
//
// }
//
// }
//
// override fun getAdapter() = fastAdapter(adapter, footerAdapter)
//
// override fun bindImpl(recyclerView: FrostRecyclerView) {
// recyclerView.addOnScrollListener(footerScrollListener)
// }
//
// override fun reload(progress: (Int) -> Unit, callback: (Boolean) -> Unit) {
// footerScrollListener.
// super.reload(progress, callback)
// }
//}
class FrostProgress : ProgressItem()