(Probably buggy) Renaming now moves the app correctly and searched items align to bottom.

This commit is contained in:
ottoptj 2024-05-21 02:28:21 +03:00
commit 23ff67f388
4 changed files with 86 additions and 34 deletions

View file

@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import eu.ottop.yamlauncher.databinding.ActivityAppMenuBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
@ -68,17 +69,21 @@ class AppActionMenu {
editLayout.visibility = View.VISIBLE
actionMenu.visibility = View.INVISIBLE
val editText = editLayout.findViewById<EditText>(R.id.app_name_edit)
val resetButton = editLayout.findViewById<AppCompatButton>(R.id.reset)
val app = Pair(mainActivity!!, Pair(userHandle, workProfile))
searchView.visibility = View.INVISIBLE
editText.requestFocus()
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val imm =
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 100)
binding.root.addOnLayoutChangeListener {
_, _, top, _, bottom, _, oldTop, _, oldBottom ->
binding.root.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom ->
if (bottom - top > oldBottom - oldTop) {
editLayout.clearFocus()
@ -90,24 +95,51 @@ class AppActionMenu {
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val imm =
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken, 0)
sharedPreferenceManager.setAppName(activity, appInfo.packageName, workProfile, editText.text.toString())
sharedPreferenceManager.setAppName(
activity,
appInfo.packageName,
workProfile,
editText.text.toString()
)
val newPosition = activity.getInstalledApps()
.indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile }
uiScope.launch {
activity.updateItem(position,Pair(mainActivity!!, Pair(userHandle, workProfile)))
activity.updateItem(position, app)
activity.moveItem(position, newPosition)
activity.manualRefresh()
}
return@setOnEditorActionListener true
}
false
}
resetButton.setOnClickListener {
val imm =
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editLayout.windowToken, 0)
sharedPreferenceManager.resetAppName(
activity,
app.first.applicationInfo.packageName,
app.second.second
)
val newPosition = activity.getInstalledApps()
.indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile }
activity.updateItem(position, app)
activity.moveItem(position, newPosition)
activity.manualRefresh()
}
}
actionMenu.findViewById<TextView>(R.id.hide).setOnClickListener {
sharedPreferenceManager.setAppHidden(activity, appInfo.packageName, workProfile, true)
textView.visibility = View.GONE
editLayout.visibility = View.GONE
textView.visibility = View.GONE
actionMenu.visibility = View.GONE
sharedPreferenceManager.setAppHidden(activity, appInfo.packageName, workProfile, true)
}
actionMenu.findViewById<TextView>(R.id.close).setOnClickListener {

View file

@ -8,7 +8,9 @@ import android.os.Bundle
import android.os.UserHandle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
@ -72,7 +74,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
searchView = findViewById(R.id.searchView)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.scrollToPosition(0)
installedApps = getInstalledApps()
filteredApps = mutableListOf()
filteredApps.addAll(installedApps)
@ -88,7 +90,6 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
val mainActivity = launcherApps.getActivityList(appInfo.applicationInfo.packageName, userHandle).firstOrNull()
if (mainActivity != null) {
launcherApps.startMainActivity(mainActivity.componentName, userHandle, null, null)
finish()
} else {
Toast.makeText(this, "Cannot launch app", Toast.LENGTH_SHORT).show()
}
@ -182,7 +183,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
return this.replace("[^a-zA-Z0-9]".toRegex(), "")
}
private fun getInstalledApps(): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
fun getInstalledApps(): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
val allApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
val launcherApps = getSystemService(LAUNCHER_APPS_SERVICE) as LauncherApps
for (i in launcherApps.profiles.indices) {
@ -202,30 +203,39 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
override fun onStop() {
super.onStop()
job.cancel()
//finish()
}
override fun onStart() {
super.onStart()
startTask()
val imm =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(binding.root.windowToken, 0)
}
private fun startTask() {
job = CoroutineScope(Dispatchers.Default).launch {
while (true) {
val updatedApps = getInstalledApps()
val changes = detectChanges(installedApps, updatedApps)
installedApps = updatedApps
withContext(Dispatchers.Main) {
applyChanges(changes, installedApps)
}
manualRefresh()
delay(5000)
}
}
}
data class Change(val type: ChangeType, val position: Int)
fun manualRefresh() {
CoroutineScope(Dispatchers.Default).launch {
val updatedApps = getInstalledApps()
val changes = detectChanges(installedApps, updatedApps)
withContext(Dispatchers.Main) {
applyChanges(changes, installedApps)
}
installedApps = updatedApps
}
}
data class Change(val type: ChangeType, val position: Int, val newPosition: Int = 0)
enum class ChangeType {
INSERT, REMOVE, UPDATE
@ -237,6 +247,16 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
val oldSet = oldList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet()
val newSet = newList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet()
oldList.forEachIndexed { index, oldItem ->
if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) {
val newIndex = newList.indexOfFirst { it.first.applicationInfo.packageName == oldItem.first.applicationInfo.packageName && it.second.second == oldItem.second.second }
if (oldItem.first.componentName != newList[newIndex].first.componentName) {
changes.add(Change(ChangeType.UPDATE, index))
}
}
}
// Detect insertions
newList.forEachIndexed { index, newItem ->
if (!oldSet.contains(Pair(newItem.first.applicationInfo.packageName, newItem.second.second))) {
@ -252,14 +272,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
}
// Detect updates
oldList.forEachIndexed { index, oldItem ->
if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) {
val newIndex = newList.indexOfFirst { it.first.applicationInfo.packageName == oldItem.first.applicationInfo.packageName }
if (oldItem.first.componentName != newList[newIndex].first.componentName) {
changes.add(Change(ChangeType.UPDATE, index))
}
}
}
changes.addAll(removalChanges.reversed())
@ -296,4 +309,10 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
adapter.notifyItemChanged(position)
}
fun moveItem(position: Int, newPosition: Int) {
Log.d("Movestatus","MOVED")
adapter.moveApp(position, newPosition)
adapter.notifyItemMoved(position, newPosition)
}
}

View file

@ -114,12 +114,6 @@ class AppMenuAdapter(
holder.textView.text = sharedPreferenceManager.getAppName(activity, app.first.applicationInfo.packageName,app.second.second, appInfo.loadLabel(holder.itemView.context.packageManager))
holder.editView.findViewById<EditText>(R.id.app_name_edit).setText(holder.textView.text)
holder.textView.visibility = View.VISIBLE
holder.editView.findViewById<AppCompatButton>(R.id.reset).setOnClickListener {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(holder.editView.windowToken, 0)
sharedPreferenceManager.resetAppName(activity, app.first.applicationInfo.packageName, app.second.second)
activity.updateItem(position, app)
}
}
override fun getItemCount(): Int {
@ -137,4 +131,10 @@ class AppMenuAdapter(
fun updateApp(position: Int, app: Pair<LauncherActivityInfo, Pair<UserHandle, Int>>) {
apps[position] = app
}
fun moveApp(position: Int, newPosition: Int) {
val app = apps.removeAt(position)
apps.add(newPosition, app)
}
}

View file

@ -36,7 +36,8 @@
android:padding="0dp"
android:requiresFadingEdge="vertical"
android:scrollbars="none"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:stackFromEnd="true">
</androidx.recyclerview.widget.RecyclerView>