Skip to content

Commit 0e9333d

Browse files
committed
Removed force null wrapping
1 parent ce2b1e5 commit 0e9333d

File tree

11 files changed

+91
-77
lines changed

11 files changed

+91
-77
lines changed

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

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class MainActivity : SimpleActivity() {
183183
findItem(R.id.unlock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
184184

185185
saveNoteButton = findItem(R.id.save_note)
186-
saveNoteButton!!.isVisible =
186+
saveNoteButton?.isVisible =
187187
!config.autosaveNotes && showSaveButton && (::mCurrentNote.isInitialized && mCurrentNote.type == NoteType.TYPE_TEXT.value)
188188
}
189189

@@ -270,17 +270,24 @@ class MainActivity : SimpleActivity() {
270270

271271
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
272272
super.onActivityResult(requestCode, resultCode, resultData)
273-
if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
274-
importUri(resultData.data!!)
275-
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
276-
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
277-
applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags)
278-
showExportFilePickUpdateDialog(resultData.dataString!!, getCurrentNoteValue())
279-
} else if (requestCode == PICK_EXPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
280-
val outputStream = contentResolver.openOutputStream(resultData.data!!)
281-
exportNotesTo(outputStream)
282-
} else if (requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
283-
importNotesFrom(resultData.data!!)
273+
resultData?.data?.let {
274+
when {
275+
requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK -> {
276+
importUri(it)
277+
}
278+
requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && mNotes.isNotEmpty() -> {
279+
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
280+
applicationContext.contentResolver.takePersistableUriPermission(it, takeFlags)
281+
showExportFilePickUpdateDialog(resultData.dataString ?: "", getCurrentNoteValue())
282+
}
283+
requestCode == PICK_EXPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK -> {
284+
val outputStream = contentResolver.openOutputStream(it)
285+
exportNotesTo(outputStream)
286+
}
287+
requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK -> {
288+
importNotesFrom(it)
289+
}
290+
}
284291
}
285292
}
286293

@@ -352,17 +359,22 @@ class MainActivity : SimpleActivity() {
352359
val realPath = intent.getStringExtra(REAL_FILE_PATH)
353360
val isFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
354361
if (!isFromHistory) {
355-
if (realPath != null && hasPermission(PERMISSION_READ_STORAGE)) {
356-
val file = File(realPath)
357-
handleUri(Uri.fromFile(file))
358-
} else if (intent.getBooleanExtra(NEW_TEXT_NOTE, false)) {
359-
val newTextNote = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "")
360-
addNewNote(newTextNote)
361-
} else if (intent.getBooleanExtra(NEW_CHECKLIST, false)) {
362-
val newChecklist = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "")
363-
addNewNote(newChecklist)
364-
} else {
365-
handleUri(data!!)
362+
when {
363+
realPath != null && hasPermission(PERMISSION_READ_STORAGE) -> {
364+
val file = File(realPath)
365+
handleUri(Uri.fromFile(file))
366+
}
367+
intent.getBooleanExtra(NEW_TEXT_NOTE, false) -> {
368+
val newTextNote = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "")
369+
addNewNote(newTextNote)
370+
}
371+
intent.getBooleanExtra(NEW_CHECKLIST, false) -> {
372+
val newChecklist = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "")
373+
addNewNote(newChecklist)
374+
}
375+
else -> {
376+
handleUri(data!!)
377+
}
366378
}
367379
}
368380
intent.removeCategory(Intent.CATEGORY_DEFAULT)
@@ -393,15 +405,15 @@ class MainActivity : SimpleActivity() {
393405
if (it as Int == 0) {
394406
displayNewNoteDialog(text)
395407
} else {
396-
updateSelectedNote(notes[it - 1].id!!)
408+
updateSelectedNote(notes[it - 1].id ?: -1)
397409
addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else "\n$text")
398410
}
399411
}
400412
}
401413
}
402414

