Skip to content

Commit

Permalink
日常修改:2024-06-28.
Browse files Browse the repository at this point in the history
  • Loading branch information
walgr committed Jun 28, 2024
1 parent caeff86 commit c7f0ea1
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ open class QuickFragment(
// abilityList.filterIsInstance<QuickLifecycleAbility>().forEach {
// it.onSaveInstanceState(outState)
// }
// outState.putSerializable("abilityList", abilityList.toTypedArray())
// }

@CallSuper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import kotlin.math.abs

fun ContextScope.viewPager2(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
fragments: List<Fragment>,
limit: Int = 0,
builder: (ViewPager2.() -> Unit)? = null,
): ViewPager2 {
val viewPager2 = ViewPager2(context)
viewPager2.id = R.id.quickViewPager
viewPager2.id = id
viewPager2.adapter =
Fragments2StateAdapter(quick.getFragmentManager(), quick.getLifecycle()) {
fragments[it]
Expand All @@ -47,6 +48,7 @@ fun ContextScope.viewPager2(
@Suppress("unused")
inline fun <reified F : Fragment> ContextScope.viewPager2(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
defaultSize: Int = 1,
limit: Int = 0,
Expand All @@ -55,12 +57,13 @@ inline fun <reified F : Fragment> ContextScope.viewPager2(
noinline builder: (ViewPager2.() -> Unit)? = null,
): ViewPager2 {
val viewPager2 = ViewPager2(context)
viewPager2.id = R.id.quickViewPager
viewPager2.id = id
val defaultPos = if (isLoop) Int.MAX_VALUE / 2 else 0
var defaultSizeNew = defaultSize
viewPager2.adapter =
Fragments2StateAdapter(quick.getFragmentManager(), quick.getLifecycle()) {
object : Fragments2StateAdapter(quick.getFragmentManager(), quick.getLifecycle(), {
val realPos =
if (isLoop) (abs(defaultSize + (it - defaultPos) % defaultSize) % defaultSize) else it
if (isLoop) (abs(defaultSizeNew + (it - defaultSizeNew) % defaultSizeNew) % defaultSizeNew) else it
val fragment = getFragment(
quick,
F::class.java.getDeclaredConstructor().newInstance().forceTo(),
Expand All @@ -70,8 +73,13 @@ inline fun <reified F : Fragment> ContextScope.viewPager2(
fragment.arguments = fragmentDataInit.invoke(it)
}
fragment
}) {
override fun setPageSize(size: Int) {
defaultSizeNew = size
super.setPageSize(if (isLoop) Int.MAX_VALUE else size)
}
}.apply {
setPageSize(if (isLoop) Int.MAX_VALUE else defaultSize)
setPageSize(defaultSizeNew)
if (isLoop) {
registerItemIdChange {
System.currentTimeMillis() % Int.MAX_VALUE
Expand All @@ -91,6 +99,7 @@ inline fun <reified F : Fragment> ContextScope.viewPager2(

fun ContextScope.viewPager2WithView(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
views: List<View>,
limit: Int = 0,
Expand All @@ -102,12 +111,13 @@ fun ContextScope.viewPager2WithView(
contentView.addView(it)
contentView.toFragment()
}
return viewPager2(layoutParams, quick, contentFragmentList, limit, builder)
return viewPager2(layoutParams, id, quick, contentFragmentList, limit, builder)
}

@Suppress("unused")
fun ContextScope.viewPager2(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
limit: Int = 0,
builder: ViewGroup.() -> Unit,
Expand All @@ -118,5 +128,5 @@ fun ContextScope.viewPager2(
childViews.forEach {
it.removeParent()
}
return viewPager2WithView(layoutParams, quick, childViews, limit)
return viewPager2WithView(layoutParams, id, quick, childViews, limit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.wpf.app.quick.helper.toFragment
import com.wpf.app.quickbind.utils.getFragment
import com.wpf.app.quickbind.viewpager.adapter.FragmentsAdapter
import com.wpf.app.quickbind.viewpager.adapter.FragmentsStateAdapter
import com.wpf.app.quickutil.helper.InitViewHelper
import com.wpf.app.quickutil.helper.matchMarginLayoutParams
import com.wpf.app.quickutil.other.forceTo
import com.wpf.app.quickwidget.viewpager.QuickViewPager
Expand All @@ -40,8 +41,10 @@ class FragmentGroup @JvmOverloads constructor(
}
}

@Suppress("unused")
inline fun <reified F : Fragment> ContextScope.viewPager(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
withState: Boolean = true,
defaultSize: Int = 1,
Expand All @@ -51,8 +54,9 @@ inline fun <reified F : Fragment> ContextScope.viewPager(
noinline builder: (QuickViewPager.() -> Unit)? = null,
): QuickViewPager {
val viewPager = QuickViewPager(context)
viewPager.id = R.id.quickViewPager
viewPager.id = id
val defaultPos = if (isLoop) Int.MAX_VALUE / 2 else 0
var defaultSizeNew = defaultSize
if (withState && !isLoop) {
viewPager.adapter =
FragmentsStateAdapter(quick.getFragmentManager()) {
Expand All @@ -66,23 +70,28 @@ inline fun <reified F : Fragment> ContextScope.viewPager(
}
fragment
}.apply {
setPageSize(defaultSize)
setPageSize(defaultSizeNew)
}
} else {
viewPager.adapter = FragmentsAdapter(quick.getFragmentManager()) {
viewPager.adapter = object : FragmentsAdapter(quick.getFragmentManager(), {
val realPos =
if (isLoop) (abs(defaultSize + (it - defaultPos) % defaultSize) % defaultSize) else it
if (isLoop) (abs(defaultSizeNew + (it - defaultPos) % defaultSizeNew) % defaultSizeNew) else it
val fragment = getFragment(
context,
F::class.java.getDeclaredConstructor().newInstance().forceTo(),
realPos
).forceTo<Fragment>()
fragmentDataInit?.apply {
fragment.arguments = fragmentDataInit.invoke(it)
fragment.arguments = fragmentDataInit.invoke(realPos)
}
fragment
}) {
override fun setPageSize(size: Int) {
defaultSizeNew = size
super.setPageSize(if (isLoop) Int.MAX_VALUE else size)
}
}.apply {
setPageSize(if (isLoop) Int.MAX_VALUE else defaultSize)
setPageSize(defaultSizeNew)
if (isLoop) {
registerItemPositionChange {
return@registerItemPositionChange PagerAdapter.POSITION_NONE
Expand All @@ -106,14 +115,15 @@ inline fun <reified F : Fragment> ContextScope.viewPager(

fun ContextScope.viewPager(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
fragments: List<Fragment>,
withState: Boolean = true,
limit: Int = 0,
builder: (QuickViewPager.() -> Unit)? = null,
): QuickViewPager {
val viewPager = QuickViewPager(context)
viewPager.id = R.id.quickViewPager
viewPager.id = id
if (withState) {
viewPager.adapter = FragmentsStateAdapter(quick.getFragmentManager()) {
fragments[it]
Expand All @@ -135,9 +145,72 @@ fun ContextScope.viewPager(
return viewPager
}

@Suppress("unused")
inline fun <reified T : View> ContextScope.viewPagerWithView(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
withState: Boolean = true,
defaultSize: Int = 1,
limit: Int = 0,
isLoop: Boolean = false,
noinline viewInit: (T.(Int) -> Unit)? = null,
noinline builder: (QuickViewPager.() -> Unit)? = null,
): QuickViewPager {
val viewPager = QuickViewPager(context)
viewPager.id = id
val defaultPos = if (isLoop) Int.MAX_VALUE / 2 else 0
var defaultSizeNew = defaultSize
if (withState && !isLoop) {
viewPager.adapter =
FragmentsStateAdapter(quick.getFragmentManager()) {
val view = InitViewHelper.newInstance<T>(context)
viewInit?.invoke(view, it)
val fragment = view.toFragment()
fragment
}.apply {
setPageSize(defaultSizeNew)
}
} else {
viewPager.adapter = object : FragmentsAdapter(quick.getFragmentManager(), {
val realPos =
if (isLoop) (abs(defaultSizeNew + (it - defaultPos) % defaultSizeNew) % defaultSizeNew) else it
val view = InitViewHelper.newInstance<T>(context)
viewInit?.invoke(view, realPos)
val fragment = view.toFragment()
fragment
}) {
override fun setPageSize(size: Int) {
defaultSizeNew = size
super.setPageSize(if (isLoop) Int.MAX_VALUE else size)
}
}.apply {
setPageSize(defaultSizeNew)
if (isLoop) {
registerItemPositionChange {
return@registerItemPositionChange PagerAdapter.POSITION_NONE
}
registerItemIdChange {
System.currentTimeMillis() % Int.MAX_VALUE
}
}
}
}
if (limit != 0) {
viewPager.offscreenPageLimit = limit
}
addView(viewPager, layoutParams)
builder?.invoke(viewPager)
if (isLoop) {
viewPager.setCurrentItem(defaultPos, false)
}
return viewPager
}

@Suppress("unused")
fun ContextScope.viewPagerWithView(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
views: List<View>,
withState: Boolean = true,
Expand All @@ -150,11 +223,13 @@ fun ContextScope.viewPagerWithView(
contentView.addView(it)
contentView.toFragment()
}
return viewPager(layoutParams, quick, contentFragmentList, withState, limit, builder)
return viewPager(layoutParams, id, quick, contentFragmentList, withState, limit, builder)
}

@Suppress("unused")
fun ContextScope.viewPagerBuilder(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
id: Int = R.id.quickViewPager,
quick: Quick,
withState: Boolean = true,
limit: Int = 0,
Expand All @@ -163,5 +238,5 @@ fun ContextScope.viewPagerBuilder(
): QuickViewPager {
val viewGroup = FragmentGroup(context, viewConvert)
builder?.invoke(viewGroup)
return viewPager(layoutParams, quick, viewGroup.fragmentList, withState, limit)
return viewPager(layoutParams, id, quick, viewGroup.fragmentList, withState, limit)
}
9 changes: 9 additions & 0 deletions Quick/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@
<resources>
<item name="quickList" type="id"/>
<item name="quickViewPager" type="id"/>
<item name="quickViewPager1" type="id"/>
<item name="quickViewPager2" type="id"/>
<item name="quickViewPager3" type="id"/>
<item name="quickViewPager4" type="id"/>
<item name="quickViewPager5" type="id"/>
<item name="quickViewPager6" type="id"/>
<item name="quickViewPager7" type="id"/>
<item name="quickViewPager8" type="id"/>
<item name="quickViewPager9" type="id"/>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.wpf.app.base.ability.base

import java.io.Serializable


fun <T : QuickAbility> MutableList<T>.with(others: MutableList<T>): MutableList<T> {
others.filter { it is Unique }.map { it.getPrimeKey() }.forEach { otherPrimeKey ->
Expand Down Expand Up @@ -34,6 +36,6 @@ fun <T : QuickAbility> T.with(other: T): MutableList<T> {
return abilityList
}

interface QuickAbility {
interface QuickAbility: Serializable {
fun getPrimeKey(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ import com.wpf.app.quickutil.helper.parent
import com.wpf.app.quickutil.helper.removeParent
import com.wpf.app.quickutil.widget.wishLayoutParams

@Suppress("unused")
fun <Follow : View, Bottom : View> ContextScope.coordinator(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
followSlideLayout: AppBarLayout.() -> Follow,
scrollFlags: Int,
bottomScrollLayout: CoordinatorLayout.() -> Bottom,
behavior: Behavior<*> = AppBarLayout.ScrollingViewBehavior(),
builder: (CoordinatorLayout.(followSlideLayout: Follow?, bottomScrollLayout: Bottom) -> Unit)? = null,
): CoordinatorLayout {
return coordinator<Follow, View, Bottom>(
layoutParams = layoutParams,
followSlideLayout = followSlideLayout,
scrollFlags = scrollFlags,
bottomScrollLayout = bottomScrollLayout,
behavior = behavior,
builder = {followLayout, _, bottomLayout ->
builder?.invoke(this, followLayout, bottomLayout)
},
)
}

fun <Follow : View, Top : View, Bottom : View> ContextScope.coordinator(
layoutParams: ViewGroup.LayoutParams = matchMarginLayoutParams(),
followSlideLayout: (AppBarLayout.() -> Follow)? = null,
Expand All @@ -31,7 +52,12 @@ fun <Follow : View, Top : View, Bottom : View> ContextScope.coordinator(
followSlideLayoutView = it
if (it.parent() != appBarLayout) {
it.removeParent()
appBarLayout.addView(it, it.layoutParams ?: layoutParams<AppBarLayout.LayoutParams>(matchWrapMarginLayoutParams()))
appBarLayout.addView(
it,
it.layoutParams ?: layoutParams<AppBarLayout.LayoutParams>(
matchWrapMarginLayoutParams()
)
)
}
it.wishLayoutParams<AppBarLayout.LayoutParams>().scrollFlags = scrollFlags!!
}
Expand All @@ -40,7 +66,12 @@ fun <Follow : View, Top : View, Bottom : View> ContextScope.coordinator(
topSuspendLayoutView = it
if (it.parent() != appBarLayout) {
it.removeParent()
appBarLayout.addView(it, it.layoutParams ?: layoutParams<AppBarLayout.LayoutParams>(matchWrapMarginLayoutParams()))
appBarLayout.addView(
it,
it.layoutParams ?: layoutParams<AppBarLayout.LayoutParams>(
matchWrapMarginLayoutParams()
)
)
}
it.wishLayoutParams<AppBarLayout.LayoutParams>().scrollFlags = 0
}
Expand All @@ -49,7 +80,12 @@ fun <Follow : View, Top : View, Bottom : View> ContextScope.coordinator(
bottomScrollLayoutView = it
if (it.parent() != parentLayout) {
it.removeParent()
parentLayout.addView(it, it.layoutParams ?: layoutParams<CoordinatorLayout.LayoutParams>(matchWrapMarginLayoutParams()))
parentLayout.addView(
it,
it.layoutParams ?: layoutParams<CoordinatorLayout.LayoutParams>(
matchMarginLayoutParams()
)
)
}
it.wishLayoutParams<CoordinatorLayout.LayoutParams>().behavior = behavior
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.wpf.app.quickbind.viewpager.ViewPagerSize
* Created by 王朋飞 on 2022/7/12.
*
*/
class FragmentsAdapter(
open class FragmentsAdapter(
fm: FragmentManager, private val getFragment: (position: Int) -> Fragment,
) : FragmentPagerAdapter(fm), ViewPagerSize {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object InitViewHelper {
return layoutViewCreate?.invoke(context) ?: layoutView ?: layoutId.toView(context, mParent, attachToRoot)
}

inline fun <reified T: ViewGroup> newInstance(context: Context): T {
inline fun <reified T: View> newInstance(context: Context): T {
return T::class.java.getConstructor(Context::class.java).newInstance(context)
}
}
Loading

0 comments on commit c7f0ea1

Please sign in to comment.