RustAssistant/app/src/main/java/com/coldmint/rust/pro/WebModInfoActivity.kt

420 lines
16 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.*
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import androidx.core.view.isVisible
2022-07-17 13:51:14 +00:00
import androidx.recyclerview.widget.DividerItemDecoration
2022-04-04 14:03:10 +00:00
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.LayoutMode
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
import com.afollestad.materialdialogs.bottomsheets.setPeekHeight
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.input.getInputField
import com.afollestad.materialdialogs.input.input
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
2022-09-01 08:47:39 +00:00
import com.coldmint.dialog.CoreDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.ModClass
import com.coldmint.rust.core.dataBean.ApiResponse
2022-09-30 11:39:54 +00:00
import com.coldmint.rust.core.dataBean.AppUpdateData
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.dataBean.mod.WebModCommentData
import com.coldmint.rust.core.dataBean.mod.WebModInfoData
import com.coldmint.rust.core.dataBean.mod.WebModUpdateLogData
import com.coldmint.rust.core.dataBean.user.SpaceInfoData
import com.coldmint.rust.core.interfaces.ApiCallBack
import com.coldmint.rust.core.tool.AppOperator
import com.coldmint.rust.core.tool.FileLoader
import com.coldmint.rust.core.tool.LineParser
import com.coldmint.rust.core.tool.ProgressResponseBody
import com.coldmint.rust.core.web.ServerConfiguration
import com.coldmint.rust.core.web.User
import com.coldmint.rust.core.web.WebMod
import com.coldmint.rust.pro.adapters.CommentAdapter
2022-08-31 06:34:45 +00:00
import com.coldmint.rust.pro.adapters.ModPageDetailsAdapter
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.databinding.ActivityWebModInfoBinding
import com.coldmint.rust.pro.tool.AppSettings
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.LoadFileLayoutBinding
2022-07-16 14:41:37 +00:00
import com.coldmint.rust.pro.dialog.CommentDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.tool.GlobalMethod
import com.coldmint.rust.pro.tool.TextStyleMaker
2022-08-31 06:34:45 +00:00
import com.google.android.material.appbar.AppBarLayout
2022-04-04 14:03:10 +00:00
import com.google.android.material.snackbar.Snackbar
2022-08-31 06:34:45 +00:00
import com.google.android.material.tabs.TabLayoutMediator
2022-09-30 11:39:54 +00:00
import com.google.gson.Gson
2022-04-04 14:03:10 +00:00
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import java.io.File
import java.text.NumberFormat
class WebModInfoActivity : BaseActivity<ActivityWebModInfoBinding>() {
lateinit var modId: String
lateinit var tip: String
val targetFile: File by lazy {
2022-08-14 02:43:53 +00:00
val modFolderPath = AppSettings.getValue(
2022-04-04 14:03:10 +00:00
AppSettings.Setting.ModFolder,
Environment.getExternalStorageDirectory().absolutePath + "/rustedWarfare/units/"
)
val modFolder = File(modFolderPath)
if (!modFolder.exists()) {
modFolder.mkdirs()
}
val modFilePath = "$modFolderPath$modId.rwmod"
File(modFilePath)
}
2022-07-09 16:33:10 +00:00
val token by lazy {
2022-08-14 02:43:53 +00:00
AppSettings.getValue(AppSettings.Setting.Token, "")
2022-04-04 14:03:10 +00:00
}
2022-09-01 08:47:39 +00:00
lateinit var adapter: ModPageDetailsAdapter
2022-04-04 14:03:10 +00:00
private fun initView() {
setReturnButton()
val activityIntent = intent
val bundle = activityIntent.getBundleExtra("data")
if (bundle == null) {
showError("意外的请求")
return
} else {
val name = bundle.getString("modName")
title = name
val temId = bundle.getString("modId")
if (temId == null) {
showError("未知的模组id")
return
}
modId = temId
if (targetFile.exists()) {
2022-04-24 14:20:26 +00:00
viewBinding.button.isEnabled = false
2022-04-04 14:03:10 +00:00
viewBinding.button.text = getString(R.string.installated)
}
2022-08-31 06:34:45 +00:00
adapter = ModPageDetailsAdapter(this, modId)
2022-09-01 08:47:39 +00:00
adapter.modName.observe(this) {
title = it
}
2022-08-31 06:34:45 +00:00
viewBinding.viewPager2.adapter = adapter
TabLayoutMediator(viewBinding.tabLayout, viewBinding.viewPager2) { tab, i ->
tab.text = when (i) {
0 -> {
getString(R.string.details)
2022-04-04 14:03:10 +00:00
}
2022-08-31 06:34:45 +00:00
1 -> {
getString(R.string.insert_coins)
2022-04-04 14:03:10 +00:00
}
2022-08-31 06:34:45 +00:00
2 -> {
getString(R.string.discussion)
2022-04-04 14:03:10 +00:00
}
2022-08-31 06:34:45 +00:00
else -> {
getString(R.string.title)
2022-04-04 14:03:10 +00:00
}
}
2022-08-31 06:34:45 +00:00
}.attach()
2022-09-01 08:47:39 +00:00
viewBinding.button.setOnClickListener {
val type = viewBinding.button.text
val installation = getString(R.string.installation)
when (type) {
installation -> {
downloadAction(adapter.getLink())
}
}
}
2022-08-31 06:34:45 +00:00
tip = getString(R.string.file_download_progress)
}
2022-04-04 14:03:10 +00:00
}
/**
* 下载事件
* @param t WebModInfoData
*/
2022-09-01 08:47:39 +00:00
fun downloadAction(link: String?) {
if (link == null) {
return
}
val fileLink = ServerConfiguration.getRealLink(link)
2022-04-04 14:03:10 +00:00
when (AppOperator.getNetworkType(this)) {
AppOperator.NetWorkType.NetWorkType_Moble -> {
val useMobileNetWork =
2022-08-14 02:43:53 +00:00
AppSettings.getValue(AppSettings.Setting.UseMobileNetwork, false)
2022-04-04 14:03:10 +00:00
if (useMobileNetWork) {
downloadWork(fileLink)
} else {
2022-09-01 08:47:39 +00:00
CoreDialog(this).setTitle(R.string.using_mobile_networks)
.setMessage(R.string.using_mobile_networks_msg)
.setPositiveButton(R.string.only_one) {
downloadWork(fileLink)
}.setNegativeButton(R.string.always_allow) {
2022-08-14 02:43:53 +00:00
AppSettings.setValue(AppSettings.Setting.UseMobileNetwork, true)
2022-04-04 14:03:10 +00:00
downloadWork(fileLink)
2022-09-01 08:47:39 +00:00
}.setNeutralButton(R.string.dialog_cancel) {
2022-04-04 14:03:10 +00:00
2022-09-01 08:47:39 +00:00
}.show()
2022-04-04 14:03:10 +00:00
}
}
AppOperator.NetWorkType.NetWorkType_Wifi -> {
downloadWork(fileLink)
}
2022-07-08 14:34:22 +00:00
else -> {}
2022-04-04 14:03:10 +00:00
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu_webmod, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.report_item -> {
if (token.isBlank()) {
Snackbar.make(
viewBinding.button,
R.string.please_login_first,
Snackbar.LENGTH_SHORT
).show()
return true
}
2022-08-31 06:34:45 +00:00
if (adapter.isOpen()) {
2022-07-09 16:33:10 +00:00
val thisIntent = Intent(this, ReportActivity::class.java)
val bundle = Bundle()
bundle.putString("target", modId)
bundle.putString("type", "mod")
2022-08-31 06:34:45 +00:00
bundle.putString("name", title.toString())
2022-07-09 16:33:10 +00:00
thisIntent.putExtra("data", bundle)
startActivity(thisIntent)
2022-04-04 14:03:10 +00:00
} else {
//不能举报未公开的模组
Snackbar.make(
viewBinding.button,
R.string.unable_to_report,
Snackbar.LENGTH_SHORT
).show()
}
}
2022-09-30 11:39:54 +00:00
R.id.share_item -> {
val link = AppSettings.getValue(AppSettings.Setting.ServerAddress, "")
val updateData = AppSettings.getValue(AppSettings.Setting.UpdateData, "")
var appUpdateLink = ""
if (!updateData.isNullOrBlank()) {
val gson = Gson()
val updateDataObj = gson.fromJson(updateData, AppUpdateData.Data::class.java)
appUpdateLink = updateDataObj.link
}
val s = String.format(
getString(R.string.share_mod_msg),
title,
link + "website/pages/modPage.php?&modId=" + modId, appUpdateLink
)
AppOperator.shareText(this, getString(R.string.share_mod), s)
2022-09-30 11:39:54 +00:00
}
2022-04-04 14:03:10 +00:00
R.id.update_record -> {
GlobalMethod.showUpdateLog(this, modId)
}
}
return super.onOptionsItemSelected(item)
}
/**
* 下载工作
* @param fileLink String
*/
fun downloadWork(fileLink: String) {
2022-04-11 05:20:24 +00:00
GlobalMethod.requestStoragePermissions(this) {
if (it) {
viewBinding.button.setText(R.string.installation_ing)
val loadFileLayoutBinding = LoadFileLayoutBinding.inflate(layoutInflater)
loadFileLayoutBinding.LinearProgressIndicator.max = 100
var progress = 0
2022-04-04 14:03:10 +00:00
2022-04-11 05:20:24 +00:00
val fileLoader = FileLoader.getInstantiate(fileLink, targetFile.absolutePath)
fileLoader.download(object : ProgressResponseBody.ResponseProgressListener {
override fun update(bytesRead: Long, contentLength: Long, done: Boolean) {
//计算百分比并更新ProgressBar
val numberFormat = NumberFormat.getNumberInstance()
numberFormat.maximumFractionDigits = 2
val trueProgress =
numberFormat.format(bytesRead.toDouble() / contentLength.toDouble() * 100)
progress = trueProgress.toFloat().toInt()
runOnUiThread {
val progressTip = String.format(tip, progress)
viewBinding.button.text = progressTip
}
}
2022-04-04 14:03:10 +00:00
2022-04-11 05:20:24 +00:00
override fun downloadFail(exception: Exception?) {
viewBinding.button.setText(R.string.installation)
}
2022-04-04 14:03:10 +00:00
2022-04-11 05:20:24 +00:00
override fun downloadSuccess() {
2022-04-24 14:20:26 +00:00
viewBinding.button.isEnabled = false
2022-04-11 05:20:24 +00:00
viewBinding.button.setText(R.string.installated)
WebMod.instance.addDownloadNum(modId)
}
2022-04-04 14:03:10 +00:00
2022-04-11 05:20:24 +00:00
})
}
}
2022-04-04 14:03:10 +00:00
}
2022-08-31 06:34:45 +00:00
// /**
// * 加载评论列表
// * @param modId String
// */
// fun loadModCommentList(modId: String) {
// viewBinding.commentLinearProgressIndicator.isVisible = true
// WebMod.instance.getCommentsList(modId, object : ApiCallBack<WebModCommentData> {
// override fun onResponse(t: WebModCommentData) {
// viewBinding.commentLinearProgressIndicator.isVisible = false
// val data = t.data
// if (data == null) {
// viewBinding.modCommentRecyclerView.isVisible = false
// } else {
// val adapter = CommentAdapter(this@WebModInfoActivity, data)
// viewBinding.discussion.text =
// String.format(getString(R.string.discussion_num), data.size)
// adapter.setItemEvent { i, itemCommentBinding, viewHolder, data ->
// itemCommentBinding.iconView.setOnClickListener {
// gotoUserPage(data.account)
// }
// }
// viewBinding.modCommentRecyclerView.isVisible = true
// viewBinding.modCommentRecyclerView.adapter = adapter
2022-04-04 14:03:10 +00:00
// }
2022-08-31 06:34:45 +00:00
// }
//
// override fun onFailure(e: Exception) {
// viewBinding.commentLinearProgressIndicator.isVisible = false
// viewBinding.modCommentRecyclerView.isVisible = false
// }
//
// })
// }
//
// fun loadDeveloperInfo(userId: String) {
// User.getSpaceInfo(userId, object : ApiCallBack<SpaceInfoData> {
// override fun onResponse(t: SpaceInfoData) {
// if (t.code == ServerConfiguration.Success_Code) {
// val icon = t.data.headIcon
// if (icon != null) {
// Glide.with(this@WebModInfoActivity)
// .load(ServerConfiguration.getRealLink(icon))
// .apply(GlobalMethod.getRequestOptions(true))
// .into(viewBinding.headIconView)
// }
// viewBinding.userNameView.text = t.data.userName
// val info = String.format(
// getString(R.string.fans_information),
// ServerConfiguration.numberToString(t.data.fans),
// ServerConfiguration.numberToString(t.data.follower),
// ServerConfiguration.numberToString(t.data.praise)
// )
// viewBinding.userInfoView.text = info
//
// viewBinding.cardView.postDelayed({
// viewBinding.cardView.isVisible = true
// viewBinding.openUserSpace.setOnClickListener {
// gotoUserPage(t.data.account)
// }
// }, 300)
// }
//// else {
//// viewBinding.cardView.isVisible = false
//// }
//
// }
//
// override fun onFailure(e: Exception) {
//// viewBinding.cardView.isVisible = false
// }
//
// })
// }
2022-04-04 14:03:10 +00:00
/**
* 打开用户主页
* @param userId String
*/
fun gotoUserPage(userId: String) {
val intent = Intent(
this@WebModInfoActivity,
UserHomePageActivity::class.java
)
intent.putExtra("userId", userId)
startActivity(
intent
)
}
2022-08-31 06:34:45 +00:00
// private fun initAction() {
// viewBinding.sendDiscussion.setOnClickListener {
// val account = AppSettings.getValue(AppSettings.Setting.Account, "")
// if (account.isBlank()) {
// showError(getString(R.string.please_login_first))
// return@setOnClickListener
// }
//
//
// CommentDialog(this).setCancelable(false)
// .setSubmitFun { button, textInputLayout, s, alertDialog ->
// button.isEnabled = false
// WebMod.instance.sendComment(
// AppSettings.getValue(AppSettings.Setting.Token, ""),
// modId,
// s,
// object : ApiCallBack<ApiResponse> {
// override fun onResponse(t: ApiResponse) {
// if (t.code == ServerConfiguration.Success_Code) {
// alertDialog.dismiss()
// loadModCommentList(modId)
// Snackbar.make(
// viewBinding.button,
// R.string.release_ok,
// Snackbar.LENGTH_SHORT
// ).show()
// } else {
// textInputLayout.error = t.message
// }
// }
//
// override fun onFailure(e: Exception) {
// textInputLayout.error = e.toString()
// }
//
// })
// }.show()
// }
//
// }
2022-04-04 14:03:10 +00:00
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityWebModInfoBinding {
2022-04-04 14:03:10 +00:00
return ActivityWebModInfoBinding.inflate(layoutInflater)
}
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
initView()
2022-08-31 06:34:45 +00:00
// initData()
// initAction()
2022-04-04 14:03:10 +00:00
}
}
}