403415
private fun handleUri(uri: Uri) {
404-
NotesHelper(this).getNoteIdWithPath(uri.path!!) {
416+
NotesHelper(this).getNoteIdWithPath(uri.path ?: "") {
405417
if (it != null && it > 0L) {
406418
updateSelectedNote(it)
407419
return@getNoteIdWithPath
@@ -425,11 +437,11 @@ class MainActivity : SimpleActivity() {
425437
view_pager.apply {
426438
adapter = mAdapter
427439
currentItem = getWantedNoteIndex(wantedNoteId)
428-
config.currentNoteId = mCurrentNote.id!!
440+
config.currentNoteId = mCurrentNote.id ?: -1
429441

430442
onPageChangeListener {
431443
mCurrentNote = mNotes[it]
432-
config.currentNoteId = mCurrentNote.id!!
444+
config.currentNoteId = mCurrentNote.id ?: -1
433445
refreshMenuItems()
434446
}
435447
}
@@ -461,7 +473,7 @@ class MainActivity : SimpleActivity() {
461473
view_pager.onPageChangeListener {
462474
currentTextFragment?.removeTextWatcher()
463475
currentNotesView()?.let { noteView ->
464-
noteView.text!!.clearBackgroundSpans()
476+
noteView.text?.clearBackgroundSpans()
465477
}
466478

467479
closeSearch()
@@ -481,7 +493,7 @@ class MainActivity : SimpleActivity() {
481493
private fun searchTextChanged(text: String) {
482494
currentNotesView()?.let { noteView ->
483495
currentTextFragment?.removeTextWatcher()
484-
noteView.text!!.clearBackgroundSpans()
496+
noteView.text?.clearBackgroundSpans()
485497

486498
if (text.isNotBlank() && text.length > 1) {
487499
searchMatches = noteView.value.searchMatches(text)
@@ -720,7 +732,7 @@ class MainActivity : SimpleActivity() {
720732

721733
private fun importUri(uri: Uri) {
722734
when (uri.scheme) {
723-
"file" -> openPath(uri.path!!)
735+
"file" -> openPath(uri.path ?: "")
724736
"content" -> {
725737
val realPath = getRealPathFromURI(uri)
726738
if (hasPermission(PERMISSION_READ_STORAGE)) {
@@ -750,8 +762,8 @@ class MainActivity : SimpleActivity() {
750762
}
751763

752764
val inputStream = contentResolver.openInputStream(uri)
753-
val content = inputStream?.bufferedReader().use { it!!.readText() }
754-
val checklistItems = content.parseChecklistItems()
765+
val content = inputStream?.bufferedReader().use { it?.readText() }
766+
val checklistItems = content?.parseChecklistItems()
755767

756768
// if we got here by some other app invoking the file open intent, we have no permission for updating the original file itself
757769
// we can do it only after using "Export as file" or "Open file" from our app
@@ -769,7 +781,7 @@ class MainActivity : SimpleActivity() {
769781

770782
val noteType = if (checklistItems != null) NoteType.TYPE_CHECKLIST.value else NoteType.TYPE_TEXT.value
771783
if (!canSyncNoteWithFile) {
772-
val note = Note(null, noteTitle, content, noteType, "", PROTECTION_NONE, "")
784+
val note = Note(null, noteTitle, content ?: "", noteType, "", PROTECTION_NONE, "")
773785
displayNewNoteDialog(note.value, title = noteTitle, "")
774786
} else {
775787
val items = arrayListOf(
@@ -780,7 +792,7 @@ class MainActivity : SimpleActivity() {
780792
RadioGroupDialog(this, items) {
781793
val syncFile = it as Int == IMPORT_FILE_SYNC
782794
val path = if (syncFile) uri.toString() else ""
783-
val note = Note(null, noteTitle, content, noteType, "", PROTECTION_NONE, "")
795+
val note = Note(null, noteTitle, content ?: "", noteType, "", PROTECTION_NONE, "")
784796
displayNewNoteDialog(note.value, title = noteTitle, path)
785797
}
786798
}
@@ -942,7 +954,7 @@ class MainActivity : SimpleActivity() {
942954

943955
private fun importNotesFrom(uri: Uri) {
944956
when (uri.scheme) {
945-
"file" -> importNotes(uri.path!!, uri.path!!.getFilenameFromPath())
957+
"file" -> importNotes(uri.path ?: "", uri.path?.getFilenameFromPath() ?: "")
946958
"content" -> {
947959
val tempFile = getTempFile("messages", "backup.txt")
948960
if (tempFile == null) {
@@ -954,7 +966,7 @@ class MainActivity : SimpleActivity() {
954966
val filename = getFilenameFromUri(uri)
955967
val inputStream = contentResolver.openInputStream(uri)
956968
val out = FileOutputStream(tempFile)
957-
inputStream!!.copyTo(out)
969+
inputStream?.copyTo(out)
958970
importNotes(tempFile.absolutePath, filename)
959971
} catch (e: Exception) {
960972
showErrorToast(e)
@@ -1077,14 +1089,16 @@ class MainActivity : SimpleActivity() {
10771089
getDocumentFile(path) ?: return@handleSAFDialog
10781090
} else {
10791091
val parent = getDocumentFile(File(path).parent) ?: return@handleSAFDialog
1080-
parent.createFile("", path.getFilenameFromPath())!!
1092+
parent.createFile("", path.getFilenameFromPath())
10811093
}
10821094

1083-
contentResolver.openOutputStream(document.uri)!!.apply {
1084-
val byteArray = content.toByteArray(Charset.forName("UTF-8"))
1085-
write(byteArray, 0, byteArray.size)
1086-
flush()
1087-
close()
1095+
document?.uri?.let { it1 ->
1096+
contentResolver.openOutputStream(it1)?.apply {
1097+
val byteArray = content.toByteArray(Charset.forName("UTF-8"))
1098+
write(byteArray, 0, byteArray.size)
1099+
flush()
1100+
close()
1101+
}
10881102
}
10891103

10901104
if (showSuccessToasts) {
@@ -1110,8 +1124,8 @@ class MainActivity : SimpleActivity() {
11101124
private fun exportNoteValueToUri(uri: Uri, title: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
11111125
try {
11121126
val outputStream = contentResolver.openOutputStream(uri, "rwt")
1113-
outputStream!!.bufferedWriter().use { out ->
1114-
out.write(content)
1127+
outputStream?.bufferedWriter().use { out ->
1128+
out?.write(content)
11151129
}
11161130
if (showSuccessToasts) {
11171131
noteExportedSuccessfully(title)
@@ -1217,7 +1231,7 @@ class MainActivity : SimpleActivity() {
12171231
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
12181232
ensureBackgroundThread {
12191233
notesDB.deleteNote(note)
1220-
widgetsDB.deleteNoteWidgets(note.id!!)
1234+
widgetsDB.deleteNoteWidgets(note.id ?: -1)
12211235
refreshNotes(note, deleteFile)
12221236
}
12231237
}
@@ -1226,9 +1240,9 @@ class MainActivity : SimpleActivity() {
12261240
NotesHelper(this).getNotes {
12271241
mNotes = it
12281242
val firstNoteId = mNotes[0].id
1229-
updateSelectedNote(firstNoteId!!)
1243+
updateSelectedNote(firstNoteId ?: -1)
12301244
if (config.widgetNoteId == note.id) {
1231-
config.widgetNoteId = mCurrentNote.id!!
1245+
config.widgetNoteId = mCurrentNote.id ?: -1
12321246
updateWidgets()
12331247
}
12341248

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ class WidgetConfigureActivity : SimpleActivity() {
131131
private fun showNoteSelector() {
132132
val items = ArrayList<RadioItem>()
133133
mNotes.forEach {
134-
items.add(RadioItem(it.id!!.toInt(), it.title))
134+
items.add(RadioItem(it.id?.toInt() ?: -1, it.title))
135135
}
136136

137137
RadioGroupDialog(this, items, mCurrentNoteId.toInt()) {
138138
val selectedId = it as Int
139-
val note = mNotes.firstOrNull { it.id!!.toInt() == selectedId } ?: return@RadioGroupDialog
139+
val note = mNotes.firstOrNull { it.id?.toInt() == selectedId } ?: return@RadioGroupDialog
140140
if (note.protectionType == PROTECTION_NONE || note.shouldBeUnlocked(this)) {
141141
updateCurrentNote(note)
142142
} else {
@@ -150,7 +150,7 @@ class WidgetConfigureActivity : SimpleActivity() {
150150
}
151151

152152
private fun updateCurrentNote(note: Note) {
153-
mCurrentNoteId = note.id!!
153+
mCurrentNoteId = note.id ?: -1
154154
notes_picker_value.text = note.title
155155
text_note_view_title.text = note.title
156156
if (note.type == NoteType.TYPE_CHECKLIST.value) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ChecklistAdapter(
4646
initDrawables()
4747

4848
touchHelper = ItemTouchHelper(ItemMoveCallback(this))
49-
touchHelper!!.attachToRecyclerView(recyclerView)
49+
touchHelper?.attachToRecyclerView(recyclerView)
5050

5151
startReorderDragListener = object : StartReorderDragListener {
5252
override fun requestDrag(viewHolder: RecyclerView.ViewHolder) {

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
@@ -43,7 +43,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
4343
}
4444

4545
val textSize = context.getPercentageFontSize() / context.resources.displayMetrics.density
46-
if (note!!.type == NoteType.TYPE_CHECKLIST.value) {
46+
if (note?.type == NoteType.TYPE_CHECKLIST.value) {
4747
remoteView = RemoteViews(context.packageName, R.layout.item_checklist_widget).apply {
4848
val checklistItem = checklistItems.getOrNull(position) ?: return@apply
4949
val widgetNewTextColor = if (checklistItem.isDone) widgetTextColor.adjustAlpha(DONE_CHECKLIST_ITEM_ALPHA) else widgetTextColor
@@ -66,7 +66,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
6666
}
6767
} else {
6868
remoteView = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
69-
val noteText = note!!.getNoteStoredValue(context) ?: ""
69+
val noteText = note?.getNoteStoredValue(context) ?: ""
7070
for (id in textIds) {
7171
setText(id, noteText)
7272
setTextColor(id, widgetTextColor)
@@ -125,7 +125,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
125125
note = context.notesDB.getNoteWithId(noteId)
126126
if (note?.type == NoteType.TYPE_CHECKLIST.value) {
127127
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
128-
checklistItems = Gson().fromJson<ArrayList<ChecklistItem>>(note!!.getNoteStoredValue(context), checklistItemType) ?: ArrayList(1)
128+
checklistItems = Gson().fromJson<ArrayList<ChecklistItem>>(note?.getNoteStoredValue(context), checklistItemType) ?: ArrayList(1)
129129

130130
// checklist title can be null only because of the glitch in upgrade to 6.6.0, remove this check in the future
131131
checklistItems = checklistItems.filter { it.title != null }.toMutableList() as ArrayList<ChecklistItem>

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
@@ -43,7 +43,7 @@ abstract class NotesDatabase : RoomDatabase() {
4343
.addMigrations(MIGRATION_2_3)
4444
.addMigrations(MIGRATION_3_4)
4545
.build()
46-
db!!.openHelper.setWriteAheadLoggingEnabled(true)
46+
db?.openHelper?.setWriteAheadLoggingEnabled(true)
4747
}
4848
}
4949
}
@@ -58,7 +58,7 @@ abstract class NotesDatabase : RoomDatabase() {
5858
Executors.newSingleThreadScheduledExecutor().execute {
5959
val generalNote = context.resources.getString(R.string.general_note)
6060
val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "")
61-
db!!.NotesDao().insertOrUpdate(note)
61+
db?.NotesDao()?.insertOrUpdate(note)
6262
}
6363
}
6464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class NewChecklistItemDialog(val activity: Activity, callback: (titles: ArrayLis
4141
alertDialog.showKeyboard(titles.first())
4242
alertDialog.getButton(BUTTON_POSITIVE).setOnClickListener {
4343
when {
44-
titles.all { it.text!!.isEmpty() } -> activity.toast(R.string.empty_name)
44+
titles.all { it.text?.isEmpty() == true } -> activity.toast(R.string.empty_name)
4545
else -> {
4646
val titles = titles.map { it.text.toString() }.filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
4747
callback(titles)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Long, new
3939
open_note_item_radio_button.apply {
4040
text = note.title
4141
isChecked = note.id == activity.config.currentNoteId
42-
id = note.id!!.toInt()
42+
id = note.id?.toInt() ?: -1
4343

4444
setOnClickListener {
45-
callback(note.id!!, null)
45+
callback(note.id ?: -1, null)
4646
dialog?.dismiss()
4747
}
4848
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ abstract class NoteFragment : Fragment() {
5252
}
5353

5454
activity?.performSecurityCheck(
55-
protectionType = note!!.protectionType,
56-
requiredHash = note!!.protectionHash,
55+
protectionType = note?.protectionType ?: -1,
56+
requiredHash = note?.protectionHash ?: "",
5757
successCallback = { _, _ ->
5858
shouldShowLockedContent = true
5959
checkLockState()

0 commit comments

Comments
 (0)