From 5bc68bb48ec0544d9d16e1a420e17c121799a227 Mon Sep 17 00:00:00 2001 From: Ztiany Date: Sat, 19 Oct 2019 14:57:28 +0800 Subject: [PATCH] fix LazyDelegate --- .../fragment/delegates/FragmentDelegate.java | 2 +- .../app/fragment/delegates/LazyDelegate.kt | 54 +++++-------------- .../java/com/android/base/app/ui/UIKit.kt | 6 +-- .../com/android/base/utils/common/Times.kt | 2 + 4 files changed, 17 insertions(+), 47 deletions(-) diff --git a/lib_base/src/main/java/com/android/base/app/fragment/delegates/FragmentDelegate.java b/lib_base/src/main/java/com/android/base/app/fragment/delegates/FragmentDelegate.java index 4ceb617..dc36aca 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/delegates/FragmentDelegate.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/delegates/FragmentDelegate.java @@ -35,7 +35,7 @@ public interface FragmentDelegate { default void onSaveInstanceState(Bundle savedInstanceState) { } - default void onViewCreated(@NonNull View view, Bundle savedInstanceState) { + default void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { } default void onStart() { diff --git a/lib_base/src/main/java/com/android/base/app/fragment/delegates/LazyDelegate.kt b/lib_base/src/main/java/com/android/base/app/fragment/delegates/LazyDelegate.kt index 34ad803..591bf84 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/delegates/LazyDelegate.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/delegates/LazyDelegate.kt @@ -3,9 +3,6 @@ package com.android.base.app.fragment.delegates import android.os.Bundle import android.view.View import androidx.fragment.app.Fragment -import com.android.base.app.fragment.BaseFragment -import kotlin.properties.ReadOnlyProperty -import kotlin.reflect.KProperty /** * 用于在ViewPager中实现懒加载的Fragment: @@ -19,12 +16,12 @@ import kotlin.reflect.KProperty open class LazyDelegate private constructor() : FragmentDelegate { /** View是否准备好,如果不需要绑定view数据,只是加载网络数据,那么该字段可以去掉 */ - private var mIsViewPrepared = false + private var isViewPrepared = false /** 滑动过来后,View是否可见 */ - private var mIsViewVisible = false + private var isViewVisible = false - private var mOnPreparedListener: (() -> Unit)? = null + private var preparedListener: (() -> Unit)? = null /** * 在这里实现Fragment数据的缓加载. @@ -33,16 +30,16 @@ open class LazyDelegate private constructor() : FragmentDelegate { */ override fun setUserVisibleHint(isVisibleToUser: Boolean) { if (isVisibleToUser) { - mIsViewVisible = true + isViewVisible = true onVisible() } else { - mIsViewVisible = false + isViewVisible = false onInvisible() } } - override fun onViewCreated(view: View, savedInstanceState: Bundle) { - mIsViewPrepared = true + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + isViewPrepared = true } override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -62,7 +59,7 @@ open class LazyDelegate private constructor() : FragmentDelegate { protected fun onInvisible() {} private fun lazyLoad() { - if (mIsViewPrepared && mIsViewVisible) { + if (isViewPrepared && isViewVisible) { notifyLazyLoad() } } @@ -71,13 +68,13 @@ open class LazyDelegate private constructor() : FragmentDelegate { * 懒加载数据,并在此绑定View数据 */ private fun notifyLazyLoad() { - mOnPreparedListener?.invoke() + preparedListener?.invoke() } companion object { fun attach(delegateFragment: FragmentDelegateOwner, onPreparedListener: () -> Unit): LazyDelegate { val delegate = LazyDelegate() - delegate.mOnPreparedListener = onPreparedListener + delegate.preparedListener = onPreparedListener delegateFragment.addDelegate(delegate) return delegate } @@ -87,38 +84,13 @@ open class LazyDelegate private constructor() : FragmentDelegate { class SimpleLazyLoadListener(private val onFirstLoad: () -> Unit) : (() -> Unit) { - private var mIsCalled = false + private var isCalled = false override fun invoke() { - if (!mIsCalled) { + if (!isCalled) { onFirstLoad() - mIsCalled = true + isCalled = true } } -} - - -/** - * 懒加载代理 - * - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-03-08 12:50 - */ -class LazyLoad(private val once: Boolean = true, private val onPrepared: (() -> Unit)) : ReadOnlyProperty { - - private lateinit var lazyDelegate: LazyDelegate - - override fun getValue(thisRef: BaseFragment, property: KProperty<*>): LazyDelegate { - if (!::lazyDelegate.isInitialized) { - lazyDelegate = if (once) { - LazyDelegate.attach(thisRef, SimpleLazyLoadListener(onPrepared)) - } else { - LazyDelegate.attach(thisRef, onPrepared) - } - } - return lazyDelegate - } - } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/ui/UIKit.kt b/lib_base/src/main/java/com/android/base/app/ui/UIKit.kt index 04fbcf2..268e9a3 100644 --- a/lib_base/src/main/java/com/android/base/app/ui/UIKit.kt +++ b/lib_base/src/main/java/com/android/base/app/ui/UIKit.kt @@ -138,11 +138,7 @@ fun RefreshStateLayout.handleResultWithStatus(t: T?, onResult: ((T) -> Unit) } } -fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable?) { - if (throwable == null) { - Timber.d("processErrorWithStatus called, but throwable is null") - return - } +fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable) { if (isRefreshing) { refreshCompleted() } diff --git a/lib_base/src/main/java/com/android/base/utils/common/Times.kt b/lib_base/src/main/java/com/android/base/utils/common/Times.kt index 26d9eb9..42ce59a 100644 --- a/lib_base/src/main/java/com/android/base/utils/common/Times.kt +++ b/lib_base/src/main/java/com/android/base/utils/common/Times.kt @@ -3,6 +3,8 @@ package com.android.base.utils.common import java.text.SimpleDateFormat import java.util.* +const val TIME_PATTERN_LONG = "yyyy-MM-dd HH:mm:ss" + fun formatMilliseconds(milliseconds: Long, pattern: String = "yyyy-MM-dd"): String { val sdp = SimpleDateFormat(pattern, Locale.getDefault()) val date = Date()