rewriting EditDialog

This commit is contained in:
tibbi 2017-11-12 16:05:03 +01:00
parent c3f1dad0aa
commit f5ddb72a6f
4 changed files with 53 additions and 33 deletions

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.applauncher.adapters package com.simplemobiletools.applauncher.adapters
import android.app.AlertDialog
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Resources import android.content.res.Resources
import android.support.v7.view.ActionMode import android.support.v7.view.ActionMode
@ -12,6 +11,7 @@ import com.bignerdranch.android.multiselector.MultiSelector
import com.bignerdranch.android.multiselector.SwappingHolder import com.bignerdranch.android.multiselector.SwappingHolder
import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.activities.SimpleActivity import com.simplemobiletools.applauncher.activities.SimpleActivity
import com.simplemobiletools.applauncher.dialogs.EditDialog
import com.simplemobiletools.applauncher.extensions.config import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
@ -20,10 +20,8 @@ import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.interfaces.MyAdapterListener import com.simplemobiletools.commons.interfaces.MyAdapterListener
import kotlinx.android.synthetic.main.app_launcher_item.view.* import kotlinx.android.synthetic.main.app_launcher_item.view.*
import kotlinx.android.synthetic.main.dialog_edit_launcher.view.*
import java.util.* import java.util.*
class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLauncher>, val listener: AppLaunchersListener?, val itemClick: (AppLauncher) -> Unit) : class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLauncher>, val listener: AppLaunchersListener?, val itemClick: (AppLauncher) -> Unit) :
@ -91,7 +89,7 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLaunc
} }
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
menu.findItem(R.id.cab_edit).isVisible = multiSelector.selectedPositions.size == 1 menu.findItem(R.id.cab_edit).isVisible = selectedPositions.size == 1
return true return true
} }
@ -117,32 +115,9 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLaunc
override fun getItemCount() = launchers.count() override fun getItemCount() = launchers.count()
private fun showEditDialog() { private fun showEditDialog() {
val selectedLauncher = launchers[multiSelector.selectedPositions[0]] EditDialog(activity, launchers[selectedPositions.first()]) {
val editView = activity.layoutInflater.inflate(R.layout.dialog_edit_launcher, null) actMode?.finish()
editView.edit_launcher_edittext.setText(selectedLauncher.name)
AlertDialog.Builder(activity).apply {
setTitle(activity.getString(R.string.rename))
setView(editView)
setPositiveButton(R.string.ok, null)
setNegativeButton(R.string.cancel, null)
create().apply {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newName = editView.edit_launcher_edittext.text.toString().trim()
if (!newName.isEmpty()) {
if (activity.dbHelper.updateLauncherName(selectedLauncher.id, newName) > 0) {
listener?.refreshLaunchers() listener?.refreshLaunchers()
dismiss()
} else {
activity.toast(R.string.unknown_error_occurred)
}
} else {
activity.toast(R.string.enter_launcher_name)
}
}
}
} }
} }

View File

@ -0,0 +1,44 @@
package com.simplemobiletools.applauncher.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import android.view.WindowManager
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import kotlinx.android.synthetic.main.dialog_edit_launcher.view.*
class EditDialog(val activity: Activity, val appLauncher: AppLauncher, val callback: () -> Unit) {
var dialog: AlertDialog
var view = (activity.layoutInflater.inflate(R.layout.dialog_edit_launcher, null) as ViewGroup)
init {
view.edit_launcher_edittext.setText(appLauncher.name)
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.rename)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
show()
getButton(android.app.AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newName = view.edit_launcher_edittext.value
if (!newName.isEmpty()) {
if (activity.dbHelper.updateLauncherName(appLauncher.id, newName)) {
callback()
dismiss()
} else {
activity.toast(R.string.unknown_error_occurred)
}
} else {
activity.toast(R.string.enter_launcher_name)
}
}
}
}
}

View File

@ -86,12 +86,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mDb.delete(MAIN_TABLE_NAME, selection, null) mDb.delete(MAIN_TABLE_NAME, selection, null)
} }
fun updateLauncherName(id: Int, newName: String): Int { fun updateLauncherName(id: Int, newName: String): Boolean {
val values = ContentValues() val values = ContentValues()
values.put(COL_NAME, newName) values.put(COL_NAME, newName)
val selection = "$COL_ID = ?" val selection = "$COL_ID = ?"
val selectionArgs = Array(1) { id.toString() } val selectionArgs = Array(1) { id.toString() }
return mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) return mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) == 1
} }
fun getLaunchers(): ArrayList<AppLauncher> { fun getLaunchers(): ArrayList<AppLauncher> {

View File

@ -11,6 +11,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:inputType="textCapWords" android:inputType="textCapWords"
android:textCursorDrawable="@null"/> android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size"/>
</FrameLayout> </FrameLayout>