MISC: enhance code step 3

This commit is contained in:
imkiva 2021-05-08 17:08:00 +08:00 committed by Kiva Oyama
parent dc694aad9c
commit 6780c29f94
6 changed files with 40 additions and 65 deletions

View File

@ -13,48 +13,34 @@ import java.io.File
* @author kiva
*/
class UserScriptComponent : NeoComponent {
lateinit var userScripts: MutableList<UserScript>
private var userScripts = listOf<UserScript>()
private val scriptDir = File(NeoTermPath.USER_SCRIPT_PATH)
override fun onServiceInit() {
checkForFiles()
}
override fun onServiceInit() = checkForFiles()
override fun onServiceDestroy() {
}
override fun onServiceObtained() {
checkForFiles()
}
override fun onServiceObtained() = checkForFiles()
private fun extractDefaultScript(context: Context): Boolean {
try {
private fun extractDefaultScript(context: Context) = kotlin.runCatching {
context.extractAssetsDir("scripts", NeoTermPath.USER_SCRIPT_PATH)
File(NeoTermPath.USER_SCRIPT_PATH)
.listFiles().forEach {
scriptDir.listFiles().forEach {
Os.chmod(it.absolutePath, 448 /*Dec of 0700*/)
}
return true
} catch (e: Exception) {
NLog.e("UserScript", "Failed to extract default user scripts: ${e.localizedMessage}")
return false
}
}.onFailure {
NLog.e("UserScript", "Failed to extract default user scripts: ${it.localizedMessage}")
}
private fun checkForFiles() {
File(NeoTermPath.USER_SCRIPT_PATH).mkdirs()
userScripts = mutableListOf()
extractDefaultScript(App.get())
reloadScripts()
}
fun reloadScripts() {
val userScriptDir = File(NeoTermPath.USER_SCRIPT_PATH)
userScriptDir.mkdirs()
userScripts.clear()
userScriptDir.listFiles()
private fun reloadScripts() {
userScripts = scriptDir.listFiles()
.takeWhile { it.canExecute() }
.mapTo(userScripts, { UserScript(it) })
.map { UserScript(it) }
.toList()
}
}

View File

@ -30,7 +30,7 @@ import java.util.*
*/
class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListener, SortedListAdapter.Callback {
private val COMPARATOR = SortedListAdapter.ComparatorBuilder<PackageModel>()
private val comparator = SortedListAdapter.ComparatorBuilder<PackageModel>()
.setOrderForModel<PackageModel>(PackageModel::class.java) { a, b ->
a.packageInfo.packageName!!.compareTo(b.packageInfo.packageName!!)
}
@ -38,7 +38,7 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
lateinit var recyclerView: androidx.recyclerview.widget.RecyclerView
lateinit var adapter: PackageAdapter
lateinit var models: ArrayList<PackageModel>
var models = listOf<PackageModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -49,7 +49,7 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
recyclerView = findViewById(R.id.pm_package_list)
recyclerView.setHasFixedSize(true)
adapter = PackageAdapter(this, COMPARATOR, object : PackageAdapter.Listener {
adapter = PackageAdapter(this, comparator, object : PackageAdapter.Listener {
override fun onModelClicked(model: PackageModel) {
AlertDialog.Builder(this@PackageManagerActivity)
.setTitle(model.packageInfo.packageName)
@ -60,14 +60,11 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
.setNegativeButton(android.R.string.no, null)
.show()
}
})
adapter.addCallback(this)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
models = ArrayList()
refreshPackageList()
}
@ -174,21 +171,17 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
}
private fun refreshPackageList() = Thread {
models.clear()
val pm = ComponentManager.getComponent<PackageComponent>()
val sourceFiles = SourceHelper.detectSourceFiles()
pm.clearPackages()
sourceFiles.forEach { pm.reloadPackages(it, false) }
pm.packages.values.mapTo(models, { PackageModel(it) })
models = pm.packages.values.map { PackageModel(it) }.toList()
this@PackageManagerActivity.runOnUiThread {
adapter.edit()
.replaceAll(models)
.commit()
adapter.edit().replaceAll(models).commit()
if (models.isEmpty()) {
Toast.makeText(this@PackageManagerActivity, R.string.package_list_empty, Toast.LENGTH_SHORT).show()
changeSource()
}
}
}.start()
@ -199,25 +192,22 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
): List<Pair<PackageModel, Int>> {
return models
.map {
Pair(
it,
StringDistance.distance(mapper(it.packageInfo).toLowerCase(Locale.ROOT), query.toLowerCase(Locale.ROOT))
)
it to StringDistance.distance(mapper(it.packageInfo).toLowerCase(Locale.ROOT), query.toLowerCase(Locale.ROOT))
}
.sortedWith(Comparator { l, r -> r.second.compareTo(l.second) })
.sortedWith { l, r -> r.second.compareTo(l.second) }
.toList()
}
private fun filter(models: List<PackageModel>, query: String): List<PackageModel> {
val filteredModelList = mutableListOf<PackageModel>()
val prepared = models.filter {
it.packageInfo.packageName!!.contains(query, true)
|| it.packageInfo.description!!.contains(query, true)
}
sortDistance(prepared, query, { it.packageName!! }).mapTo(filteredModelList, { it.first })
sortDistance(prepared, query, { it.description!! }).mapTo(filteredModelList, { it.first })
return filteredModelList
return sortDistance(prepared, query) { it.packageName!! }
.plus(sortDistance(prepared, query) { it.description!! })
.map { it.first }
.toList()
}
override fun onQueryTextSubmit(text: String?) = false

View File

@ -31,9 +31,7 @@ class GeneralSettingsActivity : BasePreferenceActivity() {
}
}
private fun postChangeShell(shellName: String) {
NeoPreference.setLoginShellName(shellName)
}
private fun postChangeShell(shellName: String) = NeoPreference.setLoginShellName(shellName)
private fun requestInstallShell(shellName: String, currentShell: String) {
AlertDialog.Builder(this)

View File

@ -98,8 +98,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
when (id) {
R.id.setup_method_backup,
R.id.setup_method_local -> {
SetupHelper.makeErrorDialog(this, R.string.setup_error_parameter_null)
.show()
SetupHelper.makeErrorDialog(this, R.string.setup_error_parameter_null).show()
return
}
}

View File

@ -9,8 +9,8 @@ import android.provider.MediaStore
import io.neoterm.R
import io.neoterm.frontend.config.NeoTermPath
import io.neoterm.frontend.floating.TerminalDialog
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.text.DecimalFormat
class RangedInt(private val number: Int, private val range: IntRange) {
@ -36,14 +36,16 @@ fun Long.formatSizeInKB(): String {
}
}
fun Context.extractAssetsDir(dirName: String, extractDir: String) {
fun Context.extractAssetsDir(assetDir: String, extractDir: String) = kotlin.runCatching {
val targetDir = Paths.get(extractDir)
Files.createDirectories(targetDir)
val assets = this.assets
assets.list(dirName)?.let {
it.map { File(extractDir, it) }
.takeWhile { !it.exists() }
.forEach { file ->
assets.open("$dirName/${file.name}").use {
kotlin.runCatching { Files.copy(it, file.toPath()) }
assets.list(assetDir)?.let {
it.map { targetDir.resolve(it) }
.takeWhile { !Files.exists(it) }
.forEach { targetPath ->
assets.open("$assetDir/${targetPath.fileName}").use {
Files.copy(it, targetPath)
}
}
}

View File

@ -63,7 +63,7 @@
<string name="package_details">Package: %s\nVersion: %s\nDepends: %s\nInstalled Size: %s\nDescription: %s\nHome
Page: %s
</string>
<string name="package_list_empty">Package list is empty, please check out your source.</string>
<string name="package_list_empty">Package list is empty, please check your source.</string>
<string name="menu_refresh_list">Refresh</string>
<string name="menu_update">Update and Refresh</string>
<string name="install_font">Install Font</string>