Browse Source

优化

master
suliang 2 years ago
parent
commit
1290a3799e

+ 1
- 0
.idea/gradle.xml View File

@@ -17,6 +17,7 @@
<option value="$PROJECT_DIR$/videoplayer" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>

+ 27
- 34
app/src/main/java/com/xkl/cdl/module/splash/SplashActivity.kt View File

@@ -58,24 +58,10 @@ class SplashActivity : BaseActivityVM<ActivitySplashBinding, SplashViewModel>()
override fun initActivity(savedInstanceState : Bundle?) {
vm.handler = Handler(Looper.getMainLooper(),object :Handler.Callback{
override fun handleMessage(msg : Message) : Boolean {
when(msg.what){
1 -> { //进度
println("接收:${ msg.obj as String}")
binding.tvShowMsg.text = msg.obj as String
}
2 -> { //数据合并完成
println("接收:合并完成")
binding.tvShowMsg.text = "资源数据检测中......"
vm.copyResource()
}
}
return true
}
})
// 网络连接无效
vm.netWorkLiveData.observe(this){
netNotWorkDialog.show(supportFragmentManager,"netNotWork")
}
//账号过期
vm.isExpirationMutableLiveData.observe(this) {
expirationDialog.show(supportFragmentManager, "expiration")
@@ -92,19 +78,11 @@ class SplashActivity : BaseActivityVM<ActivitySplashBinding, SplashViewModel>()
vm.downLoadResult.observe(this) {
downLoadFailedDialog.show(supportFragmentManager, "download")
}
// vm.downLoadNameAndProgress.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback(){
// override fun onPropertyChanged(sender : Observable?, propertyId : Int) {
// AppExecutors.mainThread.execute {
// binding.tvShowMsg.text = vm.downLoadNameAndProgress.get()
// }
// }
//
// })
//下载进度
vm.downLoadNameAndProgress.observe(this){
println("Activity 接收: $it")
binding.tvShowMsg.text = it
}
//
//课程包合并结束
vm.mergeCoursePackResult.observe(this) {
binding.tvShowMsg.text = "资源数据检测中......"
@@ -182,6 +160,27 @@ class SplashActivity : BaseActivityVM<ActivitySplashBinding, SplashViewModel>()
}
/***
* 网络连接无效, 重新走流程 先获取课程再下载课程
*/
private val netNotWorkDialog : CommonDialog by lazy {
val commonDialogBean = CommonDialogBean(titleTextValue = "警告",
contentTextValue = "网络连接失败,请检查网络后重试!",
rightTextValue = "重试", leftTextValue = "退出")
CommonDialog.newInstance(commonDialogBean).apply {
onCommonDialogButtonClickListener = { dialog, isRightClick ->
dialog.dismissAllowingStateLoss()
if (isRightClick) {
vm.loadBindCourse()
} else { //退出应用
finish()
exitProcess(0)
}
}
}
}
/**
* 课程已过期
*/
@@ -233,10 +232,4 @@ class SplashActivity : BaseActivityVM<ActivitySplashBinding, SplashViewModel>()
}
}
override fun finish() {
vm.handler.removeCallbacksAndMessages(null)
super.finish()
}
}

+ 31
- 41
app/src/main/java/com/xkl/cdl/module/splash/SplashViewModel.kt View File

@@ -7,6 +7,7 @@ import com.suliang.common.util.LogUtil
import com.suliang.common.util.file.FileUtil
import com.suliang.common.util.net.DownLoadFileListener
import com.suliang.common.util.net.HttpUtil
import com.suliang.common.util.net.NetworkUtil
import com.suliang.common.util.thread.AppExecutors
import com.xkl.cdl.data.AppConstants
import com.xkl.cdl.data.bean.course.Course
@@ -21,10 +22,8 @@ import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.json.JSONObject
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

