rewrite the Open Note dialog to a custom linearlayout

This commit is contained in:
tibbi 2017-03-09 19:49:30 +01:00
parent 4f5b8f039f
commit 0f67840897
2 changed files with 34 additions and 23 deletions

View File

@ -3,46 +3,43 @@ package com.simplemobiletools.notes.dialogs
import android.app.Activity import android.app.Activity
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.RadioButton import android.widget.LinearLayout
import android.widget.RadioGroup import android.widget.RadioGroup
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.config import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.helpers.DBHelper
import kotlinx.android.synthetic.main.dialog_radio_group.view.* import kotlinx.android.synthetic.main.open_note_item.view.*
class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener { class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) {
val dialog: AlertDialog? lateinit var dialog: AlertDialog
var wasInit = false
init { init {
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) val view = LinearLayout(activity).apply {
val radioGroup = view.dialog_radio_group orientation = LinearLayout.VERTICAL
radioGroup.setOnCheckedChangeListener(this) layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
val notes = DBHelper.newInstance(activity).getNotes() val notes = DBHelper.newInstance(activity).getNotes()
notes.forEach { notes.forEach {
val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton activity.layoutInflater.inflate(R.layout.open_note_item, null).apply {
radioButton.apply { open_note_item_radio_button.apply {
text = it.title text = it.title
isChecked = it.id == activity.config.currentNoteId isChecked = it.id == activity.config.currentNoteId
id = it.id id = it.id
setOnClickListener {
callback.invoke(id)
dialog.dismiss()
}
}
view.addView(this, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
} }
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
} }
dialog = AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.create().apply { .create().apply {
activity.setupDialogStuff(view, this, R.string.pick_a_note) activity.setupDialogStuff(view, this, R.string.pick_a_note)
} }
wasInit = true
}
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
if (wasInit) {
callback.invoke(checkedId)
dialog?.dismiss()
}
} }
} }

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/open_note_item_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/normal_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/open_note_item_radio_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>