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

196 lines
7.9 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import android.annotation.SuppressLint
import com.coldmint.rust.pro.base.BaseActivity
import org.json.JSONObject
import android.os.Bundle
import android.content.Intent
2022-08-17 01:25:20 +00:00
import android.text.Editable
import android.text.TextWatcher
2022-08-14 02:43:53 +00:00
import android.util.Log
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.tool.AppSettings
import org.json.JSONException
import org.json.JSONArray
import android.view.inputmethod.EditorInfo
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-04-04 14:03:10 +00:00
import android.view.Menu
import android.view.MenuItem
import com.google.android.material.snackbar.Snackbar
import com.coldmint.rust.core.SourceFile
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.*
import androidx.core.view.isVisible
2022-08-14 02:43:53 +00:00
import androidx.lifecycle.viewModelScope
2022-04-04 14:03:10 +00:00
import com.afollestad.materialdialogs.MaterialDialog
2022-08-14 02:43:53 +00:00
import com.coldmint.dialog.CoreDialog
import com.coldmint.rust.core.LocalTemplatePackage
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.dataBean.InputParserDataBean
import com.coldmint.rust.core.dataBean.IntroducingDataBean
import com.coldmint.rust.core.dataBean.ListParserDataBean
2022-08-14 02:43:53 +00:00
import com.coldmint.rust.core.dataBean.template.LocalTemplateFile
import com.coldmint.rust.core.dataBean.template.TemplatePackage
2022-08-17 01:25:20 +00:00
import com.coldmint.rust.core.dataBean.template.WebTemplateData
import com.coldmint.rust.core.interfaces.ApiCallBack
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.interfaces.TemplateParser
import com.coldmint.rust.core.templateParser.InputParser
import com.coldmint.rust.core.templateParser.IntroducingParser
import com.coldmint.rust.core.templateParser.ListParser
import com.coldmint.rust.core.tool.FileOperator
2022-08-17 01:25:20 +00:00
import com.coldmint.rust.core.web.ServerConfiguration
import com.coldmint.rust.core.web.TemplatePhp
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.databinding.ActivityTemplateParserBinding
2022-07-06 06:04:54 +00:00
import com.coldmint.rust.pro.tool.UnitAutoCompleteHelper
2022-08-14 02:43:53 +00:00
import com.coldmint.rust.pro.viewmodel.TemplateParserViewModel
2022-08-17 01:25:20 +00:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.dialog.MaterialDialogs
2022-04-04 14:03:10 +00:00
import com.google.android.material.textfield.TextInputLayout
import com.google.gson.Gson
import java.io.File
import java.util.*
import java.util.concurrent.Executors
import kotlin.collections.ArrayList
import kotlin.concurrent.thread
2022-08-14 02:43:53 +00:00
class TemplateParserActivity : BaseActivity<ActivityTemplateParserBinding>() {
2022-04-04 14:03:10 +00:00
2022-08-14 02:43:53 +00:00
val viewModel: TemplateParserViewModel by lazy {
TemplateParserViewModel()
}
private val language by lazy {
AppSettings.getValue(AppSettings.Setting.AppLanguage, Locale.getDefault().language)
2022-04-04 14:03:10 +00:00
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu_template, menu)
return true
}
@SuppressLint("CheckResult")
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.preview_static_code -> {
2022-08-14 02:43:53 +00:00
var code = viewModel.getCode()
if (code.isBlank()) {
code = getString(R.string.not_found_data2)
2022-04-04 14:03:10 +00:00
}
2022-08-17 01:25:20 +00:00
MaterialAlertDialogBuilder(this).setTitle(viewModel.getTemplateName(language))
.setMessage(code).setPositiveButton(R.string.dialog_ok) { i, i2 ->
2022-08-14 02:43:53 +00:00
}.show()
2022-04-04 14:03:10 +00:00
}
}
2022-08-14 02:43:53 +00:00
return super.onOptionsItemSelected(item)
2022-04-04 14:03:10 +00:00
}
2022-08-14 02:43:53 +00:00
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
setReturnButton()
2022-08-17 01:25:20 +00:00
val unitAutoCompleteHelper = UnitAutoCompleteHelper(this)
unitAutoCompleteHelper.onBindAutoCompleteTextView(viewBinding.fileNameInputView)
viewBinding.fileNameInputView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(p0: Editable?) {
val text = p0.toString()
if (text.isBlank()) {
viewBinding.fileNameInputLayout.error = getString(R.string.file_name_error)
} else {
viewBinding.fileNameInputLayout.isErrorEnabled = false
}
}
})
viewBinding.fab.setOnClickListener {
val name = viewBinding.fileNameInputView.text.toString()
if (name.isNotBlank()) {
val build = viewModel.buildFile(this, name)
if (build) {
setResult(RESULT_OK)
finish()
} else {
Snackbar.make(
viewBinding.fab,
R.string.create_unit_failed,
Snackbar.LENGTH_SHORT
).show()
}
}
}
val createDirectory = intent.getStringExtra("createDirectory")
if (createDirectory == null) {
Toast.makeText(this, "没有设置创建目录", Toast.LENGTH_SHORT).show()
finish()
return
}
viewModel.setCreateDirectory(createDirectory)
2022-08-14 02:43:53 +00:00
val link = intent.getStringExtra("link")
if (link == null) {
Toast.makeText(this, "请设置链接", Toast.LENGTH_SHORT).show()
finish()
return
2022-04-04 14:03:10 +00:00
}
2022-08-14 02:43:53 +00:00
val isLocal = intent.getBooleanExtra("isLocal", false)
if (isLocal) {
Log.d("模板解析器", "读取本地模板$link")
viewModel.setTemplate(LocalTemplateFile(File(link)))
val templateParserList = viewModel.getTemplateParserList(this)
templateParserList.forEach {
viewBinding.base.addView(it.contextView)
2022-04-04 14:03:10 +00:00
}
2022-08-14 02:43:53 +00:00
viewBinding.linearLayout.isVisible = false
viewBinding.nestedScrollView.isVisible = true
title = viewModel.getTemplateName(language)
} else {
2022-08-17 01:25:20 +00:00
Log.d("模板解析器", "加载网络模板$link")
TemplatePhp.instance.getTemplate(link, object : ApiCallBack<WebTemplateData> {
override fun onResponse(t: WebTemplateData) {
if (t.code == ServerConfiguration.Success_Code) {
viewModel.setTemplate(t.data)
val templateParserList =
viewModel.getTemplateParserList(this@TemplateParserActivity)
templateParserList.forEach {
viewBinding.base.addView(it.contextView)
}
viewBinding.linearLayout.isVisible = false
viewBinding.nestedScrollView.isVisible = true
title = viewModel.getTemplateName(language)
} else {
Toast.makeText(
this@TemplateParserActivity,
t.message,
Toast.LENGTH_SHORT
).show()
finish()
}
}
2022-04-04 14:03:10 +00:00
2022-08-17 01:25:20 +00:00
override fun onFailure(e: Exception) {
e.printStackTrace()
Toast.makeText(
this@TemplateParserActivity,
R.string.network_error,
Toast.LENGTH_SHORT
).show()
finish()
}
})
2022-04-04 14:03:10 +00:00
}
2022-08-17 01:25:20 +00:00
2022-04-04 14:03:10 +00:00
}
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityTemplateParserBinding {
return ActivityTemplateParserBinding.inflate(layoutInflater)
2022-04-04 14:03:10 +00:00
}
}