Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Cleanup CommonExtensions #80

Merged
merged 2 commits into from
Apr 8, 2022
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 @@ -6,7 +6,6 @@ package io.goooler.demoapp.common.util
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.webkit.URLUtil
import android.widget.TextView
import androidx.annotation.AnyThread
import androidx.annotation.ColorInt
Expand All @@ -29,9 +28,7 @@ import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ImageUtils
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.core.BaseViewModel
Expand All @@ -46,14 +43,9 @@ import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import java.lang.reflect.ParameterizedType
import java.math.BigDecimal
import java.util.Calendar
import java.util.Date
import kotlin.math.absoluteValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor

typealias DimensionUtil = SizeUtils
typealias SpHelper = SPUtils

val isDebug: Boolean = BuildConfig.DEBUG
Expand Down Expand Up @@ -97,90 +89,6 @@ fun SmartRefreshLayout.disableRefreshAndLoadMore() {
enableRefreshAndLoadMore(false)
}

// ---------------------Convert-------------------------------//

/**
* 拼上图片前缀
*/
fun String.toLoadUrl(): String {
return if (URLUtil.isNetworkUrl(this)) this else BuildConfig.CDN_PREFIX + this
}

fun Long.toDateString(pattern: String): String = TimeUtils.millis2String(this, pattern)

fun Long.easyTime(): String {
val now = System.currentTimeMillis()
val t = now - this
if (t < 0) {
// 未来
return toDateString("yyyy-MM-dd HH:mm")
}
val oneMinute = 1000 * 60
val oneHour = oneMinute * 60
val oneDay = oneHour * 24
val c1 = Calendar.getInstance()
val c2 = Calendar.getInstance()
c1.time = Date(this)
c2.time = Date(now)
val day1 = c1.get(Calendar.DAY_OF_WEEK)
val day2 = c2.get(Calendar.DAY_OF_WEEK)
val isYesterday = t < oneDay * 2 && (day2 - day1 == 1 || day2 - day1 == -6)

val year1 = c1.get(Calendar.YEAR)
val year2 = c2.get(Calendar.YEAR)

val isSameYear = year1 == year2

return when {
isSameYear.not() -> toDateString("yyyy-MM-dd HH:mm")
isYesterday -> toDateString("昨天 HH:mm")
t < oneMinute -> "刚刚"
t < oneHour -> (t / oneMinute).toString() + "分钟前"
t < oneDay -> (t / oneHour).toString() + "小时前"
isSameYear -> toDateString("MM-dd HH:mm")
else -> toDateString("yyyy-MM-dd HH:mm")
}
}

/**
* @param isYuan 默认以分为单位,传入元为单位传 true
* @param trans2W 是否需要在超过一万时转换为 1.2w 的形式,不需要的话传 false
*
* 分是 Long 类型、元是 Double 类型
*/
fun Number.formatMoney(isYuan: Boolean = false, trans2W: Boolean = false, scale: Int = 2): String {
val moneyF = if (isYuan) {
toDouble()
} else {
// 分转为元
toDouble() / 100
}
return try {
when {
trans2W && moneyF / 10000 > 0 -> {
BigDecimal.valueOf(moneyF / 10000)
.setScale(1, BigDecimal.ROUND_DOWN)
.stripTrailingZeros().toPlainString() + "W"
}

else ->
BigDecimal.valueOf(moneyF)
.setScale(scale, BigDecimal.ROUND_DOWN)
.stripTrailingZeros().toPlainString()
.let {
if (it.toDouble().absoluteValue < 0.000001) {
"0"
} else {
it
}
}
}
} catch (e: Exception) {
e.printStackTrace()
moneyF.toString()
}
}

// ---------------------Rx-------------------------------//

fun <T : Any> Single<T>.subscribeOnIoThread(): Single<T> = subscribeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.goooler.demoapp.obsolete.util

import com.blankj.utilcode.util.TimeUtils
import io.goooler.demoapp.common.network.HttpResponse
import io.goooler.demoapp.obsolete.network.exception.ResponseException
import io.goooler.demoapp.obsolete.network.exception.toResponseException
import java.math.BigDecimal
import java.util.Calendar
import java.util.Date
import kotlin.math.absoluteValue

@Throws(ResponseException::class)
fun <T> HttpResponse<T>.checkCodeWithException(): T? {
if (code != 200) throw (message ?: code.toString()).toResponseException()
return entry
}

// ---------------------Convert-------------------------------//

fun Long.toDateString(pattern: String): String = TimeUtils.millis2String(this, pattern)

fun Long.easyTime(): String {
val now = System.currentTimeMillis()
val t = now - this
if (t < 0) {
// 未来
return toDateString("yyyy-MM-dd HH:mm")
}
val oneMinute = 1000 * 60
val oneHour = oneMinute * 60
val oneDay = oneHour * 24
val c1 = Calendar.getInstance()
val c2 = Calendar.getInstance()
c1.time = Date(this)
c2.time = Date(now)
val day1 = c1.get(Calendar.DAY_OF_WEEK)
val day2 = c2.get(Calendar.DAY_OF_WEEK)
val isYesterday = t < oneDay * 2 && (day2 - day1 == 1 || day2 - day1 == -6)

val year1 = c1.get(Calendar.YEAR)
val year2 = c2.get(Calendar.YEAR)

val isSameYear = year1 == year2

return when {
isSameYear.not() -> toDateString("yyyy-MM-dd HH:mm")
isYesterday -> toDateString("昨天 HH:mm")
t < oneMinute -> "刚刚"
t < oneHour -> (t / oneMinute).toString() + "分钟前"
t < oneDay -> (t / oneHour).toString() + "小时前"
isSameYear -> toDateString("MM-dd HH:mm")
else -> toDateString("yyyy-MM-dd HH:mm")
}
}

/**
* @param isYuan 默认以分为单位,传入元为单位传 true
* @param trans2W 是否需要在超过一万时转换为 1.2w 的形式,不需要的话传 false
*
* 分是 Long 类型、元是 Double 类型
*/
fun Number.formatMoney(isYuan: Boolean = false, trans2W: Boolean = false, scale: Int = 2): String {
val moneyF = if (isYuan) {
toDouble()
} else {
// 分转为元
toDouble() / 100
}
return try {
when {
trans2W && moneyF / 10000 > 0 -> {
BigDecimal.valueOf(moneyF / 10000)
.setScale(1, BigDecimal.ROUND_DOWN)
.stripTrailingZeros().toPlainString() + "W"
}

else ->
BigDecimal.valueOf(moneyF)
.setScale(scale, BigDecimal.ROUND_DOWN)
.stripTrailingZeros().toPlainString()
.let {
if (it.toDouble().absoluteValue < 0.000001) {
"0"
} else {
it
}
}
}
} catch (e: Exception) {
e.printStackTrace()
moneyF.toString()
}
}

This file was deleted.