Skip to content

Commit

Permalink
支持分页数据观察
Browse files Browse the repository at this point in the history
  • Loading branch information
tangwanchao committed Sep 22, 2022
1 parent 13d1a67 commit fa75d5e
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 2 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion observer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.tangwanchao'
artifactId = 'source-observer'
version = '1.1.4'
version = '2.0.0'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ interface ISourceObserverView {
*/
fun showSourceErrorView()

/**
* 展示空数据 ui
*/
fun showSourceEmptyView()

/**
* 展示成功 ui
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ abstract class SourceObserverView @JvmOverloads constructor(
showRetry()
}

override fun showSourceEmptyView() {
showEmpty()
}

override fun showSourceSuccessView() {
showContent()
}
Expand Down
1 change: 1 addition & 0 deletions paging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
71 changes: 71 additions & 0 deletions paging/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'maven-publish'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

// 用于打包源代码的任务
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
artifact androidSourcesJar
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.tangwanchao'
artifactId = 'source-paging'
version = '2.0.0'
}
}
}
}

dependencies {

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'

compileOnly 'com.scwang.smart:refresh-layout-kernel:2.0.3'

compileOnly project(path: ':source')
compileOnly project(path: ':observer')
}
Empty file added paging/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions paging/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.twc.source.observer.paging

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("me.twc.source.observer.paging.test", appContext.packageName)
}
}
5 changes: 5 additions & 0 deletions paging/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.twc.source.observer.paging">

</manifest>
20 changes: 20 additions & 0 deletions paging/src/main/java/me/twc/source/observer/paging/IPagination.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.twc.source.observer.paging

/**
* @author 唐万超
* @date 2022/09/22
*/
interface IPagination<T> {
/**
* 是否有更多数据
*
* @return [true : 没有更多数据]
* [false: 有更多数据]
*/
fun noMoreData():Boolean

/**
* 获取分页的数据
*/
fun getPagingDataList():List<T>
}
64 changes: 64 additions & 0 deletions paging/src/main/java/me/twc/source/observer/paging/ObserverUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.twc.source.observer.paging

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.twc.source.ErrorSource
import me.twc.source.LoadingSource
import me.twc.source.Source
import me.twc.source.SuccessSource
import me.twc.source.observer.engine.ISourceObserverView

/**
* 配合 [SmartRefreshLayout] 刷新观察
*/
fun <T> LiveData<Source<IPagination<T>>>.paginationRefreshObserver(
owner: LifecycleOwner,
refreshLayout: SmartRefreshLayout,
observerView: ISourceObserverView,
block: (list: List<T>) -> Unit
) = this.observe(owner) { source ->
when (source) {
LoadingSource -> {
refreshLayout.autoRefreshAnimationOnly()
refreshLayout.setEnableLoadMore(false)
}
is ErrorSource -> {
refreshLayout.finishRefresh(false)
observerView.showSourceErrorView()
}
is SuccessSource -> {
val pagination = source.data
refreshLayout.finishRefresh(0, true, pagination.noMoreData())
val data = pagination.getPagingDataList()
if (data.isEmpty()) {
observerView.showSourceEmptyView()
} else {
refreshLayout.setEnableLoadMore(true)
observerView.showSourceSuccessView()
block(data)
}
}
}
}

/**
* 配合 [SmartRefreshLayout] 加载更多观察
*/
fun <T> LiveData<Source<IPagination<T>>>.paginationLoadMoreObserver(
owner: LifecycleOwner,
refreshLayout: SmartRefreshLayout,
block: (list: List<T>) -> Unit
) = this.observe(owner) { source ->
when (source) {
LoadingSource -> Unit
is ErrorSource -> {
refreshLayout.finishLoadMore(0, false, false)
}
is SuccessSource -> {
val pagination = source.data
refreshLayout.finishLoadMore(0, true, pagination.noMoreData())
block(pagination.getPagingDataList())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.twc.source.observer.paging

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include ':source'
include ':app'
rootProject.name = "RetrofitSource"
include ':observer'
include ':paging'
2 changes: 1 addition & 1 deletion source/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.tangwanchao'
artifactId = 'retrofit-source'
version = '1.1.4'
version = '2.0.0'
}
}
}
Expand Down

0 comments on commit fa75d5e

Please sign in to comment.