package com.casic.br.extensions import android.content.Context import android.util.Log import com.casic.br.callback.OnImageCompressListener import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.extensions.toJson import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File import java.text.ParseException import java.util.* import kotlin.math.abs /** * String扩展方法 */ fun String.createCommand(value: Any): String { val map = HashMap<String, Any>() map[this] = value val command = map.toJson() Log.d("kTag", "createCommand: $command") return command } fun String.compressImage(context: Context, listener: OnImageCompressListener) { Luban.with(context) .load(this) .ignoreBy(100) .setTargetDir(context.createCompressImageDir().toString()) .filter { !(it.isBlank() || it.lowercase(Locale.getDefault()).endsWith(".gif")) } .setCompressListener(object : OnCompressListener { override fun onStart() { } override fun onSuccess(file: File) { listener.onSuccess(file) } override fun onError(e: Throwable) { listener.onError(e) } }).launch() } /** * 时间差-天 * */ fun String.diffDate(): Int { if (this.isBlank()) { return 0 } try { val diff = abs(System.currentTimeMillis() - this.toLong() * 1000L) return (diff / (86400000)).toInt() } catch (e: ParseException) { e.printStackTrace() } return 0 } /** * 时间差-分钟 * */ fun String.diffMinute(): Int { if (this.isBlank()) { return 0 } try { val diff = abs(System.currentTimeMillis() - this.toLong()) println(diff) return (diff / (60000)).toInt() } catch (e: ParseException) { e.printStackTrace() } return 0 }