Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ package io.goooler.demoapp.base.util
import android.graphics.Color
import android.graphics.Outline
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.Px
import androidx.databinding.BindingAdapter

// ------------------------View --------------------------//
@BindingAdapter("binding_isEnabled")
internal fun View.bindingIsEnabled(enabled: Boolean) {
this.isEnabled = enabled
}
Comment on lines +21 to +24
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace clickable in xml.


@BindingAdapter("binding_isGone")
internal fun View.bindingIsGone(gone: Boolean) {
Expand Down Expand Up @@ -81,6 +87,19 @@ internal fun View.bindingMarginEnd(@Px margin: Float) {
marginDirection(2, margin)
}

@BindingAdapter("binding_onLongClick")
internal fun View.bindingOnLongClick(body: () -> Unit) {
setOnLongClickListener {
body()
true
}
}

@BindingAdapter("binding_tint")
fun ImageView.bindingTint(@ColorInt color: Int) {
setColorFilter(color, PorterDuff.Mode.SRC_IN)
}
Comment on lines +98 to +101
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// ------------------------View Bg Shape---------------------//

@BindingAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.lifecycle.coroutineScope
import androidx.lifecycle.findViewTreeLifecycleOwner
import java.io.File
import java.io.Serializable
import java.lang.reflect.Method
import java.math.BigDecimal
import java.util.Collections
import java.util.UUID
Expand Down Expand Up @@ -102,6 +103,33 @@ fun <T : Parcelable> T.deepCopy(): T? {
}
}

@Throws(ReflectiveOperationException::class)
fun lazyReflectedMethod(
declaringClass: Class<*>,
methodName: String,
vararg parameterTypes: Any
): Lazy<Method> = lazy {
getReflectedMethod(declaringClass, methodName, *getParameterTypes(parameterTypes))
}

@Throws(ReflectiveOperationException::class)
fun getParameterTypes(parameterTypes: Array<out Any>): Array<Class<*>> =
Array(parameterTypes.size) {
when (val parameterType = parameterTypes[it]) {
is Class<*> -> parameterType
is String -> Class.forName(parameterType)
else -> throw IllegalArgumentException(parameterType.toString())
}
}

@Throws(ReflectiveOperationException::class)
fun getReflectedMethod(
declaringClass: Class<*>,
methodName: String,
vararg parameterTypes: Class<*>
): Method =
declaringClass.getDeclaredMethod(methodName, *parameterTypes).also { it.isAccessible = true }

// ---------------------CharSequence-------------------------------//

