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

256 lines
9.5 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.tool.BookmarkManager
import android.widget.EditText
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.content.Intent
import android.view.KeyEvent
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-04-04 14:03:10 +00:00
import android.view.MenuItem
import android.view.View
2022-07-06 15:12:56 +00:00
import androidx.appcompat.app.AlertDialog
2022-04-04 14:03:10 +00:00
import androidx.core.view.isVisible
2022-08-10 09:27:38 +00:00
import com.coldmint.dialog.CoreDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.adapters.BookmarkAdapter
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databean.Bookmark
import com.coldmint.rust.pro.databinding.ActivityBookmarkManagerBinding
import com.coldmint.rust.pro.databinding.EditBookmarkBinding
2022-08-10 09:27:38 +00:00
import com.coldmint.rust.pro.dialog.BookmarkDialog
2022-07-06 15:12:56 +00:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2022-04-04 14:03:10 +00:00
import java.io.File
import java.util.ArrayList
class BookmarkManagerActivity : BaseActivity<ActivityBookmarkManagerBinding>() {
private lateinit var bookmarkManager: BookmarkManager
lateinit var bookmarkAdapter: BookmarkAdapter
2022-08-10 09:27:38 +00:00
lateinit var bookmarkDialog :BookmarkDialog
/**
* 重新构建对话框实例化一个对话框并使成员变量指向他
*/
fun recreateBookmarkDialog(){
bookmarkDialog = BookmarkDialog(this)
bookmarkDialog.setButtonAction {
val bundle = Bundle()
val intent =
Intent(this, FileManagerActivity::class.java)
bundle.putString("type", "selectDirectents")
val thisPath = bookmarkDialog.getPath()
if (thisPath.isNotEmpty()) {
bundle.putString("path", thisPath)
}
intent.putExtra("data", bundle)
startActivityForResult(intent, 1)
}
}
2022-04-04 14:03:10 +00:00
/**
* 升级视图
*/
@SuppressLint("NotifyDataSetChanged")
private fun updateView(bookmarks: ArrayList<Bookmark>?) {
if (bookmarks == null || bookmarks.size <= 0) {
viewBinding.progressBar.isVisible = false
viewBinding.fileError.setText(R.string.no_bookmark)
} else {
val list = bookmarkManager.list()
if (list != null && list.isNotEmpty()) {
bookmarkAdapter =
BookmarkAdapter(this@BookmarkManagerActivity, list)
bookmarkAdapter.setItemEvent { i, attachFileItemBinding, viewHolder, bookmark ->
attachFileItemBinding.root.setOnLongClickListener {
bookmarkAdapter.showDeleteItemDialog(
bookmark.name,
2022-08-10 09:27:38 +00:00
viewHolder.absoluteAdapterPosition,
onClickPositiveButton = { i: Int, b ->
2022-04-04 14:03:10 +00:00
bookmarkManager.removeBookmark(bookmark)
if (list.isEmpty()) {
showNoBookmarkToView()
}
false
}
)
return@setOnLongClickListener false
}
attachFileItemBinding.root.setOnClickListener {
showEditView(getString(R.string.edit), bookmark.name, bookmark.path)
}
}
viewBinding.bookmarkList.adapter = bookmarkAdapter
viewBinding.progressBar.isVisible = false
viewBinding.fileError.isVisible = false
viewBinding.bookmarkList.isVisible = true
} else {
showNoBookmarkToView()
}
}
}
/**
* 显示没有书签到视图上
*/
fun showNoBookmarkToView() {
viewBinding.bookmarkList.isVisible = false
viewBinding.fileError.isVisible = true
viewBinding.progressBar.isVisible = false
viewBinding.fileError.setText(R.string.no_bookmark)
}
/**
* 展示编辑视图
*
* @param title 标题
* @param name 名称
* @param path 路径
*/
fun showEditView(title: String, name: String?, path: String?) {
2022-08-10 09:27:38 +00:00
recreateBookmarkDialog()
2022-04-04 14:03:10 +00:00
if (path != null) {
2022-08-10 09:27:38 +00:00
bookmarkDialog.setPathViewText(path)
2022-04-04 14:03:10 +00:00
}
if (name != null) {
2022-08-10 09:27:38 +00:00
bookmarkDialog.setNameViewText(name)
2022-04-04 14:03:10 +00:00
}
2022-08-10 09:27:38 +00:00
bookmarkDialog.setTitle(title).setAutoDismiss(false).setCancelable(false)
bookmarkDialog.setPositiveButton(R.string.dialog_ok) { editBookmarkBinding ->
val newPath = editBookmarkBinding.pathEdit.text.toString()
val newName = editBookmarkBinding.nameView.text.toString()
if (newName.isEmpty()) {
setErrorAndInput(
editBookmarkBinding.nameView,
getString(R.string.enter_bookmark_name), editBookmarkBinding.nameInputLayout
)
return@setPositiveButton
2022-04-04 14:03:10 +00:00
}
2022-08-10 09:27:38 +00:00
if (newPath.isEmpty()) {
setErrorAndInput(
editBookmarkBinding.pathEdit,
getString(R.string.enter_file_path),
editBookmarkBinding.pathInputLayout
)
return@setPositiveButton
}
val file = File(newPath)
if (!file.exists()) {
setErrorAndInput(
editBookmarkBinding.pathEdit,
getString(R.string.bookmark_jump_failed),
editBookmarkBinding.pathInputLayout
)
return@setPositiveButton
}
if (name == null) {
val addBookmark = bookmarkManager.addBookmark(newPath, newName)
if (addBookmark) {
updateView(bookmarkManager.list())
2022-04-04 14:03:10 +00:00
2022-08-10 09:27:38 +00:00
} else {
setErrorAndInput(
editBookmarkBinding.pathEdit,
getString(R.string.bookmark_already_exists),
editBookmarkBinding.pathInputLayout
)
2022-07-06 15:12:56 +00:00
}
2022-08-10 09:27:38 +00:00
} else {
if (name == newName && path == newPath) {
bookmarkDialog.dismiss()
2022-04-04 14:03:10 +00:00
} else {
2022-08-10 09:27:38 +00:00
if (path != null) {
val oldBookmark = Bookmark(path, name)
val newBookmark = Bookmark(newPath, newName)
val addBookmark =
bookmarkManager.replaceBookmark(oldBookmark, newBookmark)
if (addBookmark) {
updateView(bookmarkManager.list())
bookmarkDialog.dismiss()
} else {
setErrorAndInput(
editBookmarkBinding.pathEdit,
getString(R.string.bookmark_already_exists),
editBookmarkBinding.pathInputLayout
)
2022-04-04 14:03:10 +00:00
}
}
}
}
2022-08-10 09:27:38 +00:00
}
bookmarkDialog.setNegativeButton(R.string.dialog_close) {
bookmarkDialog.dismiss()
}
bookmarkDialog.show()
2022-04-04 14:03:10 +00:00
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
1 -> if (resultCode == RESULT_OK) {
val path = data!!.getStringExtra("Directents")
2022-08-10 09:27:38 +00:00
bookmarkDialog.setPathViewText(path)
val oldName = bookmarkDialog.getName()
2022-04-04 14:03:10 +00:00
if (oldName.isEmpty()) {
val endIndex = path!!.lastIndexOf("/")
if (endIndex > -1) {
val name = path.substring(endIndex + 1, path.length)
2022-08-10 09:27:38 +00:00
bookmarkDialog.setNameViewText(name)
2022-04-04 14:03:10 +00:00
} else {
2022-08-10 09:27:38 +00:00
bookmarkDialog.setNameViewText(path)
2022-04-04 14:03:10 +00:00
}
}
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
bookmarkManager.save()
finish()
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
bookmarkManager.save()
finish()
return true
}
return super.onKeyDown(keyCode, event)
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityBookmarkManagerBinding {
2022-04-04 14:03:10 +00:00
return ActivityBookmarkManagerBinding.inflate(
layoutInflater
)
}
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
setReturnButton()
setTitle(R.string.bookmark_manager)
bookmarkManager = BookmarkManager(this@BookmarkManagerActivity)
bookmarkManager.load()
viewBinding.bookmarkList.layoutManager =
LinearLayoutManager(this@BookmarkManagerActivity)
val bookmarks = bookmarkManager.list()
updateView(bookmarks)
viewBinding.fab.setOnClickListener {
showEditView(
getString(R.string.create_bookmark),
null,
null
)
}
2022-08-10 09:27:38 +00:00
2022-04-04 14:03:10 +00:00
}
}
}