fix OpenNoteDialog

This commit is contained in:
tibbi 2016-11-26 21:20:10 +01:00
parent 7be53dd1f3
commit 5967b74c3e
2 changed files with 20 additions and 15 deletions

View File

@ -44,14 +44,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
fun initViewPager() {
mNotes = mDb.getNotes()
mCurrentNote = mNotes[0]
var itemIndex = 0
for (i in 0..mNotes.count() - 1) {
if (mNotes[i].id == config.currentNoteId) {
mCurrentNote = mNotes[i]
itemIndex = i
break
}
}
val itemIndex = getNoteIndexWithId(config.currentNoteId)
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes)
view_pager.apply {
@ -127,11 +120,9 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun updateSelectedNote(id: Int) {
config.currentNoteId = id
for (i in 0..mNotes.count() - 1) {
view_pager.currentItem = i
mCurrentNote = mNotes[i]
}
val index = getNoteIndexWithId(id)
view_pager.currentItem = index
mCurrentNote = mNotes[index]
}
fun displayNewNoteDialog() {
@ -172,6 +163,16 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
}
}
private fun getNoteIndexWithId(id: Int): Int {
for (i in 0..mNotes.count() - 1) {
if (mNotes[i].id == id) {
mCurrentNote = mNotes[i]
return i
}
}
return 0
}
private fun shareText() {
val text = notes_view.value
if (text.isEmpty()) {

View File

@ -12,6 +12,7 @@ import kotlinx.android.synthetic.main.dialog_radio_group.view.*
class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener {
val dialog: AlertDialog?
var wasInit = false
init {
val config = Config.newInstance(activity)
@ -36,10 +37,13 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U
.create()
dialog!!.show()
wasInit = true
}
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
callback.invoke(checkedId)
dialog?.dismiss()
if (wasInit) {
callback.invoke(checkedId)
dialog?.dismiss()
}
}
}