Runt
2025-05-18 ff59eb9415d3df520df98e14ef66d9a3d9804c5b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.runt.open.mvi.base.model
 
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.core.content.FileProvider
import androidx.lifecycle.ViewModel
import com.runt.open.mvi.base.BaseActivity
import com.runt.open.mvi.base.LayoutView
import com.runt.open.mvi.data.LoadingState
import com.runt.open.mvi.data.MessageState
import com.runt.open.mvi.data.PopupMessage
import com.runt.open.mvi.retrofit.AndroidScheduler
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
 
/**
 *
 * @author Runt(qingingrunt2010@qq.com)
 * @purpose
 * @date 6/1/24
 */
open abstract class BaseViewModel : ViewModel(){
    var TAG = "";
 
    protected var mActivity : BaseActivity<LayoutView<BaseViewModel> , BaseViewModel>? = null
 
    private val _isLoading = MutableStateFlow(LoadingState())
    val isLoading = _isLoading.asStateFlow()
 
    val messageSetDismiss = {
        _messageState.value = _messageState.value.copy(isVisible = false);
    }
    private val _messageState = MutableStateFlow(MessageState(isVisible = false, setDismiss = { }))
    val messageState = _messageState.asStateFlow()
 
    private val _popupState = MutableStateFlow(PopupMessage(isVisible = false,"",""))
    val popupState = _popupState.asStateFlow()
 
    open fun onCreate(activity : BaseActivity<LayoutView<BaseViewModel> , BaseViewModel>) {
        mActivity = activity
        TAG = javaClass.simpleName;
    }
 
    fun getActivity(): ComponentActivity {
        return mActivity!!;
    }
 
    fun showLoading(message : String = "加载中...") {
        _isLoading.value = LoadingState(isVisible = true,message = message);
    }
 
    fun hideLoading() {
        _isLoading.value = _isLoading.value.copy(isVisible = false);
    }
 
    fun showDialog( title:String = "", message: String = "", confirmText:String = "确定", cancelText:String = "",
        touchOutside:Boolean = true,//空白和系统返回 是否关闭
        showClose:Boolean = false,//显示关闭图标(默认不显示)
        confirmDissmiss:Boolean = true,//点击确定是否关闭
        cancelDissmiss:Boolean = true,//点击取消是否关闭
        onDismissRequest : () -> Unit = {},
        onConfirmRequest : () -> Unit = {}, ){
 
        _messageState.value = MessageState(title = title, message = message, touchOutside = touchOutside, showClose = showClose, confirmText = confirmText,
            cancelText = cancelText, confirmDissmiss = confirmDissmiss, cancelDissmiss = cancelDissmiss, onDismissRequest = onDismissRequest,
            onConfirmRequest = onConfirmRequest, setDismiss = messageSetDismiss);
    }
 
    fun showPopupWindow(title : String,message : String){
        _popupState.value = PopupMessage(isVisible = true,title,message)
    }
 
    fun hidePopupWindow(){
        _popupState.value = _popupState.value.copy(isVisible = false);
    }
 
    /**
     * 获取用户信息
     */
    /*fun getUserBean() {
        httpObserverOn(commonApi.getUserBean() , object : HttpObserver<UserBean?>() {
            protected fun onSuccess(data : UserBean?) {
                UserBean.setUser(data)
            }
        })
    }*/
 
    /**
     * 随机字符串
     * @param phone
     * @param time
     * @return
     */
    fun randomString(phone : String , time : String) : String {
        val p = Math.round(phone.length / 6.0).toInt()
        val t = time.length / 6
        val list : MutableList<String> = ArrayList()
        for (i in 0 .. 5) {
            var str = ""
            str = if (i * p > phone.length) {
                phone.substring((i - 1) * p)
            } else if ((i + 1) * p > phone.length) {
                phone.substring(i * p)
            } else {
                phone.substring(i * p , (i + 1) * p)
            }
            val num = (str.toInt() * time.toLong()).toString() + ""
            list.add(num)
        } //return sb.toString();
        return plusSingle2(list)
    }
 
