couple improvements to widgets and intent handling

This commit is contained in:
tibbi 2018-11-07 23:46:17 +01:00
parent a6b9cbba9b
commit 742fe5d842
8 changed files with 36 additions and 35 deletions
app/src/main/kotlin/com/simplemobiletools/notes/pro

@ -58,8 +58,7 @@ class MainActivity : SimpleActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID) appLaunched(BuildConfig.APPLICATION_ID)
initViewPager() initViewPager(intent.getLongExtra(OPEN_NOTE_ID, -1L))
pager_title_strip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize()) pager_title_strip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize())
pager_title_strip.layoutParams.height = (pager_title_strip.height + resources.getDimension(R.dimen.activity_margin) * 2).toInt() pager_title_strip.layoutParams.height = (pager_title_strip.height + resources.getDimension(R.dimen.activity_margin) * 2).toInt()
checkWhatsNewDialog() checkWhatsNewDialog()
@ -202,7 +201,8 @@ class MainActivity : SimpleActivity() {
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent) super.onNewIntent(intent)
view_pager.currentItem = getWantedNoteIndex(null) val wantedNoteId = intent.getLongExtra(OPEN_NOTE_ID, -1L)
view_pager.currentItem = getWantedNoteIndex(wantedNoteId)
} }
private fun storeStateVariables() { private fun storeStateVariables() {
@ -256,6 +256,7 @@ class MainActivity : SimpleActivity() {
view_pager.apply { view_pager.apply {
adapter = mAdapter adapter = mAdapter
currentItem = getWantedNoteIndex(wantedNoteId) currentItem = getWantedNoteIndex(wantedNoteId)
config.currentNoteId = mCurrentNote.id!!
onPageChangeListener { onPageChangeListener {
mCurrentNote = mNotes[it] mCurrentNote = mNotes[it]
@ -269,12 +270,10 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun getWantedNoteIndex(secondaryWantedNoteId: Long?): Int { private fun getWantedNoteIndex(wantedNoteId: Long?): Int {
var wantedNoteId = intent.getLongExtra(OPEN_NOTE_ID, -1) intent.removeExtra(OPEN_NOTE_ID)
if (wantedNoteId == -1L) { val noteIdToOpen = if (wantedNoteId == null || wantedNoteId == -1L) config.currentNoteId else wantedNoteId
wantedNoteId = secondaryWantedNoteId ?: config.currentNoteId return getNoteIndexWithId(noteIdToOpen)
}
return getNoteIndexWithId(wantedNoteId)
} }
private fun currentNotesView() = if (view_pager == null) { private fun currentNotesView() = if (view_pager == null) {

@ -8,7 +8,7 @@ class SplashActivity : BaseSplashActivity() {
override fun initActivity() { override fun initActivity() {
if (intent.extras?.containsKey(OPEN_NOTE_ID) == true) { if (intent.extras?.containsKey(OPEN_NOTE_ID) == true) {
Intent(this, MainActivity::class.java).apply { Intent(this, MainActivity::class.java).apply {
putExtra(OPEN_NOTE_ID, intent.getIntExtra(OPEN_NOTE_ID, -1)) putExtra(OPEN_NOTE_ID, intent.getLongExtra(OPEN_NOTE_ID, -1L))
startActivity(this) startActivity(this)
} }
} else { } else {

@ -18,7 +18,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
override fun getItem(position: Int): NoteFragment { override fun getItem(position: Int): NoteFragment {
val bundle = Bundle() val bundle = Bundle()
val id = notes[position].id val id = notes[position].id
bundle.putInt(NOTE_ID, id!!.toInt()) bundle.putLong(NOTE_ID, id!!)
if (fragments.containsKey(position)) { if (fragments.containsKey(position)) {
return fragments[position]!! return fragments[position]!!

@ -22,7 +22,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
private var widgetTextColor = context.config.widgetTextColor private var widgetTextColor = context.config.widgetTextColor
override fun getViewAt(position: Int): RemoteViews { override fun getViewAt(position: Int): RemoteViews {
val noteId = intent.getIntExtra(NOTE_ID, 1) val noteId = intent.getLongExtra(NOTE_ID, 0L)
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply { val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
val note = context.notesDB.getNoteWithId(noteId) val note = context.notesDB.getNoteWithId(noteId)
if (note != null) { if (note != null) {

@ -38,14 +38,14 @@ class NoteFragment : androidx.fragment.app.Fragment() {
private var textHistory = TextHistory() private var textHistory = TextHistory()
private var isUndoOrRedo = false private var isUndoOrRedo = false
private var skipTextUpdating = false private var skipTextUpdating = false
private var noteId = 0 private var noteId = 0L
private var note: Note? = null private var note: Note? = null
lateinit var view: ViewGroup lateinit var view: ViewGroup
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
noteId = arguments!!.getInt(NOTE_ID) noteId = arguments!!.getLong(NOTE_ID)
retainInstance = true retainInstance = true
val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable 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() {
override fun setMenuVisibility(menuVisible: Boolean) { override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
if (!menuVisible && noteId != 0 && config?.autosaveNotes == true) { if (!menuVisible && noteId != 0L && config?.autosaveNotes == true) {
saveText(false) saveText(false)
} }
if (menuVisible && noteId != 0) { if (menuVisible && noteId != 0L) {
val currentText = getCurrentNoteViewText() val currentText = getCurrentNoteViewText()
if (currentText != null) { if (currentText != null) {
(activity as MainActivity).currentNoteTextChanged(currentText, isUndoAvailable(), isRedoAvailable()) (activity as MainActivity).currentNoteTextChanged(currentText, isUndoAvailable(), isRedoAvailable())

@ -26,24 +26,26 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds) super.onUpdate(context, appWidgetManager, appWidgetIds)
context.widgetsDB.getWidgets().forEach { Thread {
val views = RemoteViews(context.packageName, R.layout.widget) context.widgetsDB.getWidgets().forEach {
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor) val views = RemoteViews(context.packageName, R.layout.widget)
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it) views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it)
Intent(context, WidgetService::class.java).apply { Intent(context, WidgetService::class.java).apply {
putExtra(NOTE_ID, it.noteId) putExtra(NOTE_ID, it.noteId)
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME)) data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
views.setRemoteAdapter(R.id.notes_widget_listview, this) views.setRemoteAdapter(R.id.notes_widget_listview, this)
}
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
startActivityIntent.putExtra(OPEN_NOTE_ID, it.noteId)
val startActivityPendingIntent = PendingIntent.getActivity(context, it.widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
appWidgetManager.updateAppWidget(it.widgetId, views)
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
} }
}.start()
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
startActivityIntent.putExtra(OPEN_NOTE_ID, it.noteId)
val startActivityPendingIntent = PendingIntent.getActivity(context, it.widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
appWidgetManager.updateAppWidget(it.widgetId, views)
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
}
} }
} }

@ -40,7 +40,7 @@ class NotesHelper(val activity: Activity) {
}.start() }.start()
} }
fun getNoteWithId(id: Int, callback: (note: Note?) -> Unit) { fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
Thread { Thread {
val note = activity.notesDB.getNoteWithId(id) val note = activity.notesDB.getNoteWithId(id)
activity.runOnUiThread { activity.runOnUiThread {

@ -9,7 +9,7 @@ interface NotesDao {
fun getNotes(): List<Note> fun getNotes(): List<Note>
@Query("SELECT * FROM notes WHERE id = :id") @Query("SELECT * FROM notes WHERE id = :id")
fun getNoteWithId(id: Int): Note? fun getNoteWithId(id: Long): Note?
@Query("SELECT id FROM notes WHERE path = :path") @Query("SELECT id FROM notes WHERE path = :path")
fun getNoteIdWithPath(path: String): Long? fun getNoteIdWithPath(path: String): Long?