Skip to content

Commit

Permalink
Merge pull request #101 from AlanCheen/feature/service-manager
Browse files Browse the repository at this point in the history
Feature/service manager
  • Loading branch information
AlanCheen authored Aug 18, 2022
2 parents 4d0edc1 + 603e143 commit 247fa95
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 25 deletions.
21 changes: 21 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/Services.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.yifeiyuan.flapdev

import android.util.Log
import me.yifeiyuan.flap.service.AdapterService

/**
* Created by 程序亦非猿 on 2022/8/16.
*/

private const val TAG = "Services"

class TestService : AdapterService {

fun log(message: String){
Log.d(TAG, "log() called with: message = $message")
}

fun testResult():String{
return "TestService.testResult is called!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import me.yifeiyuan.flap.annotations.Delegate
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flap.event.Event
import me.yifeiyuan.flap.ext.bindButton
import me.yifeiyuan.flapdev.TestService
import me.yifeiyuan.flapdev.R
import kotlin.text.StringBuilder

Expand Down Expand Up @@ -59,6 +60,19 @@ class TestAllComponent(view: View) : Component<TestAllModel>(view) {
messageTextView.text = "Adapter.getParam results=$results"
}
}

bindButton(R.id.testGetAdapterService) {
setOnClickListener {

val logService = adapter.getAdapterService(TestService::class.java)
logService?.log("LogService Message")

val logService2 = adapter.getAdapterService<TestService>("LogService")

val result = logService2?.testResult()
messageTextView.text = result
}
}
}

override fun onBind(model: TestAllModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.ext.ExtraParamsProvider
import me.yifeiyuan.flap.skeleton.Skeleton
import me.yifeiyuan.flap.widget.FlapRecyclerView
import me.yifeiyuan.flapdev.TestService
import me.yifeiyuan.flapdev.R
import me.yifeiyuan.flapdev.Scrollable
import me.yifeiyuan.flapdev.components.SimpleTextModel
Expand Down Expand Up @@ -122,6 +122,9 @@ open class BaseTestcaseFragment : Fragment(), Scrollable {
}
}

adapter.registerAdapterService(TestService::class.java)
adapter.registerAdapterService("LogService",TestService::class.java)

//配置完结束最后再赋值
recyclerView.adapter = adapter

Expand Down
27 changes: 23 additions & 4 deletions app/src/main/res/layout/component_test_all_feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4D4DD0E1"
android:orientation="vertical"
android:paddingHorizontal="4dp"
android:paddingVertical="8dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="4dp"
android:text="测试 FlapAdapter 各种 API"
android:textColor="#111111" />

<TextView
android:id="@+id/message"
android:layout_width="match_parent"
Expand All @@ -38,7 +47,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="FlapAdapter.fireEvent: 发送事件"
android:text="#fireEvent(): 发送事件"
android:textAllCaps="false"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
Expand All @@ -49,13 +58,23 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="FlapAdapter.getParam: 获取额外参数"
android:text="#getParam(): 获取额外参数"
android:textAllCaps="false"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/testFireEvent" />

</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/testGetAdapterService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#getAdapterService(): 获取服务"
android:textAllCaps="false"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/testGetParam" />
</LinearLayout>


</androidx.cardview.widget.CardView>
Expand Down
32 changes: 30 additions & 2 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package me.yifeiyuan.flap

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -17,6 +16,9 @@ import me.yifeiyuan.flap.ext.*
import me.yifeiyuan.flap.hook.AdapterHook
import me.yifeiyuan.flap.hook.PreloadHook
import me.yifeiyuan.flap.pool.ComponentPool
import me.yifeiyuan.flap.service.AdapterService
import me.yifeiyuan.flap.service.IAdapterServiceManager
import me.yifeiyuan.flap.service.AdapterServiceManager

