feat:初步完成发红包接口对接

feat:增加服务器时间字段
This commit is contained in:
Max
2023-10-25 15:17:08 +08:00
parent c3dab80e71
commit 6ec57834fd
21 changed files with 852 additions and 64 deletions

View File

@@ -0,0 +1,25 @@
package com.chuhai.utils
import android.os.SystemClock
/**
* Created by Max on 3/4/21 5:00 PM
* Desc:服务器时间
*/
object ServiceTime {
// 服务器时间与系统开机时间的时差
private var serviceTimeDiff: Long? = null
val time
get() = if (serviceTimeDiff == null) System.currentTimeMillis()
else SystemClock.elapsedRealtime() + serviceTimeDiff!!
/**
* 刷新服务器时间
*/
fun refreshServiceTime(time: Long) {
//serviceTimeDiff = 服务器时间 - 此刻系统启动时间
serviceTimeDiff = time - SystemClock.elapsedRealtime()
}
}

View File

@@ -0,0 +1,42 @@
package com.chuhai.utils
import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import kotlin.math.min
/**
* Created by Max on 2/25/21 1:50 PM
* Desc:
*/
class ShapeViewOutlineProvider {
/**
* Created by Max on 2/25/21 1:48 PM
* Desc:圆角
*/
class Round(var corner: Float) : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
outline.setRoundRect(
0,
0,
view.width,
view.height,
corner
)
}
}
/**
* Created by Max on 2/25/21 1:48 PM
* Desc:圆形
*/
class Circle : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
val min = min(view.width, view.height)
val left = (view.width - min) / 2
val top = (view.height - min) / 2
outline.setOval(left, top, min, min)
}
}
}

View File

