-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13d1a67
commit fa75d5e
Showing
15 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
paging/src/androidTest/java/me/twc/source/observer/paging/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
paging/src/main/java/me/twc/source/observer/paging/IPagination.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
paging/src/main/java/me/twc/source/observer/paging/ObserverUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
paging/src/test/java/me/twc/source/observer/paging/ExampleUnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ include ':source' | |
include ':app' | ||
rootProject.name = "RetrofitSource" | ||
include ':observer' | ||
include ':paging' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters