Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.tatarka.bindingcollectionadapter.sample

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.get
import me.tatarka.bindingcollectionadapter.sample.databinding.PagedV3RecyclerViewBinding

class FragmentPagedV3RecyclerView : Fragment() {
private lateinit var viewModel: ImmutableViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProviders.of(this).get()
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return PagedV3RecyclerViewBinding.inflate(inflater, container, false).also {
it.setLifecycleOwner(this)
it.viewModel = viewModel
it.listeners = viewModel
it.executePendingBindings()
}.root
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package me.tatarka.bindingcollectionadapter.sample

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.paging.DataSource
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.paging.PositionalDataSource
import androidx.lifecycle.*
import androidx.paging.*
import androidx.recyclerview.widget.DiffUtil
import me.tatarka.bindingcollectionadapter2.itemBindingOf
import kotlinx.coroutines.delay
import me.tatarka.bindingcollectionadapter2.*
import me.tatarka.bindingcollectionadapter2.itembindings.OnItemBindClass
import me.tatarka.bindingcollectionadapter2.map
import me.tatarka.bindingcollectionadapter2.toItemBinding
import java.util.*
import kotlin.random.Random

class ImmutableViewModel : ViewModel(), ImmutableListeners {

Expand Down Expand Up @@ -67,7 +61,49 @@ class ImmutableViewModel : ViewModel(), ImmutableListeners {
callback.onResult(list)
}
}
}, 20).build()
}, PAGE_SIZE).build()

val pagedListV3: LiveData<PagingData<Any>> = Pager<Int, Any>(PagingConfig(
pageSize = PAGE_SIZE,
maxSize = 100,
prefetchDistance = PAGE_SIZE * 2,
enablePlaceholders = false)) {
object : PagingSource<Int, Any>() {
private val random = Random(TOTAL_COUNT)

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Any> {
val safeKey = params.key ?: 0
val list =
(0 until params.loadSize).map {
ImmutableItem(
index = it + safeKey,
checked = false
)
}
// Pretend we are slow
delay(1000)

if (random.nextBoolean()) {
return LoadResult.Error(IllegalAccessError("Error plz try again"))
}

return LoadResult.Page(
data = list,
prevKey = if (safeKey == 0) null else (safeKey - params.loadSize),
nextKey = if (safeKey >= TOTAL_COUNT - params.loadSize) null else (safeKey + params.loadSize),
itemsBefore = safeKey,
itemsAfter = TOTAL_COUNT - params.loadSize - safeKey
)
}
}
}.flow.asLiveData()

val loadStateItemBinding = LoadStateItemBindingFactory { callback: PagedListCallback ->
OnItemBindClass<LoadState>().apply {
map<LoadState.Error>(BR.item, R.layout.network_state_item_error)
map<LoadState.Loading>(BR.item, R.layout.network_state_item_progress)
}.toItemBinding().bindExtra(BR.callback, callback)
}

val items = itemBindingOf<Any>(BR.item, R.layout.item_immutable)

Expand Down Expand Up @@ -108,4 +144,9 @@ class ImmutableViewModel : ViewModel(), ImmutableListeners {
}
}
}

companion object {
private val TOTAL_COUNT = 200
private val PAGE_SIZE = 20
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MainActivity : AppCompatActivity() {
R.id.action_recyclerview -> FragmentRecyclerView()
R.id.action_diff_recyclerview -> FragmentDiffRecyclerView()
R.id.action_paged_recyclerview -> FragmentPagedRecyclerView()
R.id.action_paged_v3_recyclerview -> FragmentPagedV3RecyclerView()
R.id.action_viewpager -> FragmentViewPagerView()
R.id.action_viewpager2 -> FragmentViewPager2View()
R.id.action_spinner -> FragmentSpinnerView()
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/res/layout/network_state_item_error.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2017 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

<variable
name="item"
type="androidx.paging.LoadState.Error" />

<variable
name="callback"
type="me.tatarka.bindingcollectionadapter2.PagedListCallback" />

</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<TextView
android:id="@+id/error_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@{item.error.message}"/>

<Button
android:id="@+id/retry_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/retry"
android:onClick="@{() -> callback.retry()}"/>
</LinearLayout>

</layout>
34 changes: 34 additions & 0 deletions app/src/main/res/layout/network_state_item_progress.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2017 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />

</LinearLayout>

</layout>
35 changes: 35 additions & 0 deletions app/src/main/res/layout/paged_v3_recycler_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<import type="me.tatarka.bindingcollectionadapter.sample.ImmutableItem" />

<variable
name="viewModel"
type="me.tatarka.bindingcollectionadapter.sample.ImmutableViewModel" />

<variable
name="listeners"
type="me.tatarka.bindingcollectionadapter.sample.Listeners" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:diffConfig="@{viewModel.diff}"
app:itemBinding="@{viewModel.items}"
app:items="@{viewModel.pagedListV3}"
app:footerLoadStateItemBinding="@{viewModel.loadStateItemBinding}"
app:headerLoadStateItemBinding="@{viewModel.loadStateItemBinding}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
</layout>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
android:id="@+id/action_paged_recyclerview"
android:title="Paged RecyclerView" />

<item
android:id="@+id/action_paged_v3_recyclerview"
android:title="Paged V3 RecyclerView" />


<item
android:id="@+id/action_viewpager"
android:title="ViewPager" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<string name="action_settings">Settings</string>
<string name="open_drawer">Open Drawer</string>
<string name="close_drawer">Close Drawer</string>

<string name="retry">Retry</string>
</resources>
2 changes: 1 addition & 1 deletion bindingcollectionadapter-paging/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {

dependencies {
implementation project(':bindingcollectionadapter-recyclerview')
implementation 'androidx.paging:paging-runtime:2.0.0'
implementation 'androidx.paging:paging-runtime:3.0.0-alpha04'

testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.6.2'
Expand Down
Loading