Skip to content

Commit 8671422

Browse files
author
Pavol Franek
committed
- wip: app crash when gson can not deserialize note
1 parent 51f1dc6 commit 8671422

File tree

13 files changed

+65
-41
lines changed

13 files changed

+65
-41
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ class MainActivity : SimpleActivity() {
9999
override fun onCreateOptionsMenu(menu: Menu): Boolean {
100100
menuInflater.inflate(R.menu.menu, menu)
101101
menu.apply {
102-
findItem(R.id.undo).isVisible = showUndoButton && mCurrentNote.type == TYPE_TEXT
103-
findItem(R.id.redo).isVisible = showRedoButton && mCurrentNote.type == TYPE_TEXT
102+
findItem(R.id.undo).isVisible = showUndoButton && mCurrentNote.type == NoteType.TYPE_TEXT.value
103+
findItem(R.id.redo).isVisible = showRedoButton && mCurrentNote.type == NoteType.TYPE_TEXT.value
104104
}
105105

106106
updateMenuItemColors(menu)
@@ -116,7 +116,7 @@ class MainActivity : SimpleActivity() {
116116
findItem(R.id.export_all_notes).isVisible = shouldBeVisible
117117

118118
saveNoteButton = findItem(R.id.save_note)
119-
saveNoteButton!!.isVisible = !config.autosaveNotes && showSaveButton && mCurrentNote.type == TYPE_TEXT
119+
saveNoteButton!!.isVisible = !config.autosaveNotes && showSaveButton && mCurrentNote.type == NoteType.TYPE_TEXT.value
120120
}
121121

122122
pager_title_strip.beVisibleIf(shouldBeVisible)
@@ -274,7 +274,7 @@ class MainActivity : SimpleActivity() {
274274
}
275275
}
276276

277-
if (!config.showKeyboard || mCurrentNote.type == TYPE_CHECKLIST) {
277+
if (!config.showKeyboard || mCurrentNote.type == NoteType.TYPE_CHECKLIST.value) {
278278
hideKeyboard()
279279
}
280280
}
@@ -361,7 +361,7 @@ class MainActivity : SimpleActivity() {
361361
val fileText = it.readText().trim()
362362
val checklistItems = fileText.parseChecklistItems()
363363
if (checklistItems != null) {
364-
val note = Note(null, it.absolutePath.getFilenameFromPath().substringBeforeLast('.'), fileText, TYPE_CHECKLIST)
364+
val note = Note(null, it.absolutePath.getFilenameFromPath().substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST.value)
365365
addNewNote(note)
366366
} else {
367367
runOnUiThread {
@@ -416,9 +416,9 @@ class MainActivity : SimpleActivity() {
416416
val fileText = it.readText().trim()
417417
val checklistItems = fileText.parseChecklistItems()
418418
val note = if (checklistItems != null) {
419-
Note(null, title.substringBeforeLast('.'), fileText, TYPE_CHECKLIST)
419+
Note(null, title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST.value)
420420
} else {
421-
Note(null, title, "", TYPE_TEXT, path)
421+
Note(null, title, "", NoteType.TYPE_TEXT.value, path)
422422
}
423423

424424
if (mNotes.any { it.title.equals(note.title, true) }) {
@@ -464,10 +464,10 @@ class MainActivity : SimpleActivity() {
464464

465465
private fun exportAsFile() {
466466
ExportFileDialog(this, mCurrentNote) {
467-
val textToExport = if (mCurrentNote.type == TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
467+
val textToExport = if (mCurrentNote.type == NoteType.TYPE_TEXT.value) getCurrentNoteText() else mCurrentNote.value
468468
if (textToExport == null || textToExport.isEmpty()) {
469469
toast(R.string.unknown_error_occurred)
470-
} else if (mCurrentNote.type == TYPE_TEXT) {
470+
} else if (mCurrentNote.type == NoteType.TYPE_TEXT.value) {
471471
showExportFilePickUpdateDialog(it, textToExport)
472472
} else {
473473
tryExportNoteValueToFile(it, textToExport, true)
@@ -626,7 +626,7 @@ class MainActivity : SimpleActivity() {
626626

627627
private fun saveCurrentNote(force: Boolean) {
628628
getPagerAdapter().saveCurrentNote(view_pager.currentItem, force)
629-
if (mCurrentNote.type == TYPE_CHECKLIST) {
629+
if (mCurrentNote.type == NoteType.TYPE_CHECKLIST.value) {
630630
mCurrentNote.value = getPagerAdapter().getNoteChecklistItems(view_pager.currentItem) ?: ""
631631
}
632632
}
@@ -712,7 +712,7 @@ class MainActivity : SimpleActivity() {
712712
}
713713

714714
private fun shareText() {
715-
val text = if (mCurrentNote.type == TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
715+
val text = if (mCurrentNote.type == NoteType.TYPE_TEXT.value) getCurrentNoteText() else mCurrentNote.value
716716
if (text == null || text.isEmpty()) {
717717
toast(R.string.cannot_share_empty_text)
718718
return

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class WidgetConfigureActivity : SimpleActivity() {
122122
private fun updateCurrentNote(note: Note) {
123123
mCurrentNoteId = note.id!!
124124
notes_picker_value.text = note.title
125-
if (note.type == TYPE_CHECKLIST) {
125+
if (note.type == NoteType.TYPE_CHECKLIST.value) {
126126
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
127127
val items = Gson().fromJson<ArrayList<ChecklistItem>>(note.value, checklistItemType) ?: ArrayList(1)
128128
items.apply {

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.simplemobiletools.notes.pro.fragments.ChecklistFragment
1010
import com.simplemobiletools.notes.pro.fragments.NoteFragment
1111
import com.simplemobiletools.notes.pro.fragments.TextFragment
1212
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
13-
import com.simplemobiletools.notes.pro.helpers.TYPE_TEXT
13+
import com.simplemobiletools.notes.pro.helpers.NoteType
1414
import com.simplemobiletools.notes.pro.models.Note
1515

1616
class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity: Activity) : FragmentStatePagerAdapter(fm) {
@@ -30,7 +30,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
3030
return fragments[position]!!
3131
}
3232

33-
val fragment = if (note.type == TYPE_TEXT) TextFragment() else ChecklistFragment()
33+
val fragment = if (note.type == NoteType.TYPE_TEXT.value) TextFragment() else ChecklistFragment()
3434
fragment.arguments = bundle
3535
fragments[position] = fragment
3636
return fragment

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
3737
}
3838

3939
val textSize = context.getTextSize() / context.resources.displayMetrics.density
40-
if (note!!.type == TYPE_CHECKLIST) {
40+
if (note!!.type == NoteType.TYPE_CHECKLIST.value) {
4141
remoteView = RemoteViews(context.packageName, R.layout.item_checklist_widget).apply {
4242
val checklistItem = checklistItems.getOrNull(position) ?: return@apply
4343
setText(checklist_title, checklistItem.title)
@@ -91,7 +91,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
9191
widgetTextColor = intent.getIntExtra(WIDGET_TEXT_COLOR, DEFAULT_WIDGET_TEXT_COLOR)
9292
val noteId = intent.getLongExtra(NOTE_ID, 0L)
9393
note = context.notesDB.getNoteWithId(noteId)
94-
if (note?.type == TYPE_CHECKLIST) {
94+
if (note?.type == NoteType.TYPE_CHECKLIST.value) {
9595
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
9696
checklistItems = Gson().fromJson<ArrayList<ChecklistItem>>(note!!.value, checklistItemType) ?: ArrayList(1)
9797
if (context.config.moveUndoneChecklistItems) {
@@ -103,7 +103,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
103103
override fun hasStableIds() = true
104104

105105
override fun getCount(): Int {
106-
return if (note?.type == TYPE_CHECKLIST) {
106+
return if (note?.type == NoteType.TYPE_CHECKLIST.value) {
107107
checklistItems.size
108108
} else {
109109
1

app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
99
import com.simplemobiletools.commons.helpers.DEFAULT_WIDGET_BG_COLOR
1010
import com.simplemobiletools.notes.pro.R
1111
import com.simplemobiletools.notes.pro.helpers.DEFAULT_WIDGET_TEXT_COLOR
12-
import com.simplemobiletools.notes.pro.helpers.TYPE_TEXT
12+
import com.simplemobiletools.notes.pro.helpers.NoteType
1313
import com.simplemobiletools.notes.pro.interfaces.NotesDao
1414
import com.simplemobiletools.notes.pro.interfaces.WidgetsDao
1515
import com.simplemobiletools.notes.pro.models.Note
@@ -53,7 +53,7 @@ abstract class NotesDatabase : RoomDatabase() {
5353
private fun insertFirstNote(context: Context) {
5454
Executors.newSingleThreadScheduledExecutor().execute {
5555
val generalNote = context.resources.getString(R.string.general_note)
56-
val note = Note(null, generalNote, "", TYPE_TEXT)
56+
val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value)
5757
db!!.NotesDao().insertOrUpdate(note)
5858
}
5959
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import com.simplemobiletools.notes.pro.R
1111
import com.simplemobiletools.notes.pro.activities.SimpleActivity
1212
import com.simplemobiletools.notes.pro.extensions.notesDB
1313
import com.simplemobiletools.notes.pro.extensions.parseChecklistItems
14+
import com.simplemobiletools.notes.pro.helpers.NoteType
1415
import com.simplemobiletools.notes.pro.helpers.NotesHelper
15-
import com.simplemobiletools.notes.pro.helpers.TYPE_CHECKLIST
16-
import com.simplemobiletools.notes.pro.helpers.TYPE_TEXT
1716
import com.simplemobiletools.notes.pro.models.Note
1817
import kotlinx.android.synthetic.main.dialog_import_folder.view.*
1918
import java.io.File
@@ -59,14 +58,14 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
5958
val fileText = it.readText().trim()
6059
val checklistItems = fileText.parseChecklistItems()
6160
if (checklistItems != null) {
62-
saveNote(title.substringBeforeLast('.'), fileText, TYPE_CHECKLIST, "")
61+
saveNote(title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST.value, "")
6362
} else {
6463
if (updateFilesOnEdit) {
6564
activity.handleSAFDialog(path) {
66-
saveNote(title, value, TYPE_TEXT, storePath)
65+
saveNote(title, value, NoteType.TYPE_TEXT.value, storePath)
6766
}
6867
} else {
69-
saveNote(title, value, TYPE_TEXT, storePath)
68+
saveNote(title, value, NoteType.TYPE_TEXT.value, storePath)
7069
}
7170
}
7271
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1111
import com.simplemobiletools.notes.pro.R
1212
import com.simplemobiletools.notes.pro.extensions.config
1313
import com.simplemobiletools.notes.pro.extensions.notesDB
14-
import com.simplemobiletools.notes.pro.helpers.TYPE_CHECKLIST
15-
import com.simplemobiletools.notes.pro.helpers.TYPE_TEXT
14+
import com.simplemobiletools.notes.pro.helpers.NoteType
1615
import com.simplemobiletools.notes.pro.models.Note
1716
import kotlinx.android.synthetic.main.dialog_new_note.view.*
1817

1918
class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
2019
init {
2120
val view = activity.layoutInflater.inflate(R.layout.dialog_new_note, null).apply {
22-
new_note_type.check(if (activity.config.lastCreatedNoteType == TYPE_TEXT) type_text_note.id else type_checklist.id)
21+
new_note_type.check(if (activity.config.lastCreatedNoteType == NoteType.TYPE_TEXT.value) type_text_note.id else type_checklist.id)
2322
}
2423

2524
AlertDialog.Builder(activity)
@@ -35,7 +34,7 @@ class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
3534
title.isEmpty() -> activity.toast(R.string.no_title)
3635
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
3736
else -> {
38-
val type = if (view.new_note_type.checkedRadioButtonId == view.type_checklist.id) TYPE_CHECKLIST else TYPE_TEXT
37+
val type = if (view.new_note_type.checkedRadioButtonId == view.type_checklist.id) NoteType.TYPE_CHECKLIST.value else NoteType.TYPE_TEXT.value
3938
activity.config.lastCreatedNoteType = type
4039
val newNote = Note(null, title, "", type)
4140
callback(newNote)

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenFileDialog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.simplemobiletools.commons.extensions.humanizePath
77
import com.simplemobiletools.commons.extensions.setupDialogStuff
88
import com.simplemobiletools.notes.pro.R
99
import com.simplemobiletools.notes.pro.activities.SimpleActivity
10-
import com.simplemobiletools.notes.pro.helpers.TYPE_TEXT
10+
import com.simplemobiletools.notes.pro.helpers.NoteType
1111
import com.simplemobiletools.notes.pro.models.Note
1212
import kotlinx.android.synthetic.main.dialog_open_file.*
1313
import kotlinx.android.synthetic.main.dialog_open_file.view.*
@@ -45,7 +45,7 @@ class OpenFileDialog(val activity: SimpleActivity, val path: String, val callbac
4545

4646
private fun saveNote(storeContent: String, storePath: String) {
4747
val filename = path.getFilenameFromPath()
48-
val note = Note(null, filename, storeContent, TYPE_TEXT, storePath)
48+
val note = Note(null, filename, storeContent, NoteType.TYPE_TEXT.value, storePath)
4949
callback(note)
5050
dialog.dismiss()
5151
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.simplemobiletools.notes.pro.extensions
22

33
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentActivity
45
import com.simplemobiletools.notes.pro.helpers.Config
56

67
val Fragment.config: Config? get() = if (context != null) Config.newInstance(context!!) else null
8+
9+
val Fragment.requiredActivity: FragmentActivity get() = this.activity!!

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
1515
import com.simplemobiletools.notes.pro.dialogs.NewChecklistItemDialog
1616
import com.simplemobiletools.notes.pro.extensions.config
1717
import com.simplemobiletools.notes.pro.extensions.notesDB
18+
import com.simplemobiletools.notes.pro.extensions.requiredActivity
1819
import com.simplemobiletools.notes.pro.extensions.updateWidgets
1920
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
21+
import com.simplemobiletools.notes.pro.helpers.NoteType
2022
import com.simplemobiletools.notes.pro.helpers.NotesHelper
2123
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
2224
import com.simplemobiletools.notes.pro.models.ChecklistItem
@@ -39,17 +41,23 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
3941
override fun onResume() {
4042
super.onResume()
4143

42-
NotesHelper(activity!!).getNoteWithId(noteId) {
44+
NotesHelper(requiredActivity).getNoteWithId(noteId) {
4345
if (it != null && activity?.isDestroyed == false) {
4446
note = it
4547

46-
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
47-
items = Gson().fromJson<ArrayList<ChecklistItem>>(note!!.value, checklistItemType) ?: ArrayList(1)
48+
try {
49+
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
50+
items = Gson().fromJson<ArrayList<ChecklistItem>>(note!!.value, checklistItemType) ?: ArrayList(1)
51+
} catch (e: Exception) {
52+
note?.run { migrateCheckListOnFailure(it) }
53+
e.printStackTrace()
54+
}
55+
4856
if (config!!.moveUndoneChecklistItems) {
4957
items.sortBy { it.isDone }
5058
}
5159

52-
context!!.updateTextColors(view.checklist_holder)
60+
requiredActivity.updateTextColors(view.checklist_holder)
5361
setupFragment()
5462
}
5563
}
@@ -62,19 +70,35 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
6270
}
6371
}
6472

73+
private fun migrateCheckListOnFailure(note: Note) {
74+
items.clear()
75+
76+
val notes = note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }
77+
78+
notes.forEachIndexed { index, value ->
79+
items.add(ChecklistItem(
80+
id = index,
81+
title = value,
82+
isDone = false
83+
))
84+
}
85+
86+
saveChecklist()
87+
}
88+
6589
private fun setupFragment() {
66-
val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (context!!.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE)
90+
val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE)
6791
view.apply {
6892
checklist_fab.apply {
6993
setImageDrawable(plusIcon)
70-
background.applyColorFilter(context!!.getAdjustedPrimaryColor())
94+
background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor())
7195
setOnClickListener {
7296
showNewItemDialog()
7397
}
7498
}
7599

76100
fragment_placeholder_2.apply {
77-
setTextColor(context!!.getAdjustedPrimaryColor())
101+
setTextColor(requiredActivity.getAdjustedPrimaryColor())
78102
underlineText()
79103
setOnClickListener {
80104
showNewItemDialog()

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Config.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Config(context: Context) : BaseConfig(context) {
7272
set(useIncognitoMode) = prefs.edit().putBoolean(USE_INCOGNITO_MODE, useIncognitoMode).apply()
7373

7474
var lastCreatedNoteType: Int
75-
get() = prefs.getInt(LAST_CREATED_NOTE_TYPE, TYPE_TEXT)
75+
get() = prefs.getInt(LAST_CREATED_NOTE_TYPE, NoteType.TYPE_TEXT.value)
7676
set(lastCreatedNoteType) = prefs.edit().putInt(LAST_CREATED_NOTE_TYPE, lastCreatedNoteType).apply()
7777

7878
var moveUndoneChecklistItems: Boolean

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ const val GRAVITY_CENTER = 1
3737
const val GRAVITY_RIGHT = 2
3838

3939
// note types
40-
const val TYPE_TEXT = 0
41-
const val TYPE_CHECKLIST = 1
40+
enum class NoteType(val value: Int) { TYPE_TEXT(0), TYPE_CHECKLIST(1) }
4241

4342
// mime types
4443
const val MIME_TEXT_PLAIN = "text/plain"

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class NotesHelper(val context: Context) {
3232

3333
if (notes.isEmpty()) {
3434
val generalNote = context.resources.getString(R.string.general_note)
35-
val note = Note(null, generalNote, "", TYPE_TEXT)
35+
val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value)
3636
context.notesDB.insertOrUpdate(note)
3737
notes.add(note)
3838
}

0 commit comments

Comments
 (0)