    private fun plusSingle2(list : List<String>) : String {
        val sb = StringBuilder()
        for (i in list.indices) {
            sb.append(list[i].substring(if (list[i].length - 2 < 0) 0 else list[i].length - 2))
        }
        return sb.toString()
    }
 
 
    private fun downloadApk(url : String) {
        val request : Request = Request.Builder().url(url).build()
        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call : okhttp3.Call , response : Response) {
                val code : Int = response.code
                Log.d("downloadFile" , "code:$code")
                if (code != 200) {
                    mActivity!!.showToast("下载失败$code")
                    return
                }
                var fos : FileOutputStream? = null
                var input : InputStream? = null
                var file : File? = null
                val buf = ByteArray(2048)
                var len = 0
                try {
                    Log.d("downloadFile" , "下载文件:$url")
                    input = response.body!!.byteStream()
                    val total : Long = response.body!!.contentLength()
                    Log.d("downloadFile" , "total:$total")
                    file = File(mActivity!!.getExternalFilesDir(null)!!.getAbsolutePath() + "/")
                    if (! file !!.exists()) {
                        file.mkdirs()
                    }
                    file = File(file.path + "/" + url.substring(url.lastIndexOf("/")))
                    file.createNewFile()
                    fos = FileOutputStream(file)
                    var sum : Long = 0
                    while (input.read(buf).also { len = it } != - 1) {
                        fos.write(buf , 0 , len)
                        sum += len.toLong()
                        val progress = sum * 1.0f / total * 100 // 下载中
                        Log.d("downloadFile" , "下载进度:$progress")
                    }
                    Log.d("downloadFile" , file.path) // 下载完成
                    val finalFile : File = file
                    mActivity!!.runOnUiThread {
                        /*mActivity!!.showDialog("安装" , "新版安装包下载完成" , "安装" , "取消" , object : ResPonse() {
                            fun doSuccess(obj : Any?) {
                                openAPK(finalFile)
                            }
                        })*/
                    }
                } catch (e : Exception) {
                    Log.d("downloadFileException" , e.toString())
                    Log.d("downloadFileException" , "Url:\${call.request().url}")
                    mActivity!!.showToast("下载失败$e")
                    file !!.delete()
                    call.cancel()
                } finally {
                    fos !!.flush()
                    input !!.close()
                    fos.close()
                }
            }
 
            override fun onFailure(call : okhttp3.Call , e : IOException) {
                mActivity!!.showToast("下载失败$e")
            }
 
        })
    }
 
    /**
     * 打开apk文件
     *
     * @param file
     */
    private fun openAPK(file : File) {
        val intent = Intent()
        intent.setAction(Intent.ACTION_VIEW)
        var photoURI : Uri? = null
        photoURI = if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            FileProvider.getUriForFile(mActivity!! , mActivity!!.getApplicationContext().getPackageName() + ".fileprovider" , file) //添加这一句表示对目标应用临时授权该Uri所代表的文件
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            Uri.fromFile(file)
        }
        intent.setDataAndType(photoURI , "application/vnd.android.package-archive")
        mActivity!!.startActivity(intent)
    }
 
    /**
     * 网络请求观察
     * @param observable
     * @param <T>
     * @return
    </T> */
    fun <T> httpObserverOn(observable : Observable<T> , observer : Observer<T>) {
        observable.subscribeOn(Schedulers.io()) //指定网络请求在io后台线程中进行
            .observeOn(AndroidScheduler.mainThread()).subscribe(observer)
    }
 
 
    /**
     * 网络请求观察(加载框)
     * @param observable
     * @param <T>
     * @return
    </T> */
    fun <T> httpObserverOnLoading(observable : Observable<T> , observer : Observer<T>) {
        observable.subscribeOn(Schedulers.io()) //指定网络请求在io后台线程中进行
            .doOnSubscribe { disposable ->
                //mActivity.showLoadingDialog("")
            }
            .observeOn(AndroidScheduler.mainThread()).doOnError { throwable ->
                //mActivity.dissmissLoadingDialog()
                Log.e("ViewModel" , hashCode().toString() + " httpObserverOnLoading " + throwable)
            }.doOnComplete {
                //mActivity.dissmissLoadingDialog()
            }.subscribe(observer)
    }
 
    fun getFilePathFromUri(uri:Uri):String?{
        var filePath = "";
        var filePathColumn = arrayOf(MediaStore.Video.Media.DATA)
        var cursor = mActivity!!.contentResolver.query(uri!!,filePathColumn,null,null,null)
        cursor?.let {
            it.moveToFirst()
            var index = it.getColumnIndex(filePathColumn[0])
            if(index > -1 && index < it.columnCount){
                filePath = it.getString(index)
            }
            it.close();
        }
        var file = File(filePath);
        Log.i(TAG,"文件是否存在=${file.exists()} file=${filePath} ")
        if(file.exists()){
            try {
                var inputStream = FileInputStream(file)
                inputStream.close();
                return filePath;
            }catch (e:Exception){
                return null;
            }
        }else{
            return null;
        }
    }
}