# IceFramework **Repository Path**: ice_king/ice-framework ## Basic Information - **Project Name**: IceFramework - **Description**: 这是一个基于kotlin、viewmodel、retrofit+协程、ViewBinding的MVVM框架 - **Primary Language**: Android - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 10 - **Forks**: 1 - **Created**: 2021-11-24 - **Last Updated**: 2023-11-26 ## Categories & Tags **Categories**: android-modules **Tags**: jetpack, Android, Kotlin, Framework, MVVM ## README # IceFramework [Gitee地址,感谢给个star](https://gitee.com/ice_king/ice-framework) ### 这是一个基于kotlin、viewmodel、retrofit+协程、ViewBinding的MVVM框架 ## 一、添加依赖 ```gradle allprojects { repositories { google() jcenter() mavenCentral() maven { url 'https://jitpack.io' } maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' } } } ``` ```gradle implementation 'com.gitee.ice_king:iceframework:1.0.8' ``` ## 二、初始化,在application中 ```kotlin IceApplication.statusBarColor=0xff000000.toInt()//设置全局状态栏颜色 IceApplication.init(this) ``` ## 三、基类介绍 #### BaseActivity、BaseFragment、BaseViewModel、BaseRecyclerAdapter ### 1.BaseActivity #### 获取页面栈 ```kotlin BaseActivity.activityTasks ``` #### 处理富文本图片宽度为100% ```kotlin fun getHtmlData(htmlBody: String?): String ``` #### 已默认处理点击输入框以外区域隐藏输入法 #### 获取View对象,使用viewBinding,例如 ```kotlin binding.textView ``` ### 2.BaseFragment #### 获取View对象,使用viewBinding,例如 ```kotlin binding.textView ``` ### 3.BaseViewModel #### 提示错误 ```kotlin toastMsg.value="" ``` #### 显示加载中 ```kotlin showProgressBar.value=true ``` ### 4.BaseRecyclerAdapter #### 设置数据 ```kotlin adapter.list=arrayListOf() ``` #### 点击下标 ```kotlin adapter.selectPosition ``` #### 点击过的下标多个 ```kotlin adapter.selectPositions ``` #### 点击事件 ```kotlin adapter.onItemClickListener={} ``` #### 修改视图 ```kotlin viewHolder.binding.textView.text="" ``` ## 四、网络请求使用 ### 1.初始化 ```kotlin http = Http.config(baseUrl){ val headers=HashMap() return@config headers } ``` ### 2.声明接口,创建Api Interface文件,写入如下代码 #### 我自己用的ApiResponse代码如下: ```kotlin class ApiResponse( var data: T?, var code: Int, var msg: String ) ``` ```kotlin @GET("demo/demo") suspend fun test(): ApiResponse ``` ### 3.在viewmodel中调用请求 #### 为避免网络请求出现异常导致闪退,建议扩展BaseViewModel,代码如下 ```kotlin suspend fun 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 } ``` ```kotlin private val api = App.http.instance() viewModelScope.launch { val res=call { api.test() } } ``` #### 4.组件文件请求参数 ```kotlin RetrofitUtils.file2MultiPartBody(file) ``` ## 五、扩展函数 ### 1.加载图片 ```kotlin fun ImageView.load(context:Context,url:String){ ... } fun ImageView.loadBlur(context:Context, url:String){ ... } ``` ### 2.日期和字符串转换 ```kotlin fun Date.format(format:String):String{ ... } fun String.toDate(format:String):Date{ ... } ``` ### 3.防止连续点击 ```kotlin fun View.setOnNotDoubleClickListener(block:(v:View)->Unit){ ... } ``` ## 六、工具类 ### 1.AppInfoUtils(应用信息) ```kotlin //版本号 fun packageCode(context: Context):Int{ ... } //版本名称 fun packageName(context: Context):String{ ... } ``` ### 2.CacheUtil(缓存) ```kotlin //缓存大小,返回的是格式化后的大小 fun getTotalCacheSize(context: Context): String { ... } //清理所有缓存 fun clearAllCache(context: Context) { ... } ``` ### 3.MobileCodeUtils(验证码) ```kotlin //开始倒计时 fun countDown(lifecycleScope:LifecycleCoroutineScope, tv: TextView) { ... } ``` ### 4.PermissionUtil(权限) ```kotlin fun requestPermissions(permission:Array,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