New cache system

Refactoring
Separation into libraries
This commit is contained in:
2021-03-17 19:47:53 +03:00
parent 2004cb7c5e
commit 84d812a6d6
198 changed files with 4892 additions and 3477 deletions
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meloda.mvp">
</manifest>
@@ -0,0 +1,21 @@
package com.meloda.mvp
import android.app.Application
import android.os.Handler
object MvpBase {
lateinit var handler: Handler
fun init(application: Application) {
handler = Handler(application.mainLooper)
}
fun init(appHandler: Handler) {
handler = appHandler
}
fun post(runnable: Runnable) {
handler.post(runnable)
}
}
@@ -0,0 +1,11 @@
package com.meloda.mvp
object MvpConstants {
const val PEER_ID = "_peer_id"
const val ID = "_id"
const val COUNT = "_count"
const val OFFSET = "_offset"
const val FROM_CACHE = "_from_cache"
}
@@ -0,0 +1,3 @@
package com.meloda.mvp
class MvpException(var errorId: String) : Exception(errorId)
@@ -0,0 +1,30 @@
package com.meloda.mvp
import android.util.ArrayMap
import java.util.*
@Suppress("UNCHECKED_CAST")
class MvpFields {
private val fields = ArrayMap<String, Any>()
fun put(key: String, value: Any): MvpFields {
fields[key] = value
return this
}
operator fun <T> get(key: String): T {
return fields[key] as T
}
fun getNonNull(key: String): Any {
return fields[key]!!
}
fun getNonNull(`object`: Any): Any {
return Objects.requireNonNull(`object`)
}
fun getFields(): Map<String, Any> {
return fields
}
}
@@ -0,0 +1,9 @@
package com.meloda.mvp
interface MvpOnLoadListener {
fun onSuccess()
fun onError(t: Throwable)
}
@@ -0,0 +1,9 @@
package com.meloda.mvp
interface MvpOnResponseListener<T> {
fun onResponse(response: T)
fun onError(t: Throwable)
}
@@ -0,0 +1,104 @@
package com.meloda.mvp
import android.content.Context
import android.os.Bundle
@Suppress("UNCHECKED_CAST")
abstract class MvpPresenter<MainItem, Repository : MvpRepository<MainItem>, V : MvpView>(
protected var viewState: V,
private val repositoryStringClassName: String
) {
protected var context: Context? = null
protected fun requireContext(): Context {
if (context == null) throw IllegalStateException("context is null")
return context!!
}
enum class ListState {
EMPTY, EMPTY_LOADING, EMPTY_NO_INTERNET, EMPTY_ERROR, FILLED, FILLED_LOADING
}
protected var tag: String = ""
lateinit var repository: Repository
init {
initRepository()
}
private fun initRepository() {
val clazz = Class.forName(repositoryStringClassName)
this.repository = clazz.newInstance() as Repository
}
open fun onCreate(context: Context, bundle: Bundle? = null) {
this.context = context
}
open fun onCreateView(bundle: Bundle? = null) {}
open fun onViewCreated(bundle: Bundle? = null) {}
protected fun post(runnable: Runnable) {
MvpBase.post(runnable)
}
open fun destroy() {}
fun prepareViews() {
viewState.prepareNoItemsView()
viewState.prepareNoInternetView()
viewState.prepareErrorView()
}
fun setState(state: ListState) {
when (state) {
ListState.EMPTY -> {
viewState.hideRefreshLayout()
viewState.hideProgressBar()
viewState.showNoItemsView()
viewState.hideNoInternetView()
viewState.hideErrorView()
}
ListState.EMPTY_LOADING -> {
viewState.hideRefreshLayout()
viewState.showProgressBar()
viewState.hideNoItemsView()
viewState.hideNoInternetView()
viewState.hideErrorView()
}
ListState.EMPTY_NO_INTERNET -> {
viewState.hideRefreshLayout()
viewState.hideProgressBar()
viewState.hideNoItemsView()
viewState.showNoInternetView()
viewState.hideErrorView()
}
ListState.EMPTY_ERROR -> {
viewState.hideRefreshLayout()
viewState.hideProgressBar()
viewState.hideNoItemsView()
viewState.hideNoInternetView()
viewState.showErrorView()
}
ListState.FILLED -> {
viewState.hideRefreshLayout()
viewState.hideProgressBar()
viewState.hideNoItemsView()
viewState.hideNoInternetView()
viewState.hideErrorView()
}
ListState.FILLED_LOADING -> {
viewState.showRefreshLayout()
viewState.hideProgressBar()
viewState.hideNoItemsView()
viewState.hideNoInternetView()
viewState.hideErrorView()
}
}
}
}
@@ -0,0 +1,28 @@
package com.meloda.mvp
@Suppress("UNCHECKED_CAST")
abstract class MvpRepository<T> {
protected fun <Item> sendError(
listener: MvpOnResponseListener<Item>,
t: Throwable
) {
MvpBase.post { listener.onError(t) }
}
protected fun <Item> sendResponseArray(
listener: MvpOnResponseListener<ArrayList<Item>>,
response: ArrayList<Item>
) {
listener.let { MvpBase.handler.post { listener.onResponse(response) } }
}
protected fun <Item> sendResponse(
listener: MvpOnResponseListener<Item>,
response: Item
) {
listener.let {
MvpBase.handler.post { listener.onResponse(response) }
}
}
}
@@ -0,0 +1,33 @@
package com.meloda.mvp
interface MvpView {
fun showErrorSnackbar(t: Throwable)
fun prepareNoItemsView()
fun showNoItemsView()
fun hideNoItemsView()
fun prepareNoInternetView()
fun showNoInternetView()
fun hideNoInternetView()
fun prepareErrorView()
fun showErrorView()
fun hideErrorView()
fun showProgressBar()
fun hideProgressBar()
fun showRefreshLayout()
fun hideRefreshLayout()
}