1 Star 10 Fork 1

王耀冰 / IceFramework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

IceFramework

Gitee地址,感谢给个star

这是一个基于kotlin、viewmodel、retrofit+协程、ViewBinding的MVVM框架

一、添加依赖

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' }
    }
}
implementation 'com.gitee.ice_king:iceframework:1.0.8'

二、初始化,在application中

IceApplication.statusBarColor=0xff000000.toInt()//设置全局状态栏颜色
IceApplication.init(this)

三、基类介绍

BaseActivity、BaseFragment、BaseViewModel、BaseRecyclerAdapter

1.BaseActivity

获取页面栈

BaseActivity.activityTasks

处理富文本图片宽度为100%

fun getHtmlData(htmlBody: String?): String

已默认处理点击输入框以外区域隐藏输入法

获取View对象,使用viewBinding,例如

    binding.textView

2.BaseFragment

获取View对象,使用viewBinding,例如

    binding.textView

3.BaseViewModel

提示错误

toastMsg.value=""

显示加载中

showProgressBar.value=true

4.BaseRecyclerAdapter

设置数据

adapter.list=arrayListOf()

点击下标

adapter.selectPosition

点击过的下标多个

adapter.selectPositions

点击事件

adapter.onItemClickListener={}

修改视图

viewHolder.binding.textView.text=""

四、网络请求使用

1.初始化

http = Http.config(baseUrl){
    val headers=HashMap<String,String>()
    return@config headers
}

2.声明接口,创建Api Interface文件,写入如下代码

我自己用的ApiResponse代码如下:

class ApiResponse<T>(
        var data: T?,
        var code: Int,
        var msg: String
)
@GET("demo/demo")
    suspend fun test(): ApiResponse<Any>

3.在viewmodel中调用请求

为避免网络请求出现异常导致闪退,建议扩展BaseViewModel,代码如下

suspend fun <T> BaseViewModel.call(isBackground:Boolean=false, job:suspend ()->T):T{
    if(!isBackground){
        this.showProgressBar.value=true
    }
    var apiResponse: ApiResponse<*>?=null
    if(!NetworkUtils.isConnected()){
        apiResponse=ApiResponse(null,201,"当前网络未连接")
    }
    withContext(Dispatchers.IO){
        if(!NetworkUtils.isAvailable()){
            apiResponse=ApiResponse(null,201,"网络链接不可用")
        }
    }
    if(apiResponse!=null){
        if(!isBackground){
            this.showProgressBar.value=false
        }
        return apiResponse as T
    }
    try {
        val res=job()
        if(res is ApiResponse<*>){
            apiResponse=res
            if(apiResponse!!.code==401){
                apiResponse!!.msg="请登录"
                App.needLogin()
            }
            if(apiResponse!!.code==402){
                apiResponse!!.msg="登录状态过期,请重新登录"
                App.needLogin()
            }
        }else{
            apiResponse=ApiResponse(null,201,"Server Error")
        }
    }catch (e:Exception){
        e.printStackTrace()
        //对异常进行判断,这个是我随便写的一点,可以写一个工具类给封装起来
        val msg = when (e) {
            is SocketTimeoutException -> "time out"
            is HttpException -> {
                when (e.code()) {
                    404 -> "Did not find suitable resources"
                    500 -> "Server internal error"
                    else -> e.message()
                }
            }
            is JsonSyntaxException -> "json parsing exception"
            is UnknownHostException -> "network anomaly"
            else -> e.message
        }
        this.toastMsg.value=msg!!
        apiResponse=ApiResponse(null,201, msg)
    }
    if(!isBackground){
        this.showProgressBar.value=false
    }
    return apiResponse as T
}
private val api = App.http.instance<Api>()

viewModelScope.launch {
    val res=call {
        api.test()
    }
}

4.组件文件请求参数

RetrofitUtils.file2MultiPartBody(file)

五、扩展函数

1.加载图片

fun ImageView.load(context:Context,url:String){
    ...
}

fun ImageView.loadBlur(context:Context, url:String){
    ...
}

2.日期和字符串转换

fun Date.format(format:String):String{
    ...
}

fun String.toDate(format:String):Date{
    ...
}

3.防止连续点击

fun View.setOnNotDoubleClickListener(block:(v:View)->Unit){
    ...
}

六、工具类

1.AppInfoUtils(应用信息)

//版本号
fun packageCode(context: Context):Int{
    ...
}
//版本名称
fun packageName(context: Context):String{
    ...
}

2.CacheUtil(缓存)

//缓存大小,返回的是格式化后的大小
fun getTotalCacheSize(context: Context): String {
        ...
    }

//清理所有缓存
fun clearAllCache(context: Context) {
    ...
}

3.MobileCodeUtils(验证码)

//开始倒计时
fun countDown(lifecycleScope:LifecycleCoroutineScope, tv: TextView) {
        ...
    }

4.PermissionUtil(权限)

fun requestPermissions(permission:Array<String>,success:()->Unit,fail:()->Unit){
        ...
    }

七、内置的第三方library,使用方式请自行查看相应文档

1.EventBus,默认已在Activity、Fragment注册
2.状态栏管理:StatusBarUtil
3.工具库 AndroidUtilCode
4.相册选择 PictureSelector
5.轮播图 io.github.youth5201314:banner
6.图片加载 Glide
7.开关切换组件 com.github.zcweng:switch-button
8.日志工具 com.orhanobut:logger,默认已初始化
9.滚轮组件 Android-PickerView
10.flex布局 com.google.android:flexbox
11.WebView DSBridge-Android:x5
12.下拉刷新 SmartRefreshLayout
13.日历组件 com.haibin:calendarview
14.密码输入框 com.github.WGwangguan:SeparatedEditText
15.json工具 fastjson、Gson
16.圆角ImageView com.makeramen:roundedimageview
MIT License Copyright (c) 2021 王耀冰 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

这是一个基于kotlin、viewmodel、retrofit+协程、ViewBinding的MVVM框架 展开 收起
Android 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/ice_king/ice-framework.git
git@gitee.com:ice_king/ice-framework.git
ice_king
ice-framework
IceFramework
master

搜索帮助