文件、时间相关的 Utils,方便的扩展方法

1. 文件相关

1.1 根据文件路径获取文件名
/**
* 根据文件路径获取文件名
* @param pathUrl 文件路径
*/
fun getFileName(pathUrl: String): String {
   val start = pathUrl.lastIndexOf("/");
   val end = pathUrl.lastIndexOf(".");
   if (start != -1 && end != -1) {
       return pathUrl.substring(start + 1, end);
   }
   return ""
}

2. 时间相关

2.1 将单位秒转换为 00:00
/**
 * 将单位秒转换为 00:00
 */
fun secondsToTime(seconds: Int): String {
    if (seconds <= 0) {
        return "00:00"
    }
    var time = ""
    val min = seconds / 60
    MLog.d(TAG, "intToTime: seconds = $seconds")
    time = if (min < 10) {
        time + "0" + min + ":"
    } else {
        "$time$min:"
    }
    MLog.d(TAG, "intToTime: time = $time")
    val sec = seconds - min * 60
    time = if (sec < 10) {
        time + "0" + sec
    } else {
        time + sec
    }
    return time
}

3. 扩展方法

3.1 设置可见性
fun View?.gone() {
  if (this?.visibility != View.GONE) {
    this?.visibility = View.GONE
  }
}

fun View?.visible() {
  if (this?.visibility != View.VISIBLE) {
    this?.visibility = View.VISIBLE
  }
}

fun View?.invisible() {
  if (this?.visibility != View.INVISIBLE) {
    this?.visibility = View.INVISIBLE
  }
}
3.2 设置圆角
fun clipRoundCornerOutline(view: View, value: Float) {
  view.clipToOutline = true
  view.outlineProvider = object : ViewOutlineProvider() {
    override fun getOutline(view: View?, outline: Outline?) {
      outline?.setRoundRect(
        Rect(0, 0, view?.width ?: 0, view?.height ?: 0),
        applyDimension(view?.context, value)
      )
    }

  }
}
3.3 给 Group 设置点击事件
/**
 * 添加点击事件
 */
fun Group.setAllOnClickListener(listener: (view: View) -> Unit) {
  referencedIds.forEach { id ->
    rootView.findViewById<View>(id).setOnClickListener(listener)
  }
}
3.4 Observable转为协程
/**
 * Observable转为协程
 */
@SuppressLint("CheckResult")
suspend fun <T> convertCoroutine(observable: Observable<Response<T>>) =
    suspendCancellableCoroutine<Response<T>> {
        observable.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
            .subscribe(
                { result ->
                    MLog.d("TAG", "convertCoroutine: ")
                    it.resumeWith(Result.success(result))
                },
                { error ->
                    MLog.d("TAG", "convertCoroutine: error = $error")
                    it.resumeWith(Result.success(Response(Response.CODE_FAILED, error.message)))
                },
                {
                    MLog.d("TAG", "convertCoroutine: complete")
                    if (it.isActive) {
                        it.resumeWith(
                            Result.success(Response(Response.CODE_FAILED, "request complete"))
                        )
                    }
                }
            )
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值