Skip to content

Commit 742fe5d

Browse files
committed
couple improvements to widgets and intent handling
1 parent a6b9cbb commit 742fe5d

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class MainActivity : SimpleActivity() {
5858
setContentView(R.layout.activity_main)
5959
appLaunched(BuildConfig.APPLICATION_ID)
6060

61-
initViewPager()
62-
61+
initViewPager(intent.getLongExtra(OPEN_NOTE_ID, -1L))
6362
pager_title_strip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize())
6463
pager_title_strip.layoutParams.height = (pager_title_strip.height + resources.getDimension(R.dimen.activity_margin) * 2).toInt()
6564
checkWhatsNewDialog()
@@ -202,7 +201,8 @@ class MainActivity : SimpleActivity() {
202201

203202
override fun onNewIntent(intent: Intent) {
204203
super.onNewIntent(intent)
205-
view_pager.currentItem = getWantedNoteIndex(null)
204+
val wantedNoteId = intent.getLongExtra(OPEN_NOTE_ID, -1L)
205+
view_pager.currentItem = getWantedNoteIndex(wantedNoteId)
206206
}
207207

208208
private fun storeStateVariables() {
@@ -256,6 +256,7 @@ class MainActivity : SimpleActivity() {
256256
view_pager.apply {
257257
adapter = mAdapter
258258
currentItem = getWantedNoteIndex(wantedNoteId)
259+
config.currentNoteId = mCurrentNote.id!!
259260

260261
onPageChangeListener {
261262
mCurrentNote = mNotes[it]
@@ -269,12 +270,10 @@ class MainActivity : SimpleActivity() {
269270
}
270271
}
271272

272-
private fun getWantedNoteIndex(secondaryWantedNoteId: Long?): Int {
273-
var wantedNoteId = intent.getLongExtra(OPEN_NOTE_ID, -1)
274-
if (wantedNoteId == -1L) {
275-
wantedNoteId = secondaryWantedNoteId ?: config.currentNoteId
276-
}
277-
return getNoteIndexWithId(wantedNoteId)
273+
private fun getWantedNoteIndex(wantedNoteId: Long?): Int {
274+
intent.removeExtra(OPEN_NOTE_ID)
275+
val noteIdToOpen = if (wantedNoteId == null || wantedNoteId == -1L) config.currentNoteId else wantedNoteId
276+
return getNoteIndexWithId(noteIdToOpen)
278277
}
279278

280279
private fun currentNotesView() = if (view_pager == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SplashActivity : BaseSplashActivity() {
88
override fun initActivity() {
99
if (intent.extras?.containsKey(OPEN_NOTE_ID) == true) {
1010
Intent(this, MainActivity::class.java).apply {
11-
putExtra(OPEN_NOTE_ID, intent.getIntExtra(OPEN_NOTE_ID, -1))
11+
putExtra(OPEN_NOTE_ID, intent.getLongExtra(OPEN_NOTE_ID, -1L))
1212
startActivity(this)
1313
}
1414
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
1818
override fun getItem(position: Int): NoteFragment {
1919
val bundle = Bundle()
2020
val id = notes[position].id
21-
bundle.putInt(NOTE_ID, id!!.toInt())
21+
bundle.putLong(NOTE_ID, id!!)
2222

2323
if (fragments.containsKey(position)) {
2424
return fragments[position]!!

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
2222
private var widgetTextColor = context.config.widgetTextColor
2323

2424
override fun getViewAt(position: Int): RemoteViews {
25-
val noteId = intent.getIntExtra(NOTE_ID, 1)
25+
val noteId = intent.getLongExtra(NOTE_ID, 0L)
2626
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
2727
val note = context.notesDB.getNoteWithId(noteId)
2828
if (note != null) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ class NoteFragment : androidx.fragment.app.Fragment() {
3838
private var textHistory = TextHistory()
3939
private var isUndoOrRedo = false
4040
private var skipTextUpdating = false
41-
private var noteId = 0
41+
private var noteId = 0L
4242
private var note: Note? = null
4343

4444
lateinit var view: ViewGroup
4545

4646
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
4747
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
48-
noteId = arguments!!.getInt(NOTE_ID)
48+
noteId = arguments!!.getLong(NOTE_ID)
4949
retainInstance = true
5050

5151
val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable
@@ -87,11 +87,11 @@ class NoteFragment : androidx.fragment.app.Fragment() {
8787

8888
override fun setMenuVisibility(menuVisible: Boolean) {
8989
super.setMenuVisibility(menuVisible)
90-
if (!menuVisible && noteId != 0 && config?.autosaveNotes == true) {
90+
if (!menuVisible && noteId != 0L && config?.autosaveNotes == true) {
9191
saveText(false)
9292
}
9393

94-
if (menuVisible && noteId != 0) {
94+
if (menuVisible && noteId != 0L) {
9595
val currentText = getCurrentNoteViewText()
9696
if (currentText != null) {
9797
(activity as MainActivity).currentNoteTextChanged(currentText, isUndoAvailable(), isRedoAvailable())

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ class MyWidgetProvider : AppWidgetProvider() {
2626

2727
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
2828
super.onUpdate(context, appWidgetManager, appWidgetIds)
29-
context.widgetsDB.getWidgets().forEach {
30-
val views = RemoteViews(context.packageName, R.layout.widget)
31-
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
32-
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it)
29+
Thread {
30+
context.widgetsDB.getWidgets().forEach {
31+
val views = RemoteViews(context.packageName, R.layout.widget)
32+
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
33+
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it)
3334

34-
Intent(context, WidgetService::class.java).apply {
35-
putExtra(NOTE_ID, it.noteId)
36-
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
37-
views.setRemoteAdapter(R.id.notes_widget_listview, this)
38-
}
35+
Intent(context, WidgetService::class.java).apply {
36+
putExtra(NOTE_ID, it.noteId)
37+
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
38+
views.setRemoteAdapter(R.id.notes_widget_listview, this)
39+
}
3940

40-
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
41-
startActivityIntent.putExtra(OPEN_NOTE_ID, it.noteId)
42-
val startActivityPendingIntent = PendingIntent.getActivity(context, it.widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
43-
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
41+
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
42+
startActivityIntent.putExtra(OPEN_NOTE_ID, it.noteId)
43+
val startActivityPendingIntent = PendingIntent.getActivity(context, it.widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
44+
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
4445

45-
appWidgetManager.updateAppWidget(it.widgetId, views)
46-
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
47-
}
46+
appWidgetManager.updateAppWidget(it.widgetId, views)
47+
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
48+
}
49+
}.start()
4850
}
4951
}

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
@@ -40,7 +40,7 @@ class NotesHelper(val activity: Activity) {
4040
}.start()
4141
}
4242

43-
fun getNoteWithId(id: Int, callback: (note: Note?) -> Unit) {
43+
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
4444
Thread {
4545
val note = activity.notesDB.getNoteWithId(id)
4646
activity.runOnUiThread {

app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface NotesDao {
99
fun getNotes(): List<Note>
1010

1111
@Query("SELECT * FROM notes WHERE id = :id")
12-
fun getNoteWithId(id: Int): Note?
12+
fun getNoteWithId(id: Long): Note?
1313

1414
@Query("SELECT id FROM notes WHERE path = :path")
1515
fun getNoteIdWithPath(path: String): Long?

0 commit comments

Comments
 (0)