@@ -0,0 +1,106 @@
package com.chuhai.utils.ktx
import android.text.Editable
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.Spanned
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.EditText
/**
* 设置editText输入监听
* @param onChanged 改变事件
* @return 是否接受此次文本的改变
*/
inline fun EditText.setOnInputChangedListener(
/**
* @param Int当前长度
* @return 是否接受此次文本的改变
*/
crossinline onChanged: (Int).() -> Boolean
) {
this.addTextChangedListener(object : TextWatcher {
var flag = false
override fun afterTextChanged(p0: Editable?) {
if (flag) {
return
}
if (!onChanged(p0?.length ?: 0)) {
flag = true
this@setOnInputChangedListener.setText(
this@setOnInputChangedListener.getTag(
1982329101
) as? String
)
this@setOnInputChangedListener.setSelection(this@setOnInputChangedListener.length())
flag = false
} else {
flag = false
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
this@setOnInputChangedListener.setTag(1982329101, p0?.toString())
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
})
}
/**
* 切换密码可见度
*/
fun EditText.switchPasswordVisibility(visibility: Boolean) {
transformationMethod =
if (!visibility) HideReturnsTransformationMethod.getInstance() else PasswordTransformationMethod.getInstance()
}
/**
* 设置输入功能是否启用不启用就相当于TextView
*/
fun EditText.setInputEnabled(isEnabled: Boolean) {
if (isEnabled) {
isFocusable = true
isFocusableInTouchMode = true
isClickable = true
} else {
isFocusable = false
isFocusableInTouchMode = false
isClickable = false
keyListener = null
}
}
/**
* 添加输入长度限制过滤器
*/
fun EditText.addLengthFilter(maxLength: Int) {
val newFilters = filters.copyOf(filters.size + 1)
newFilters[newFilters.size - 1] = LengthFilter(maxLength)
filters = newFilters
}
/**
* 添加禁用文本过滤器
* @param disableText 不允许输入该文本
*/
fun EditText.addDisableFilter(vararg disableText: CharSequence) {
val newFilters = filters.copyOf(filters.size + 1)
newFilters[newFilters.size - 1] = InputFilter { source, p1, p2, p3, p4, p5 ->
disableText.forEach {
if (source.equals(it)) {
return@InputFilter ""
}
}
return@InputFilter null
}
filters = newFilters
}

View File

@@ -0,0 +1,192 @@
package com.chuhai.utils.ktx
import android.graphics.*
import android.os.Build
import android.os.SystemClock
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Checkable
import android.widget.TextView
import androidx.core.view.ScrollingView
import com.chuhai.utils.ShapeViewOutlineProvider
import com.chuhai.utils.UiUtils
/**
* 是否右-左布局
*/
fun View.isRtl(): Boolean {
return UiUtils.isRtl(this)
}
/**
* 展示or隐藏
*/
fun View.visibleOrGone(isShow: Boolean) {
visibility = if (isShow) {
View.VISIBLE
} else {
View.GONE
}
}
/**
* 展示or隐藏
*/
inline fun View.visibleOrGone(show: View.() -> Boolean = { true }) {
visibility = if (show(this)) {
View.VISIBLE
} else {
View.GONE
}
}
/**
* 展示or不可见
*/
inline fun View.visibleOrInvisible(show: View.() -> Boolean = { true }) {
visibility = if (show(this)) {
View.VISIBLE
} else {
View.INVISIBLE
}
}
/**
* 点击事件
*/
inline fun <T : View> T.singleClick(time: Long = 800, crossinline block: (T) -> Unit) {
setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0L
override fun onClick(v: View?) {
val currentTimeMillis = SystemClock.elapsedRealtime()
if (currentTimeMillis - lastClickTime > time || this is Checkable) {
lastClickTime = currentTimeMillis
block(this@singleClick)
}
}
})
}
/**
* 点击事件
*/
fun <T : View> T.singleClick(onClickListener: View.OnClickListener, time: Long = 800) {
setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0L
override fun onClick(v: View?) {
val currentTimeMillis = SystemClock.elapsedRealtime()
if (currentTimeMillis - lastClickTime > time || this is Checkable) {
lastClickTime = currentTimeMillis
onClickListener.onClick(v)
}
}
})
}
/**
* 设置View圆角矩形
*/
fun <T : View> T.roundCorner(corner: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (outlineProvider == null || outlineProvider !is ShapeViewOutlineProvider.Round) {
outlineProvider = ShapeViewOutlineProvider.Round(corner.toFloat())
} else if (outlineProvider != null && outlineProvider is ShapeViewOutlineProvider.Round) {
(outlineProvider as ShapeViewOutlineProvider.Round).corner = corner.toFloat()
}
clipToOutline = true
}
}
/**
* 设置View为圆形
*/
fun <T : View> T.circle() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (outlineProvider == null || outlineProvider !is ShapeViewOutlineProvider.Circle) {
outlineProvider = ShapeViewOutlineProvider.Circle()
}
clipToOutline = true
}
}
fun View.getBitmap(): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.translate(scrollX.toFloat(), scrollY.toFloat())
draw(canvas)
return bitmap
}
/**
* 设置边距
*/
fun View?.setMargin(start: Int? = null, top: Int? = null, end: Int? = null, bottom: Int? = null) {
(this?.layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
start?.let {
this.marginStart = start
}
top?.let {
this.topMargin = top
}
end?.let {
this.marginEnd = end
}
bottom?.let {
this.bottomMargin = bottom
}
}
}
/**
* 设置内边距
*/
fun View?.setPadding2(start: Int? = null, top: Int? = null, end: Int? = null, bottom: Int? = null) {
if (this == null) return
this.setPadding(
start ?: paddingStart, top ?: paddingTop, end ?: paddingEnd, bottom ?: paddingBottom
)
}
/**
* 描边宽度
*/
fun TextView.strokeWidth(width: Float) {
this.paint?.style = Paint.Style.FILL_AND_STROKE
this.paint?.strokeWidth = width
this.invalidate()
}
/**
* 模拟点击并取消
*/
fun ScrollingView.simulateClickAndCancel() {
val view = this as? View ?: return
val downEvent = MotionEvent.obtain(
System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, (view.right - view.left) / 2f, (view.bottom - view.top) / 2f, 0
)
view.dispatchTouchEvent(downEvent)
val cancelEvent = MotionEvent.obtain(
System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_CANCEL, (view.right - view.left) / 2f, (view.bottom - view.top) / 2f, 0
)
view.dispatchTouchEvent(cancelEvent)
}
/**
* 使用灰色滤镜
*/
fun View.applyGrayFilter(isGray: Boolean) {
try {
val paint = Paint()
val colorMatrix = ColorMatrix()
colorMatrix.setSaturation(if (isGray) 0f else 1f)
paint.colorFilter = ColorMatrixColorFilter(colorMatrix)
setLayerType(View.LAYER_TYPE_HARDWARE, paint)
} catch (e: Exception) {
e.printStackTrace()
}
}