AppVersionUtil.kt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.develop.common.utils
  2. import com.azhon.appupdate.util.LogUtil
  3. import com.blankj.utilcode.util.FileUtils
  4. import com.blankj.utilcode.util.ZipUtils
  5. import com.develop.base.ext.fromJson
  6. import com.develop.base.util.FileKit
  7. import com.develop.base.util.TopResumedAtyHolder
  8. import com.develop.common.data_repo.FoodDataProvider
  9. import com.develop.common.data_repo.db.entity.DevVersion
  10. import com.develop.common.data_repo.net.Api
  11. import com.develop.common.data_repo.net.model.request.DeviceInfoBody
  12. import com.develop.common.data_repo.net.model.response.DevInfoResult
  13. import com.develop.common.data_repo.net.model.response.RecipeDataConfig
  14. import com.develop.common.dialog.RecipeUpdateDialog
  15. import com.drake.net.Get
  16. import com.drake.net.Post
  17. import com.drake.net.component.Progress
  18. import com.drake.net.interfaces.ProgressListener
  19. import com.drake.net.utils.scopeNetLife
  20. import java.io.File
  21. import java.util.*
  22. object AppVersionUtil {
  23. fun checkRecipeUpdate(shoNoUpdateDialog: Boolean = false) {
  24. TopResumedAtyHolder.getCurrentActivity()?.apply {
  25. scopeNetLife {
  26. try {
  27. val result = Post<DevInfoResult>(Api.DEV_INFO) {
  28. body = DeviceInfoBody.genDeviceInfoBody()
  29. }.await()
  30. val downloadDir = this@apply.externalCacheDir.toString()
  31. val downloadName = System.nanoTime().toString()
  32. val recipeUpdateTime = result.recipeUpdateTime
  33. val newRecipes = LinkedList(result.newRecipes)
  34. if (newRecipes.isEmpty() && shoNoUpdateDialog) {
  35. val dialog = RecipeUpdateDialog()
  36. dialog.onDialogClickListener =
  37. object : RecipeUpdateDialog.OnDialogClickListener {
  38. override fun onConfirm() {
  39. }
  40. override fun onCancel() {
  41. }
  42. }
  43. dialog.showNoUpdateTips(supportFragmentManager, "RECIPE_UPDATE_DIALOG")
  44. }
  45. if (newRecipes.isNotEmpty()) {
  46. val dialog = RecipeUpdateDialog()
  47. dialog.onDialogClickListener =
  48. object : RecipeUpdateDialog.OnDialogClickListener {
  49. override fun onConfirm() {
  50. downloadRecipes(
  51. newRecipes,
  52. dialog,
  53. newRecipes.size.toLong(),
  54. downloadDir,
  55. downloadName,
  56. recipeUpdateTime
  57. )
  58. }
  59. override fun onCancel() {
  60. }
  61. }
  62. dialog.showUpdateTips(
  63. supportFragmentManager, "RECIPE_UPDATE_DIALOG", newRecipes.size.toLong()
  64. )
  65. }
  66. }catch (e:java.lang.Exception){
  67. e.printStackTrace()
  68. }
  69. }
  70. }
  71. }
  72. private fun downloadRecipes(
  73. newRecipes: LinkedList<String>,
  74. recipeUpdateDialog: RecipeUpdateDialog,
  75. sum: Long,
  76. downloadDir: String,
  77. downloadName: String,
  78. recipeUpdateTime: Long?
  79. ) {
  80. TopResumedAtyHolder.getCurrentActivity()?.apply {
  81. if (newRecipes.isEmpty()) {
  82. recipeUpdateDialog.removeSelf()
  83. showRecipesUpdateDialog(recipeUpdateTime)
  84. return
  85. }
  86. val recipeUrl = newRecipes.pop()
  87. val recipeNumber = recipeUrl.split("@")[0]
  88. val fileUrl = recipeUrl.split("@")[1]
  89. recipeUpdateDialog.showUpdating(supportFragmentManager, "", sum - newRecipes.size, sum)
  90. scopeNetLife {
  91. val result = Get<File>(fileUrl) {
  92. setDownloadFileName(downloadName)
  93. setDownloadDir(downloadDir)
  94. addDownloadListener(object : ProgressListener() {
  95. override fun onProgress(p: Progress) {
  96. LogUtil.d("dddddd", p.toString())
  97. }
  98. })
  99. }.await()
  100. if (!result.exists()) {
  101. downloadRecipes(
  102. newRecipes,
  103. recipeUpdateDialog,
  104. sum,
  105. downloadDir,
  106. downloadName,
  107. recipeUpdateTime
  108. )
  109. } else {
  110. prepareResource(result, recipeNumber)
  111. downloadRecipes(
  112. newRecipes,
  113. recipeUpdateDialog,
  114. sum,
  115. downloadDir,
  116. downloadName,
  117. recipeUpdateTime
  118. )
  119. }
  120. }
  121. }
  122. }
  123. private fun prepareResource(file: File, recipeNumber: String) {
  124. try {
  125. val dst = ZipUtils.unzipFile(file, FoodDataProvider.getExtRecipeResourceDir())
  126. FileUtils.delete(file)
  127. if (dst.isNullOrEmpty()) {
  128. return
  129. }
  130. val jsonFile = FoodDataProvider.getResourceConfigJsonPath(recipeNumber)
  131. if (!jsonFile.exists()) {
  132. return
  133. }
  134. val jsonContent = FileKit.readFileToString(jsonFile)
  135. val contentData = jsonContent.fromJson() as RecipeDataConfig
  136. contentData.resetAllCodes()
  137. FoodDataProvider.getDatabase().runInTransaction {
  138. FoodDataProvider.getDatabase().recipeDao().apply {
  139. deleteDevRecipeAccessorys(recipeNumber)
  140. deleteDevRecipeCookingSteps(recipeNumber)
  141. deleteDevRecipeFoods(recipeNumber)
  142. deleteDevRecipeNutritions(recipeNumber)
  143. deleteDevRecipeRelTags(recipeNumber)
  144. deleteDevRecipePortionSizes(recipeNumber)
  145. deleteRecipe(recipeNumber)
  146. insertDevAccessorys(contentData.devAccessorys)
  147. insertHotTags(contentData.devHotTags)
  148. insertDevPortraits(contentData.devPortraits)
  149. insertDevRecipeAccessorys(contentData.devRecipeAccessorys)
  150. insertDevRecipeCategorys(contentData.devRecipeCategorys)
  151. insertDevRecipeCookingSteps(contentData.devRecipeCookingSteps)
  152. insertDevRecipeFoods(contentData.devRecipeFoods)
  153. insertDevRecipeNutritions(contentData.devRecipeNutritions)
  154. insertDevRecipePortionSizes(contentData.devRecipePortionSizes)
  155. insertDevRecipeRelTags(contentData.devRecipeRelTags)
  156. insertDevRecipeTags(contentData.devRecipeTags)
  157. insertDevRecipes(contentData.devRecipes)
  158. }
  159. }
  160. } catch (e: Exception) {
  161. e.printStackTrace()
  162. }
  163. }
  164. private fun showRecipesUpdateDialog(recipeUpdateTime: Long?) {
  165. TopResumedAtyHolder.getCurrentActivity()?.apply {
  166. RecipeUpdateDialog().apply {
  167. onDialogClickListener = object : RecipeUpdateDialog.OnDialogClickListener {
  168. override fun onConfirm() {
  169. val v = FoodDataProvider.getUserDatabase().devConfigDao().recipeVersion()
  170. ?: DevVersion(0, 0)
  171. v.recipeUpdateTime = recipeUpdateTime
  172. FoodDataProvider.getUserDatabase().devConfigDao().saveDevVersion(v)
  173. removeSelf()
  174. }
  175. override fun onCancel() {
  176. }
  177. }
  178. showSuccess(supportFragmentManager, "recipeUpdateDialog")
  179. }
  180. }
  181. }
  182. }