Skip to content

Commit

Permalink
added support for no data found
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh Paliyal <paliyalyogesh@gmail.com>
  • Loading branch information
yogeshpaliyal committed May 4, 2021
1 parent 8c27cba commit e00fc57
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import com.techpaliyal.androidkotlinmvvm.ui.view_model.LoadingListingViewModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.initViewModel
import com.yogeshpaliyal.universal_adapter.adapter.SectionUniversalRecyclerAdapterBuilder
import com.yogeshpaliyal.universal_adapter.adapter.UniversalAdapterViewType
import com.yogeshpaliyal.universal_adapter.adapter.UniversalRecyclerAdapter
import com.yogeshpaliyal.universal_adapter.utils.Resource
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder

class LoadingListingActivity : AppCompatActivity() {
lateinit var binding: ActivityListingBinding
Expand All @@ -36,7 +36,7 @@ class LoadingListingActivity : AppCompatActivity() {

private val mAdapter by lazy {

val header = UniversalAdapterBuilder(
val header = UniversalRecyclerAdapter.Builder(
lifecycleOwner = this,
content = UniversalAdapterViewType.Content(
R.layout.item_simple,
Expand All @@ -51,7 +51,7 @@ class LoadingListingActivity : AppCompatActivity() {
})
)

val content = UniversalAdapterBuilder<UserModel>(R.layout.item_user,
val content = UniversalRecyclerAdapter.Builder<UserModel>(R.layout.item_user,
resourceLoading = R.layout.layout_loading_full_page,
defaultLoadingItems = 1,
mListener = object : BasicListener<UserModel> {
Expand All @@ -61,7 +61,8 @@ class LoadingListingActivity : AppCompatActivity() {
}
})

SectionUniversalRecyclerAdapterBuilder<UserModel, BasicModel, Unit>(content, header)

SectionUniversalRecyclerAdapterBuilder<UserModel, BasicModel, Unit>(content, header, null)
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.yogeshpaliyal.universal_adapter.adapter

import androidx.recyclerview.widget.ConcatAdapter
import com.yogeshpaliyal.universal_adapter.utils.Resource
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder


/*
Expand All @@ -11,12 +10,19 @@ import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder
* https://techpaliyal.com
* created on 08-01-2021 19:47
*/

class SectionUniversalRecyclerAdapterBuilder<X : Any, Y : Any, Z : Any>(
contentOptions: UniversalAdapterBuilder<X>? = null,
headerOptions: UniversalAdapterBuilder<Y>? = null,
footerOptions: UniversalAdapterBuilder<Z>? = null
class SectionUniversalRecyclerAdapterBuilder<X : Any, Y : Any, Z : Any> constructor(
contentOptions: UniversalRecyclerAdapter<X>? = null,
headerOptions: UniversalRecyclerAdapter<Y>? = null,
footerOptions: UniversalRecyclerAdapter<Z>? = null
) {


constructor(
contentBuilder: UniversalRecyclerAdapter.Builder<X>? = null,
headerBuilder: UniversalRecyclerAdapter.Builder<Y>? = null,
footerBuilder: UniversalRecyclerAdapter.Builder<Z>? = null
) : this(contentBuilder?.build(), headerBuilder?.build(), footerBuilder?.build())

val concatedAdapter by lazy {
val ada = ConcatAdapter()
headerAdapter?.let {
Expand All @@ -43,15 +49,15 @@ class SectionUniversalRecyclerAdapterBuilder<X : Any, Y : Any, Z : Any>(

init {
if (headerOptions != null) {
headerAdapter = UniversalRecyclerAdapter(headerOptions)
headerAdapter = headerOptions
}

if (contentOptions != null) {
contentAdapter = UniversalRecyclerAdapter(contentOptions)
contentAdapter = contentOptions
}

if (footerOptions != null) {
footerAdapter = UniversalRecyclerAdapter(footerOptions)
footerAdapter = footerOptions
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sealed class UniversalAdapterViewType {
)
val noDataListener: Any? = null,
val listener: Any? = null,
val customBindingMapping: ((itemBinding: ViewDataBinding, item: T) -> Unit)? = null
val customBindingMapping: ((itemBinding: ViewDataBinding, item: String?) -> Unit)? = null
)

data class Error<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder
* @author Yogesh Paliyal
* Created Date : 15 October 2020
*/
class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapterBuilder<T>) {
class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: Builder<T>) {
// var adapterOptions : UniversalAdapterOptions<T> = UniversalAdapterOptions(resource, resourceLoading, defaultLoadingItems, loaderFooter, data, errorLayout, errorListener, mListener, noDataLayout, noDataListener, lifecycleOwner)


Expand Down Expand Up @@ -51,7 +51,7 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt
lifecycleOwner: LifecycleOwner? = null
) :
this(
UniversalAdapterBuilder<T>(
Builder<T>(
lifecycleOwner,
data,
UniversalAdapterViewType.Content(resource, mListener),
Expand All @@ -73,6 +73,7 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt

private var contentAdapter: ContentListAdapter<T>? = null
private var loadingAdapter: LoadingAdapter<T>? = null
private var noDataFound: ErrorAdapter<T>? = null // to support only message
private var loadMoreAdapter: LoadingAdapter<T>? = null
private var errorAdapter: ErrorAdapter<T>? = null

Expand Down Expand Up @@ -104,6 +105,14 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt
listener = adapterBuilder.error.listener,
customBinding = adapterBuilder.error.customBindingMapping
)

if (adapterBuilder.noData?.noDataLayout != null)
noDataFound = ErrorAdapter(
adapterBuilder.lifecycleOwner,
adapterBuilder.noData.noDataLayout,
listener = adapterBuilder.noData.listener,
customBinding = adapterBuilder.noData.customBindingMapping
)
}

fun updateData(data: Resource<List<T>?>) {
Expand All @@ -114,23 +123,24 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt
fun getLoadMoreAdapter() = loadMoreAdapter
fun getLoadingAdapter() = loadingAdapter
fun getErrorAdapter() = errorAdapter
fun getNoDataAdapter() = noDataFound

fun getAdapter() = mainAdapter

private fun setupAdapters(data: Resource<List<T>?>) {
when (data.status) {
Status.LOADING -> {
// if has data then data + loading else loading
noDataFound?.let { mainAdapter.removeAdapter(it) }
errorAdapter?.let { mainAdapter.removeAdapter(it) }
if (data.data.isNullOrEmpty()) {
// add only loading state
contentAdapter?.let { mainAdapter.removeAdapter(it) }
loadMoreAdapter?.let { mainAdapter.removeAdapter(it) }
errorAdapter?.let { mainAdapter.removeAdapter(it) }
if (isContains(loadingAdapter).not())
loadingAdapter?.let { mainAdapter.addAdapter(it) }
} else {
loadingAdapter?.let { mainAdapter.removeAdapter(it) }
errorAdapter?.let { mainAdapter.removeAdapter(it) }
if (isContains(contentAdapter).not())
contentAdapter?.let { mainAdapter.addAdapter(it) }
contentAdapter?.submitList(data.data)
Expand All @@ -144,14 +154,27 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt
loadMoreAdapter?.let { mainAdapter.removeAdapter(it) }
errorAdapter?.let { mainAdapter.removeAdapter(it) }

if (isContains(contentAdapter).not())
contentAdapter?.let { mainAdapter.addAdapter(it) }
contentAdapter?.submitList(data.data)
// check if there is data or note
if (data.data.isNullOrEmpty()) {
// show no data found screen
contentAdapter?.let { mainAdapter.removeAdapter(it) }

if (isContains(noDataFound).not())
noDataFound?.let { mainAdapter.addAdapter(it) }
noDataFound?.submitList(data.data)
} else {
noDataFound?.let { mainAdapter.removeAdapter(it) }

if (isContains(contentAdapter).not())
contentAdapter?.let { mainAdapter.addAdapter(it) }
contentAdapter?.submitList(data.data)
}
}
Status.ERROR -> {
// if has data then data + error else error
loadMoreAdapter?.let { mainAdapter.removeAdapter(it) }
errorAdapter?.let { mainAdapter.removeAdapter(it) }
noDataFound?.let { mainAdapter.removeAdapter(it) }
if (data.data.isNullOrEmpty()) {

contentAdapter?.let { mainAdapter.removeAdapter(it) }
Expand Down Expand Up @@ -187,4 +210,53 @@ class UniversalRecyclerAdapter<T> constructor(val adapterBuilder: UniversalAdapt
}
}

data class Builder<T> constructor(
var lifecycleOwner: LifecycleOwner? = null,
var data: Resource<List<T>?>? = null,
val content: UniversalAdapterViewType.Content<T>? = null,
val loading: UniversalAdapterViewType.Loading<T>? = null,
val loadingFooter: UniversalAdapterViewType.LoadingFooter<T>? = null,
val noData: UniversalAdapterViewType.NoData<T>? = null,
val error: UniversalAdapterViewType.Error<T>? = null
) {
constructor(
@LayoutRes
resource: Int,
@LayoutRes
resourceLoading: Int? = null,
defaultLoadingItems: Int = 5,
@LayoutRes
loaderFooter: Int? = null,
data: Resource<ArrayList<T>?>? = null,
@LayoutRes
errorLayout: Int? = null,
errorListener: Any? = null,
mListener: Any? = null,
@LayoutRes
noDataLayout: Int? = null,
noDataListener: Any? = null,
lifecycleOwner: LifecycleOwner? = null
) : this(
lifecycleOwner,
data,
UniversalAdapterViewType.Content(resource, mListener),
UniversalAdapterViewType.Loading(resourceLoading, defaultLoadingItems),
UniversalAdapterViewType.LoadingFooter(loaderFooter),
UniversalAdapterViewType.NoData(noDataLayout, noDataListener),
UniversalAdapterViewType.Error(errorLayout, errorListener)
)

constructor(oldBuilder: UniversalAdapterBuilder<T>) : this(
oldBuilder.lifecycleOwner,
oldBuilder.data,
oldBuilder.content,
oldBuilder.loading,
oldBuilder.loadingFooter,
oldBuilder.noData,
oldBuilder.error
)

fun build() = UniversalRecyclerAdapter(this)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.yogeshpaliyal.universal_adapter.adapter

import androidx.recyclerview.widget.ConcatAdapter
import com.yogeshpaliyal.universal_adapter.utils.Resource
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder


/*
Expand All @@ -20,9 +19,9 @@ import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder
)
)
class UniversalRecyclerAdapterHelper<X : Any, Y : Any, Z : Any>(
contentOptions: UniversalAdapterBuilder<X>? = null,
headerOptions: UniversalAdapterBuilder<Y>? = null,
footerOptions: UniversalAdapterBuilder<Z>? = null
contentOptions: UniversalRecyclerAdapter.Builder<X>? = null,
headerOptions: UniversalRecyclerAdapter.Builder<Y>? = null,
footerOptions: UniversalRecyclerAdapter.Builder<Z>? = null
){
val concatedAdapter by lazy {
val ada= ConcatAdapter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ open class UniversalAdapterBuilder<T> constructor(
UniversalAdapterViewType.Error(errorLayout, errorListener)
)

fun build() = UniversalRecyclerAdapter(this)
fun build() = UniversalRecyclerAdapter.Builder(this).build()
}

0 comments on commit e00fc57

Please sign in to comment.