/**
* FlapAdapter is a flexible and powerful Adapter that makes you enjoy developing with RecyclerView.
Expand All @@ -29,7 +31,7 @@ import me.yifeiyuan.flap.pool.ComponentPool
* @since 2020/9/22
* @since 3.0.0
*/
open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry {
open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry , IAdapterServiceManager {

companion object {
private const val TAG = "FlapAdapter"
Expand Down Expand Up @@ -102,6 +104,8 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry {
lateinit var bindingRecyclerView: RecyclerView
lateinit var bindingContext: Context

private val serviceManager = AdapterServiceManager()

init {
adapterHooks.addAll(Flap.globalHooks)
adapterDelegates.addAll(Flap.globalAdapterDelegates)
Expand Down Expand Up @@ -509,6 +513,30 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry {
emptyViewHelper.emptyView = emptyView
}

override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>) {
serviceManager.registerAdapterService(serviceClass)
}

override fun <T : AdapterService> registerAdapterService(clazz: Class<T>, service: T) {
serviceManager.registerAdapterService(clazz, service)
}

override fun <T : AdapterService> getAdapterService(clazz: Class<T>): T? {
return serviceManager.getAdapterService(clazz)
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) {
serviceManager.registerAdapterService(serviceName, serviceClass)
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, service: T) {
serviceManager.registerAdapterService(serviceName, service)
}

override fun <T : AdapterService> getAdapterService(serviceName: String): T? {
return serviceManager.getAdapterService(serviceName)
}

/**
* 当 Adapter.data 中存在一个 Model 没有对应的 AdapterDelegate.delegate()==true 时抛出
*/
Expand Down
2 changes: 1 addition & 1 deletion flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private const val TAG = "ComponentPool"
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
* @author 程序亦非猿 [Follow me](<a> https://github.com/AlanCheen</a>)
* @since 2020/9/22
* @since 3.0.1
* @since 3.0.2
*/
open class ComponentPool : RecycledViewPool(), ComponentCallbacks2 {

Expand Down
39 changes: 22 additions & 17 deletions flap/src/main/java/me/yifeiyuan/flap/pool/RecyclerViewDumpHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import java.util.*
* Dump RecyclerView 的缓存情况
*
* Created by 程序亦非猿 on 2022/8/15.
*
* @since 3.0.2
*/

private const val TAG = "RecyclerViewDumpHelper"
Expand Down Expand Up @@ -143,7 +145,7 @@ class RecyclerMediator(val recyclerView: RecyclerView) {
.appendLine("mCachedViews:")
.appendLine("mCachedViews.size=${mCachedViews?.size}")
.appendLine("mCachedViews=$mCachedViews")
.appendLine("${poolMediator.dump()}")
.appendLine(poolMediator.dump())
.appendLine(">>>>>>> Recycler End <<<<<<<<")
return stringBuilder.toString()
}
Expand Down Expand Up @@ -227,7 +229,7 @@ class RecycledViewPoolMediator(private val pool: RecyclerView.RecycledViewPool)
val itemViewType = mScrap.keyAt(i)
val scrapData = mScrap.valueAt(i)
val scrapDataMediator = ScrapDataMediator(scrapData)
stringBuilder.appendLine("index=$i,itemViewType=$itemViewType,scrapData.size=${scrapDataMediator.mScrapHeap.size},scrapData=$scrapDataMediator")
stringBuilder.appendLine("index=$i,itemViewType=$itemViewType,mScrapHeap.size=${scrapDataMediator.mScrapHeap.size},scrapData=$scrapDataMediator")
}

stringBuilder.appendLine("======= Recycler.RecycledViewPool End =======")
Expand Down Expand Up @@ -275,24 +277,27 @@ class ScrapDataMediator(private val scrapData: Any) {
private val mCreateRunningAverageNsField: Field
private val mBindRunningAverageNsField: Field

private val scrapDataClass: Class<*>

init {

val scrapDataClassName = RecyclerView::class.java.name + "$" + "RecycledViewPool" + "$" + "ScrapData"
scrapDataClass = Class.forName(scrapDataClassName)
val scrapDataClass = Class.forName(scrapDataClassName)

mScrapHeapField = scrapDataClass.getDeclaredField("mScrapHeap").also {
it.isAccessible = true
}
mMaxScrapField = scrapDataClass.getDeclaredField("mMaxScrap").also {
it.isAccessible = true
}
mCreateRunningAverageNsField = scrapDataClass.getDeclaredField("mCreateRunningAverageNs").also {
it.isAccessible = true
}
mBindRunningAverageNsField = scrapDataClass.getDeclaredField("mBindRunningAverageNs").also {
it.isAccessible = true
scrapDataClass.run {

mScrapHeapField = getDeclaredField("mScrapHeap").also {
it.isAccessible = true
}

mMaxScrapField = getDeclaredField("mMaxScrap").also {
it.isAccessible = true
}

mCreateRunningAverageNsField = getDeclaredField("mCreateRunningAverageNs").also {
it.isAccessible = true
}

mBindRunningAverageNsField = getDeclaredField("mBindRunningAverageNs").also {
it.isAccessible = true
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.yifeiyuan.flap.service

/**
* Created by 程序亦非猿 on 2022/8/16.
*
* @since 3.0.3
*/
interface AdapterService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.yifeiyuan.flap.service

/**
*
* 提供注册和发现 AdapterService 的能力
*
* @see me.yifeiyuan.flap.FlapAdapter.registerAdapterService
* @see me.yifeiyuan.flap.FlapAdapter.getAdapterService
*
* Created by 程序亦非猿 on 2022/8/16.
* @since 3.0.3
*/
@Suppress("UNCHECKED_CAST")
internal class AdapterServiceManager : IAdapterServiceManager {

private val services = mutableMapOf<Class<*>, AdapterService>()

/**
* 有名字的 Service
*/
private val namedServices = mutableMapOf<String, AdapterService>()

override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>) {
try {
val service = serviceClass.getConstructor().newInstance()
services[serviceClass] = service
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>, service: T) {
services[serviceClass] = service
}

override fun <T : AdapterService> getAdapterService(serviceClass: Class<T>): T? {
return services[serviceClass] as? T
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) {
try {
val service = serviceClass.getConstructor().newInstance()
namedServices[serviceName] = service
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, service: T) {
namedServices[serviceName] = service
}

override fun <T : AdapterService> getAdapterService(serviceName: String): T? {
return namedServices[serviceName] as? T
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.yifeiyuan.flap.service

/**
* Created by 程序亦非猿 on 2022/8/18.
*
* @since 3.0.3
*/
interface IAdapterServiceManager {

fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>)

fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>, service: T)

fun <T : AdapterService> getAdapterService(serviceClass: Class<T>): T?

fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>)

fun <T : AdapterService> registerAdapterService(serviceName: String, service: T)

fun <T : AdapterService> getAdapterService(serviceName: String): T?
}

0 comments on commit 247fa95

Please sign in to comment.