return the selected launchers to the activity

This commit is contained in:
tibbi 2016-08-14 18:27:16 +02:00
parent 8c6062180f
commit dbd27b7baf
2 changed files with 21 additions and 16 deletions

View File

@ -5,7 +5,6 @@ import android.content.pm.PackageManager
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.google.gson.Gson
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerAdapter
import com.simplemobiletools.applauncher.databases.DbHelper
@ -18,7 +17,7 @@ import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.comparisons.compareBy
class MainActivity : SimpleActivity() {
class MainActivity : SimpleActivity(), AddAppDialog.AddLaunchersInterface {
lateinit var dbHelper: DbHelper
lateinit var launchers: ArrayList<AppLauncher>
lateinit var remainingLaunchers: ArrayList<AppLauncher>
@ -40,11 +39,7 @@ class MainActivity : SimpleActivity() {
remainingLaunchers = getNotDisplayedLaunchers()
fab.setOnClickListener {
val dialog = AddAppDialog()
val args = Bundle()
args.putString(dialog.LAUNCHERS, Gson().toJson(remainingLaunchers))
dialog.arguments = args
dialog.show(fragmentManager, "")
AddAppDialog.newInstance(this, remainingLaunchers).show(fragmentManager, "")
}
}
@ -85,6 +80,10 @@ class MainActivity : SimpleActivity() {
return filtered as ArrayList<AppLauncher>
}
override fun selectedLaunchers(launchers: ArrayList<AppLauncher>) {
}
override fun onDestroy() {
super.onDestroy()
preferences.isFirstRun = false

View File

@ -5,8 +5,6 @@ import android.app.DialogFragment
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerAdapter
import com.simplemobiletools.applauncher.models.AppLauncher
@ -14,17 +12,20 @@ import kotlinx.android.synthetic.main.launcher_picker.view.*
import java.util.*
class AddAppDialog() : DialogFragment() {
val LAUNCHERS = "launchers"
lateinit var launchers: ArrayList<AppLauncher>
companion object {
lateinit var launchers: ArrayList<AppLauncher>
var callback: AddLaunchersInterface? = null
fun newInstance(cb: AddLaunchersInterface, appLaunchers: ArrayList<AppLauncher>): AddAppDialog {
callback = cb
launchers = appLaunchers
return AddAppDialog()
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
builder.setTitle(R.string.add_apps)
val json = arguments.getString(LAUNCHERS)
val listType = object : TypeToken<ArrayList<AppLauncher>>() {}.type
launchers = Gson().fromJson(json, listType)
val recyclerView = View.inflate(activity, R.layout.launcher_picker, null)
recyclerView.launchers_holder.adapter = RecyclerAdapter(activity, true, launchers) {
@ -33,10 +34,15 @@ class AddAppDialog() : DialogFragment() {
builder.setView(recyclerView)
builder.setPositiveButton(android.R.string.ok, { dialogInterface, i ->
val selectedApps = launchers.filter { it.isChecked } as ArrayList<AppLauncher>
callback?.selectedLaunchers(selectedApps)
})
builder.setNegativeButton(android.R.string.cancel, null)
return builder.create()
}
interface AddLaunchersInterface {
fun selectedLaunchers(launchers: ArrayList<AppLauncher>)
}
}