1. 文件相关
1.1 根据文件路径获取文件名
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
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转为协程
@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" ) )
)
}
}
)
}