Skip to content

Commit c34d5dc

Browse files
author
邹铭杰
committed
feat:Release Version 0.0.2
1 parent e37ea17 commit c34d5dc

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

README.md

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ A library that provides several functionalities to make it easy to write a list
99
# Usage
1010
To start using this library, apps need to define pager data and provide it to the list
1111
## Pager Data
12-
Apps can define the pager data for list in the ViewModel by using [easyPager](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/EasyPager.kt)
12+
Apps can define the pager data for list in the ViewModel in several ways
13+
14+
1. The simplest way is to use the [easyPager](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/EasyPager.kt).
15+
It requires the apps wrap the data in [PagingListWrapper](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/pagerconfig/PaglingListWrapper.kt) which need the data list and the hasMore sign.
1316
```kotlin
1417
@HiltViewModel
1518
class MainViewModel @Inject constructor() : ViewModel() {
@@ -27,26 +30,58 @@ class MainViewModel @Inject constructor() : ViewModel() {
2730
}
2831
}
2932
```
30-
Also, this library provides a raw version of it so that apps have the freedom to decide how to process the data with the given PagingSource.LoadParams by themselves
33+
2. However, the first method is only suitable for the simple data model. If you have a complex model, then you can use the [easyPager2](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/EasyPager2.kt)
34+
```kotlin
35+
val pager2 = easyPager2(initialKey = 0){
36+
return@easyPager2 loadData2(it)
37+
}
38+
39+
private suspend fun loadData2(page: Int): PageListVO {
40+
delay(1500)
41+
val data = mutableListOf("Page $page")
42+
repeat(20) {
43+
data.add("Item $it")
44+
}
45+
return PageListVO(page, data, page < 2)
46+
}
47+
```
48+
It requires the data model to implement the interface [IHasMoreListVO](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/pagerconfig/IHasMoreListVO.kt) to provide the list data, hasMore sign, preKey and the nextKey.
3149
```kotlin
32-
pager(pagerConfig, 0) { params ->
33-
val page = params.key ?: 0
34-
val response = try {
35-
// your own load data logic
36-
loadData(page)
37-
} catch (exception: Exception) {
38-
return@pager PagingSource.LoadResult.Error(exception)
50+
data class PageListVO(var page: Int, var items: MutableList<String>, var hasMore: Boolean) : IHasMoreListVO<Int,String> {
51+
override fun hasMore(): Boolean {
52+
return hasMore
3953
}
40-
41-
if(reponse.code == HttpCode.Error){
42-
return@pager PagingSource.LoadResult.Error()
54+
55+
override fun getList(): List<String> {
56+
return items
57+
}
58+
59+
override fun getPreKey(): Int? {
60+
return if (page - 1 < 0) null else page - 1
4361
}
4462

45-
return@pager PagingSource.LoadResult.Page(
46-
response.list,
47-
prevKey = if (page - 1 < 0) null else page - 1,
48-
nextKey = if (!response.hasMore) null else page + 1
49-
)
63+
override fun getNextKey(): Int? {
64+
return page + 1
65+
}
66+
}
67+
```
68+
3. Lastly, the library provides the [pager](https://github.com/KevinnZou/compose-pagingList/blob/main/core-paginglist/src/main/java/com/kevinnzou/compose/core/paginglist/Pager.kt) which allows the apps to map their own defined Http Result to Kotlin Standard Result. Then it will map it to PagingSource.LoadResult automatically.
69+
```kotlin
70+
pager(pagerConfig, initialKey) { key ->
71+
when (val result = loadData(key)) {
72+
is HttpResult.Error.BusinessError -> {
73+
return@pager Result.failure(Exception("${result.code} - ${result.message}"))
74+
}
75+
is HttpResult.Error.NetworkError -> {
76+
return@pager Result.failure(result.error)
77+
}
78+
is HttpResult.Success -> {
79+
return@pager Result.success(result.response)
80+
}
81+
else -> {
82+
return@pager Result.failure(Exception("Other Exception"))
83+
}
84+
}
5085
}
5186
```
5287
## List
@@ -137,6 +172,7 @@ fun <T : Any> PagingLazyColumn(
137172
retry
138173
)
139174
},
175+
emptyListContent: @Composable (() -> Unit)? = { DefaultEmptyListContent() },
140176
pagingItemContent: @Composable (index: Int, value: T?) -> Unit,
141177
)
142178
```
@@ -184,7 +220,7 @@ fun CustomRefreshingContent() {
184220
```
185221

186222
# Download
187-
The Current Release Version is 0.0.1. For future release, please refer to the release session of the github repository.
223+
The Current Release Version is 0.0.2. For future release, please refer to the release session of the github repository.
188224
``` kotlin
189225
repositories {
190226
mavenCentral()

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ android.disableAutomaticComponentCreation=true
2525

2626
GROUP_ID=io.github.kevinnzou
2727
ARTIFACT_ID=compose-paginglist
28-
VERSION_NAME=0.0.1
28+
VERSION_NAME=0.0.2
2929

3030

3131
POM_DESCRIPTION=Pull To Refresh layout for Jetpack Compose

0 commit comments

Comments
 (0)