/**
@@ -34,11 +33,10 @@ import java.util.zip.ZipFile
*/
class SplashViewModel : BaseViewModel() {
lateinit var handler : Handler
//账号是否过期
var isExpirationMutableLiveData = MutableLiveData<Boolean>()
val isExpirationMutableLiveData = MutableLiveData<Boolean>()
//网络连接无效
val netWorkLiveData = MutableLiveData<Boolean>()
//绑定课程的结果
var bindCoursePackList : List<CoursePack>? = null
val bindCoursePackResult : MutableLiveData<Boolean> = MutableLiveData()
@@ -46,9 +44,6 @@ class SplashViewModel : BaseViewModel() {
//下载课程与进度
val downLoadNameAndProgress : MutableLiveData<String> = MutableLiveData()
// val downLoadNameAndProgress = ObservableField<String>()
var progress = ""
//下载课程失败
val downLoadResult : MutableLiveData<Boolean> = MutableLiveData()
@@ -71,7 +66,6 @@ class SplashViewModel : BaseViewModel() {
* @return
*/
fun loadBindCourse() {
val deviceId = UserInfoManager.instance.getUuid()
val licenceId = UserInfoManager.instance.getLicence()
@@ -100,22 +94,21 @@ class SplashViewModel : BaseViewModel() {
UserInfoManager.instance.putIsExpiration(false) //成功 账号没有过期
bindCoursePackList = result
bindCoursePackResult.postValue(true)
return
}
20002 -> {
isExpirationMutableLiveData.postValue(true)
UserInfoManager.instance.putIsExpiration(true) //账号已经过期了
return
}
}
} catch (e : Exception) {
e.printStackTrace()
}
}else{
//其他错误,拿本地数据
LogUtil.e("$response")
bindCoursePackList = decodeBinderCourseJson(UserInfoManager.instance.getBindCourse())
bindCoursePackResult.postValue(false)
}
//其他错误,拿本地数据
LogUtil.e("$response")
bindCoursePackList = decodeBinderCourseJson(UserInfoManager.instance.getBindCourse())
bindCoursePackResult.postValue(false)
}
})
}
@@ -125,6 +118,8 @@ class SplashViewModel : BaseViewModel() {
if (json.isEmpty()) return null
val result = mutableListOf<CoursePack>()
val jo = JSONObject(json)
if (!jo.has("course_pack"))
return result
val cpList = jo.getJSONArray("course_pack") //课程包数组
for (i in 0 until cpList.length()) {
val cp = cpList.getJSONObject(i) // 课程包
@@ -182,7 +177,6 @@ class SplashViewModel : BaseViewModel() {
*/
fun checkCoursePack() {
AppExecutors.io.execute {
//需要下载的数量
@@ -211,6 +205,12 @@ class SplashViewModel : BaseViewModel() {
val lock : String = ""
//默认下载成功,当有false时,说明下载有失败
var downLoadSuccess = true
if (needCount > 0 && !NetworkUtil.isConnected() ){
// 网络连接不可用
netWorkLiveData.postValue(true)
return@execute
}
println("开始下载")
//下载课程
bindCoursePackList?.forEach { coursePackBaseInfo ->
@@ -223,12 +223,7 @@ class SplashViewModel : BaseViewModel() {
}
override fun downFileProgress(progress : Int) {
downLoadNameAndProgress.postValue("课程下载中: ${coursePackBaseInfo.coursePackName} : $progress%")
// println("发送:${coursePackBaseInfo.coursePackName} : $progress% ")
// val obtainMessage = handler.obtainMessage()
// obtainMessage.obj = "课程下载中: ${coursePackBaseInfo.coursePackName} : $progress%"
// obtainMessage.what = 1
// handler.sendMessage(obtainMessage)
downLoadNameAndProgress.postValue("课程下载中:\n${coursePackBaseInfo.coursePackName} : $progress%")
}
override fun downFileResult(saveFile : File?) {
@@ -258,9 +253,7 @@ class SplashViewModel : BaseViewModel() {
println("开始判断是否下载完成")
//判断下载是否成功
if (!downLoadSuccess) { //下载失败、弹窗提示
AppExecutors.mainThread.execute {
downLoadResult.value = false
}
downLoadResult.postValue(false)
return@execute
}
@@ -293,13 +286,7 @@ class SplashViewModel : BaseViewModel() {
}
CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_ENGLISH] = englishCoursePack.toList()
CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_CHINESE] = chineseCoursePack.toList()
// println("发送:合并完成! ")
// handler.sendEmptyMessage(2)
mergeCoursePackResult.postValue(true)
/* AppExecutors.mainThread.execute {
mergeCoursePackResult.value = true
}
*/
}
}
@@ -323,7 +310,7 @@ class SplashViewModel : BaseViewModel() {
}
}
@Throws(IOException::class)
/* @Throws(IOException::class)
private fun unZipFile(f : File, coursePack : CoursePack) {
val zipFile = ZipFile(f)
val entries = zipFile.entries()
@@ -399,19 +386,22 @@ class SplashViewModel : BaseViewModel() {
}
}
}
}
}*/
@Throws(IOException::class)
private fun unZipFile_1(f : File, coursePack : CoursePack) {
val zipFile = ZipFile(f)
val entries = zipFile.entries()
val buffer = ByteArray(1024 * 1024 * 5)
var nextEntry : ZipEntry
var count = 0
var zipInputStream : InputStream
var outPut : Array<BufferedOutputStream>
while (entries.hasMoreElements()) {
val nextEntry = entries.nextElement()
val zipInputStream = zipFile.getInputStream(nextEntry)
val buffer = ByteArray(1024 * 1024 * 5)
var count = 0
val outPut : Array<BufferedOutputStream> = when {
nextEntry = entries.nextElement()
zipInputStream = zipFile.getInputStream(nextEntry)
outPut = when {
nextEntry.name.endsWith(("cover.png")) -> {
Array<BufferedOutputStream>(1) {
val fileName = File(FilePathManager.getIconRootPath(),

Loading…
Cancel
Save