Skip to content

Commit

Permalink
Merge pull request #128 from AlanCheen/feature/paging-support
Browse files Browse the repository at this point in the history
Feature/paging support
  • Loading branch information
AlanCheen authored Nov 14, 2022
2 parents 38b057a + 843096f commit 45f58dd
Show file tree
Hide file tree
Showing 51 changed files with 1,098 additions and 863 deletions.
22 changes: 13 additions & 9 deletions app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.core.view.get
import androidx.multidex.MultiDexApplication
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import me.yifeiyuan.flap.Flap
import me.yifeiyuan.flap.FlapInitializer
import me.yifeiyuan.flap.dsl.adapterHook
import me.yifeiyuan.flap.hook.LoggingHook
import me.yifeiyuan.flapdev.components.*
Expand Down Expand Up @@ -47,28 +47,29 @@ class FlapApplication : MultiDexApplication() {
private fun initFlap() {

val dslAdapterHook = adapterHook {
onCreateViewHolderStart { adapter, delegate, viewType ->
onCreateViewHolderStart { adapter, viewType ->

}
onCreateViewHolderEnd { adapter, delegate, viewType, component ->
onCreateViewHolderEnd { adapter, viewType, component ->

}
onBindViewHolderStart { adapter, delegate, component, data, position, payloads ->
onBindViewHolderStart { adapter, component, data, position, payloads ->

}
onBindViewHolderEnd { adapter, delegate, component, data, position, payloads ->
Log.d("dslAdapterHook", "onBindViewHolderEnd() called with: adapter = $adapter, delegate = $delegate, component = $component, data = $data, position = $position, payloads = $payloads")
onBindViewHolderEnd { adapter, component, data, position, payloads ->
Log.d("dslAdapterHook", "onBindViewHolderEnd() called with: adapter = $adapter, component = $component, data = $data, position = $position, payloads = $payloads")
}
onViewAttachedToWindow { adapter, delegate, component ->

onViewAttachedToWindow { adapter, component ->

}

onViewDetachedFromWindow { adapter, delegate, component ->
onViewDetachedFromWindow { adapter, component ->

}
}

Flap.apply {
FlapInitializer.apply {

//Flap 这里注册的都是是全局的,只是为了测试方便
//实际开发使用的话 哪个 Adapter 需要才注册更加合适。
Expand Down Expand Up @@ -101,6 +102,9 @@ class FlapApplication : MultiDexApplication() {
//可选
withContext(this@FlapApplication)

//可选
// withFallbackAdapterDelegate(YourFallbackAdapterDelegate())

//打开日志
setDebug(true)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/me/yifeiyuan/flapdev/components/Banner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fun createBannerAdapterDelegate() = adapterDelegate<BannerModel>(R.layout.compon

val viewPager2 = findViewById<ViewPager2>(R.id.banner)

val bannerAdapter = FlapAdapter().apply {
val bannerAdapter = FlapAdapter {
registerAdapterDelegate(bannerImageDelegate())
}

viewPager2.adapter = bannerAdapter

onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->
model.images?.let {
bannerAdapter.setDataAndNotify(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CustomViewTypeModel(var content: String = "自定义 itemViewType 的 Comp
const val CUSTOM_ITEM_VIEW_TYPE = 466

fun createCustomViewTypeComponentDelegate() = adapterDelegate<CustomViewTypeModel>(R.layout.flap_item_custom_type, itemViewType = CUSTOM_ITEM_VIEW_TYPE) {
onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->
bindTextView(R.id.tv_content) {
text = model.content
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ class TestDiffModel(var content: String, var id: Int, var desc: String) : IDiffe
}

fun createDiffDelegate() = adapterDelegate<TestDiffModel>(R.layout.component_diff) {
onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->

//当 payloads 更新时,事件点击需要重新设置
bindButton(R.id.modifyContent) {
setOnClickListener {
model.content = "修改后 Content:" + (SystemClock.uptimeMillis() % 10000).toInt().toString()

adapter.notifyItemChanged(position, model.content)//不会闪
// adapter.notifyItemChanged(position)//会闪
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SimpleImageModel {
}

fun createSimpleImageDelegate() = adapterDelegate<SimpleImageModel>(R.layout.component_simple_image) {
onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->
bindImageView(R.id.logo) {
if (model.url?.isNotEmpty() == true) {
Glide.with(context).load(model.url).into(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import me.yifeiyuan.flap.Component
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.ViewTypeGenerator
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flapdev.Logger
import me.yifeiyuan.flapdev.R

/**
Expand All @@ -17,7 +15,7 @@ import me.yifeiyuan.flapdev.R
* 作为文档 Demo
*/

data class SimpleTextModel(val content: String){
data class SimpleTextModel(val content: String) {
override fun toString(): String {
return "content=$content,SimpleTextModel"
}
Expand All @@ -29,18 +27,13 @@ class SimpleTextComponent(itemView: View) : Component<SimpleTextModel>(itemView)

private val tvContent: TextView = findViewById(R.id.tv_content)

override fun onBind(model: SimpleTextModel, position: Int, payloads: List<Any>, adapter: FlapAdapter, delegate: AdapterDelegate<*, *>) {
Logger.d(TAG, "onBind() called with: model = $model, position = $position, payloads = $payloads, adapter = $adapter")
override fun onBind(model: SimpleTextModel, position: Int, payloads: List<Any>) {
tvContent.text = model.content
}

override fun onBind(model: SimpleTextModel) {
Logger.d(TAG, "onBind() called with: model = $model")
}

companion object {
private const val TAG = "SimpleTextComponent"
}
}

//自定义 AdapterDelegate 实现
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fun createTestAdapterApiComponentDelegate() = adapterDelegate<TestAdapterApiMode
//展示信息
val messageTextView = findViewById<TextView>(R.id.message)

onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->

bindButton(R.id.testFireEvent) {
// fireEvent 发送事件
Expand All @@ -31,19 +31,19 @@ fun createTestAdapterApiComponentDelegate() = adapterDelegate<TestAdapterApiMode

messageTextView.text = "showToast event 收到回调:success"
}
adapter.fireEvent(showToastEvent)
fireEvent(showToastEvent)

val intE = Event("intEvent", 233333)
adapter.fireEvent(intE)
fireEvent(intE)
}
}

bindButton(R.id.testGetParam){

setOnClickListener {
val stringValue = adapter.getParam<String>("stringValue")
val intValue = adapter.getParam<Int>("intValue")
val booleanValue = adapter.getParam<Boolean>("booleanValue")
val stringValue = getParam<String>("stringValue")
val intValue = getParam<Int>("intValue")
val booleanValue = getParam<Boolean>("booleanValue")

val results :String = StringBuilder().
append("stringValue=$stringValue ;;")
Expand All @@ -58,9 +58,14 @@ fun createTestAdapterApiComponentDelegate() = adapterDelegate<TestAdapterApiMode
bindButton(R.id.testGetAdapterService) {
setOnClickListener {

val logService = adapter.getAdapterService(TestService::class.java)
logService?.log("LogService Message")
messageTextView.text = logService?.testResult()
callService<TestService> {
log("LogService Message")
messageTextView.text = testResult()
}

// val logService = adapter.getAdapterService(TestService::class.java)
// logService?.log("LogService Message")
// messageTextView.text = logService?.testResult()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import me.yifeiyuan.flapdev.R
class TestClickModel(var content: String)

fun createTestClickDelegate() = adapterDelegate<TestClickModel>(R.layout.component_test_click) {
onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->
bindTextView(R.id.clicks){
text = model.content
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestConfigModel : IDiffer {

fun createFullConfigAdapterDelegate() = adapterDelegate<TestConfigModel>(R.layout.component_full_feature) {

onBind { model, position, payloads, adapter ->
onBind { model, position, payloads ->

bindTextView(R.id.title) {
text = model.title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class ViewBindingModel

private const val TAG = "ViewBindingComponent"

fun createViewBindingDelegate() = adapterDelegateViewBinding<ViewBindingModel, FlapItemVbBinding>({ layoutInflater, parent -> FlapItemVbBinding.inflate(layoutInflater, parent, false) }
) {
fun createViewBindingDelegate() = adapterDelegateViewBinding<ViewBindingModel, FlapItemVbBinding>({ layoutInflater, parent -> FlapItemVbBinding.inflate(layoutInflater, parent, false) }) {

onBind { model ->
binding.tvContent.text = "adapterDelegateViewBinding DSL 支持 ViewBinding"
}

onClick { model, position, adapter ->
onClick { model, position ->
toast("viewBindingDelegate onClick() called with: component = $this, model = $model, position = $position")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ open class BaseTestcaseFragment : Fragment(), Scrollable, IMenuView {
emptyView = view.findViewById(R.id.emptyView)
recyclerView = view.findViewById(R.id.recyclerView)
adapter = createAdapter()
.withLifecycleOwner(viewLifecycleOwner)
adapter.withLifecycleOwner(viewLifecycleOwner)
.withEmptyView(emptyView)
.observeEvent<String>("showToast") {
toast(it.arg ?: "Default Message")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class ItemDecorationTestcase : BaseTestcaseFragment() {

recyclerView.setBackgroundColor(Color.parseColor("#16000000"))

adapter.doOnCreateViewHolderEnd { adapter, delegate, viewType, component ->
adapter.doOnCreateViewHolderEnd { adapter, viewType, component ->
val preLP = component.itemView.layoutParams
preLP.height = Random.nextInt(requireActivity().toPixel(50), requireActivity().toPixel(150))
}

adapter.doOnBindViewHolderEnd { adapter, delegate, component, data, position, payloads ->
adapter.doOnBindViewHolderEnd { adapter, component, data, position, payloads ->
if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
val index = (component.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams).spanIndex
Log.d("StaggeredGridL", "onInit() called with: position = $position, spanIndex = $index")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MultiTypeTestcase : BaseTestcaseFragment() {
}
.onlyOnce(false)
.suppressLayout(true)
.withEmptyViewHelper(adapter.emptyViewHelper)
.withEmptyViewHelper(adapter.getEmptyViewHelper())
.show()

SwipeDragHelper(adapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ViewPager2Testcase : Fragment(), Scrollable {
* java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
* at androidx.viewpager2.widget.ViewPager2$4.onChildViewAttachedToWindow(ViewPager2.java:270)
*/
doOnCreateViewHolderEnd { adapter, delegate, viewType, component ->
doOnCreateViewHolderEnd { adapter, viewType, component ->
component.itemView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.animation.Animator
import android.view.View
import android.view.animation.Interpolator
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import me.yifeiyuan.flap.Component
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flap.hook.AdapterHook

/**
Expand All @@ -23,8 +23,8 @@ abstract class BaseAdapterAnimation : AdapterHook {
var interpolator: Interpolator = LinearInterpolator()
private var lastPosition = -1

override fun onBindViewHolderEnd(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
super.onBindViewHolderEnd(adapter, delegate, component, data, position, payloads)
override fun onBindViewHolderEnd(adapter: RecyclerView.Adapter<*>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
super.onBindViewHolderEnd(adapter, component, data, position, payloads)
val adapterPosition = component.adapterPosition
if (!isFirstOnly || adapterPosition > lastPosition) {
val animator = createAnimator(component.itemView, component, data, position)
Expand Down
1 change: 1 addition & 0 deletions flap-paging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
46 changes: 46 additions & 0 deletions flap-paging/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 17
targetSdkVersion 29
versionCode 1
versionName "1.0"

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'
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

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

api project(':flap')

def paging_version = "3.1.1"

implementation "androidx.paging:paging-runtime:$paging_version"
}
Empty file added flap-paging/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions flap-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.yifeiyuan.flap.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.yifeiyuan.flap.paging.test", appContext.packageName)
}
}
Loading

0 comments on commit 45f58dd

Please sign in to comment.