operator fun String.times(@IntRange(from = 0) num: Int): String {
Expand All @@ -126,6 +154,11 @@ fun String.onlyDigits(): String = replace(Regex("\\D*"), "")

fun String.removeAllSpecialCharacters(): String = replace(Regex("[^a-zA-Z]+"), "")

/**
* Validate given text is a valid filename.
*
* @return true if given text is a valid filename
*/
fun String.isValidFilename(): Boolean {
val filenameRegex =
Pattern.compile("[\\\\\\/:\\*\\?\"<>\\|\\x01-\\x1F\\x7F]", Pattern.CASE_INSENSITIVE)
Expand Down Expand Up @@ -391,17 +424,17 @@ fun Intent.getStringExtra(name: String, defaultValue: String): String =
fun Intent.getCharSequenceExtra(name: String, defaultValue: CharSequence): CharSequence =
getCharSequenceExtra(name) ?: defaultValue

fun <T : Parcelable> Intent.getParcelableExtra(name: String, defaultValue: T): T =
inline fun <reified T : Parcelable> Intent.getParcelableExtra(name: String, defaultValue: T): T =
getParcelableExtra(name) ?: defaultValue

fun <T : Serializable> Intent.getSerializableExtra(
inline fun <reified T : Serializable> Intent.getSerializableExtra(
name: String,
defaultValue: Serializable
): Serializable = getSerializableExtra(name) ?: defaultValue
defaultValue: T
): T = (getSerializableExtra(name) ?: defaultValue) as T

// ---------------------Fragment-------------------------------//

fun <T : Fragment> T.putArguments(bundle: Bundle): T {
fun <T : Fragment> T.putArguments(bundle: Bundle?): T {
arguments = bundle
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StatusBarView(context: Context, attrs: AttributeSet? = null) : View(contex
private fun getStatusBarHeight(): Int {
val resources = Resources.getSystem()
@SuppressLint("InternalInsetResource")
@DimenRes val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId)
@DimenRes val resId = resources.getIdentifier("status_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.ViewDataBinding
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import io.goooler.demoapp.base.core.BaseActivity
import io.goooler.demoapp.common.util.inflateBinding
import java.lang.reflect.ParameterizedType

abstract class BaseBindingActivity<VB : ViewDataBinding> : BaseActivity(), IBinding<VB> {

Expand Down Expand Up @@ -39,4 +41,16 @@ abstract class BaseBindingActivity<VB : ViewDataBinding> : BaseActivity(), IBind
else
AdaptScreenUtils.adaptHeight(super.getResources(), 640)
}

companion object {
@Suppress("UNCHECKED_CAST")
internal fun <T : ViewBinding> Any.inflateBinding(inflater: LayoutInflater): T {
return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
.filterIsInstance<Class<T>>()
.first()
.getDeclaredMethod("inflate", LayoutInflater::class.java)
.also { it.isAccessible = true }
.invoke(null, inflater) as T
}
}
Comment on lines +45 to +55
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set isAccessible to true for faster access.

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import io.goooler.demoapp.base.core.BaseDialogFragment
import io.goooler.demoapp.common.util.inflateBinding
import io.goooler.demoapp.common.base.binding.BaseBindingActivity.Companion.inflateBinding

abstract class BaseBindingDialogFragment<VB : ViewDataBinding> :
BaseDialogFragment(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import io.goooler.demoapp.base.core.BaseFragment
import io.goooler.demoapp.common.util.inflateBinding
import io.goooler.demoapp.common.base.binding.BaseBindingActivity.Companion.inflateBinding

abstract class BaseBindingFragment<VB : ViewDataBinding> :
BaseFragment(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

package io.goooler.demoapp.common.util

import android.content.ContentResolver
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.text.format.DateFormat
import android.text.format.Formatter
import android.view.View
import android.widget.TextView
import androidx.annotation.AnyThread
Expand All @@ -17,10 +20,8 @@ import androidx.annotation.PluralsRes
import androidx.annotation.Px
import androidx.annotation.StringRes
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ImageUtils
Expand All @@ -29,6 +30,7 @@ import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.google.android.material.textfield.TextInputLayout
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import io.goooler.demoapp.base.util.Dp
Expand All @@ -38,7 +40,7 @@ import io.goooler.demoapp.base.util.ToastUtil
import io.goooler.demoapp.common.BuildConfig
import io.goooler.demoapp.common.CommonApplication
import io.goooler.demoapp.common.type.SpKeys
import java.lang.reflect.ParameterizedType
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor

Expand All @@ -63,25 +65,23 @@ fun @receiver:StringRes Int.showToast() {
ToastUtil.show(CommonApplication.app, this)
}

@MainThread
fun SmartRefreshLayout.finishRefreshAndLoadMore() {
finishRefresh()
finishLoadMore()
inline fun <reified T : Any> DiffUtil.ItemCallback<T>.asConfig(): AsyncDifferConfig<T> {
return AsyncDifferConfig.Builder(this)
.setBackgroundThreadExecutor(Dispatchers.Default.asExecutor())
.build()
}

@MainThread
fun SmartRefreshLayout.enableRefreshAndLoadMore(enable: Boolean = true) {
setEnableRefresh(enable)
setEnableLoadMore(enable)
}
val contentResolver: ContentResolver get() = CommonApplication.app.contentResolver

@MainThread
fun SmartRefreshLayout.disableRefreshAndLoadMore() {
enableRefreshAndLoadMore(false)
}
val packageManager: PackageManager get() = CommonApplication.app.packageManager

// ---------------------String-------------------------------//

fun Long.formatFileSize(): String = Formatter.formatFileSize(CommonApplication.app, this)

fun Long.millis2String(pattern: String = "yyyyMMddHHmmss"): String =
TimeUtils.millis2String(this, DateFormat.getBestDateTimePattern(Locale.getDefault(), pattern))

@AnyThread
fun String.showToast() {
ToastUtil.show(CommonApplication.app, this)
Expand Down Expand Up @@ -158,19 +158,19 @@ fun TextView.hideTextInputLayoutErrorOnTextChange(textInputLayout: TextInputLayo
doAfterTextChanged { textInputLayout.error = null }
}

inline fun <reified T : Any> DiffUtil.ItemCallback<T>.asConfig(): AsyncDifferConfig<T> {
return AsyncDifferConfig.Builder(this)
.setBackgroundThreadExecutor(Dispatchers.Default.asExecutor())
.build()
@MainThread
fun SmartRefreshLayout.finishRefreshAndLoadMore() {
finishRefresh()
finishLoadMore()
}

// ---------------------VM & Binding-------------------------------//
@MainThread
fun SmartRefreshLayout.enableRefreshAndLoadMore(enable: Boolean = true) {
setEnableRefresh(enable)
setEnableLoadMore(enable)
}

@Suppress("UNCHECKED_CAST")
fun <T : ViewBinding> LifecycleOwner.inflateBinding(inflater: LayoutInflater): T {
return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
.filterIsInstance<Class<T>>()
.first()
.getDeclaredMethod("inflate", LayoutInflater::class.java)
.invoke(null, inflater) as T
@MainThread
fun SmartRefreshLayout.disableRefreshAndLoadMore() {
enableRefreshAndLoadMore(false)
}