From 0f2315f39d48c406d6871a854ca9bbe89f6e147c Mon Sep 17 00:00:00 2001
From: tibbi <tibor@kaputa.sk>
Date: Thu, 18 Aug 2016 20:57:12 +0200
Subject: [PATCH] implement launcher renaming

---
 .../applauncher/activities/MainActivity.kt           |  4 ++++
 .../applauncher/adapters/RecyclerAdapter.kt          | 12 ++++++++++--
 .../applauncher/databases/DbHelper.kt                |  8 ++++++++
 app/src/main/res/values/strings.xml                  |  1 +
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt
index 068defa..3b4aef1 100644
--- a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt
@@ -115,6 +115,10 @@ class MainActivity : SimpleActivity(), AddAppDialog.AddLaunchersInterface, Recyc
         remainingLaunchers.sortBy { it.name }
     }
 
+    override fun launcherRenamed() {
+        setupLaunchers()
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         preferences.isFirstRun = false
diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt
index d0c8abd..15a715f 100644
--- a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt
@@ -85,9 +85,15 @@ class RecyclerAdapter(val act: Activity, val launchers: List<AppLauncher>, val i
         val alertDialog = builder.create()
         alertDialog.show()
         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
-            val newName = editView.edit_launcher_edittext.text.toString()
+            val newName = editView.edit_launcher_edittext.text.toString().trim()
             if (isValidName(newName)) {
-                alertDialog.dismiss()
+                if (DbHelper(act).updateLauncherName(selectedLauncher.id, newName) > 0) {
+                    (act as EditLaunchersInterface).launcherRenamed()
+                    actMode?.finish()
+                    alertDialog.dismiss()
+                } else {
+                    Toast.makeText(act, act.resources.getString(R.string.unknown_error), Toast.LENGTH_SHORT).show()
+                }
             } else {
                 Toast.makeText(act, act.resources.getString(R.string.invalid_characters), Toast.LENGTH_SHORT).show()
             }
@@ -167,5 +173,7 @@ class RecyclerAdapter(val act: Activity, val launchers: List<AppLauncher>, val i
 
     interface EditLaunchersInterface {
         fun launchersDeleted(indexes: List<Int>, deletedLaunchers: List<AppLauncher>)
+
+        fun launcherRenamed()
     }
 }
diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt
index 15b9e0e..d0bcdda 100644
--- a/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt
@@ -52,6 +52,14 @@ class DbHelper(context: Context) : SQLiteOpenHelper(context, "launchers.db", nul
         writableDatabase.delete(TABLE, "$ID IN ($args)", null)
     }
 
+    fun updateLauncherName(id: Int, newName: String): Int {
+        val values = ContentValues()
+        values.put(NAME, newName)
+        val selection = ID + " = ?"
+        val selectionArgs = Array(1) { id.toString() }
+        return writableDatabase.update(TABLE, values, selection, selectionArgs)
+    }
+
     fun getLaunchers(): ArrayList<AppLauncher> {
         val launchers = ArrayList<AppLauncher>()
         val cursor = readableDatabase.query(TABLE, arrayOf(ID, NAME, PKG_NAME, ICON_ID), null, null, null, null, NAME)
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index c985589..e57ce1b 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -6,6 +6,7 @@
     <string name="ok">OK</string>
     <string name="cancel">Cancel</string>
     <string name="invalid_characters">New launcher name contains invalid characters</string>
+    <string name="unknown_error">An unknown error occurred</string>
 
     <!-- About -->
     <string name="about">About</string>