mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	mark the currently selected Note in the Open Note dialog
This commit is contained in:
		| @@ -38,19 +38,19 @@ public class Config { | ||||
|         mPrefs.edit().putInt(Constants.FONT_SIZE, size).apply(); | ||||
|     } | ||||
|  | ||||
|     public int getCurrentNoteIndex() { | ||||
|         return mPrefs.getInt(Constants.CURRENT_NOTE_INDEX, 0); | ||||
|     public int getCurrentNoteId() { | ||||
|         return mPrefs.getInt(Constants.CURRENT_NOTE_ID, 0); | ||||
|     } | ||||
|  | ||||
|     public void setCurrentNoteIndex(int index) { | ||||
|         mPrefs.edit().putInt(Constants.CURRENT_NOTE_INDEX, index).apply(); | ||||
|     public void setCurrentNoteId(int id) { | ||||
|         mPrefs.edit().putInt(Constants.CURRENT_NOTE_ID, id).apply(); | ||||
|     } | ||||
|  | ||||
|     public int getWidgetNoteIndex() { | ||||
|     public int getWidgetNoteId() { | ||||
|         return mPrefs.getInt(Constants.WIDGET_NOTE_ID, 1); | ||||
|     } | ||||
|  | ||||
|     public void setWidgetNoteIndex(int id) { | ||||
|     public void setWidgetNoteId(int id) { | ||||
|         mPrefs.edit().putInt(Constants.WIDGET_NOTE_ID, id).apply(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -7,7 +7,7 @@ public class Constants { | ||||
|     public static final String PREFS_KEY = "Notes"; | ||||
|     public static final String IS_FIRST_RUN = "is_first_run"; | ||||
|     public static final String IS_DARK_THEME = "is_dark_theme"; | ||||
|     public static final String CURRENT_NOTE_INDEX = "current_note_index"; | ||||
|     public static final String CURRENT_NOTE_ID = "current_note_id"; | ||||
|     public static final String WIDGET_NOTE_ID = "widget_note_id"; | ||||
|     public static final String FONT_SIZE = "font_size"; | ||||
|     public static final String WIDGET_BG_COLOR = "widget_bg_color"; | ||||
|   | ||||
| @@ -19,6 +19,7 @@ import com.simplemobiletools.notes.R; | ||||
| import com.simplemobiletools.notes.Utils; | ||||
| import com.simplemobiletools.notes.databases.DBHelper; | ||||
| import com.simplemobiletools.notes.models.Note; | ||||
| import com.simplemobiletools.notes.views.dialogs.OpenNoteDialog; | ||||
| import com.simplemobiletools.notes.views.dialogs.WidgetNoteDialog; | ||||
|  | ||||
| import java.util.List; | ||||
| @@ -27,7 +28,7 @@ import butterknife.BindView; | ||||
| import butterknife.ButterKnife; | ||||
| import butterknife.OnClick; | ||||
|  | ||||
| public class MainActivity extends SimpleActivity { | ||||
| public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenNoteListener { | ||||
|     @BindView(R.id.notes_view) EditText mNotesView; | ||||
|     @BindView(R.id.current_note_label) TextView mCurrNoteLabel; | ||||
|     @BindView(R.id.current_note_title) TextView mCurrNoteTitle; | ||||
| @@ -44,7 +45,7 @@ public class MainActivity extends SimpleActivity { | ||||
|  | ||||
|         mDb = DBHelper.newInstance(getApplicationContext()); | ||||
|         mNotes = mDb.getNotes(); | ||||
|         updateSelectedNote(mConfig.getCurrentNoteIndex()); | ||||
|         updateSelectedNote(mConfig.getCurrentNoteId()); | ||||
| } | ||||
|  | ||||
|     @Override | ||||
| @@ -111,10 +112,13 @@ public class MainActivity extends SimpleActivity { | ||||
|         new WidgetNoteDialog(this); | ||||
|     } | ||||
|  | ||||
|     private void updateSelectedNote(int index) { | ||||
|     private void updateSelectedNote(int id) { | ||||
|         saveText(); | ||||
|         mConfig.setCurrentNoteIndex(index); | ||||
|         mCurrentNote = mNotes.get(index); | ||||
|         mCurrentNote = mDb.getNote(id); | ||||
|         if (mCurrentNote == null) | ||||
|             return; | ||||
|  | ||||
|         mConfig.setCurrentNoteId(id); | ||||
|         mNotesView.setText(mCurrentNote.getValue()); | ||||
|         mCurrNoteTitle.setText(mCurrentNote.getTitle()); | ||||
|  | ||||
| @@ -199,22 +203,7 @@ public class MainActivity extends SimpleActivity { | ||||
|     } | ||||
|  | ||||
|     private void displayOpenNoteDialog() { | ||||
|         final AlertDialog.Builder builder = new AlertDialog.Builder(this); | ||||
|         builder.setTitle(getResources().getString(R.string.pick_a_note)); | ||||
|  | ||||
|         final int cnt = mNotes.size(); | ||||
|         String[] notes = new String[cnt]; | ||||
|         for (int i = 0; i < cnt; i++) { | ||||
|             notes[i] = mNotes.get(i).getTitle(); | ||||
|         } | ||||
|  | ||||
|         builder.setItems(notes, new DialogInterface.OnClickListener() { | ||||
|             @Override | ||||
|             public void onClick(DialogInterface dialog, int which) { | ||||
|                 updateSelectedNote(which); | ||||
|             } | ||||
|         }); | ||||
|         builder.show(); | ||||
|         new OpenNoteDialog(this); | ||||
|     } | ||||
|  | ||||
|     private void saveText() { | ||||
| @@ -258,4 +247,9 @@ public class MainActivity extends SimpleActivity { | ||||
|         final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | ||||
|         imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void noteSelected(int id) { | ||||
|         updateSelectedNote(id); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,48 @@ | ||||
| package com.simplemobiletools.notes.views.dialogs | ||||
|  | ||||
| import android.app.Activity | ||||
| import android.app.AlertDialog | ||||
| import android.view.ViewGroup | ||||
| import android.widget.RadioButton | ||||
| import android.widget.RadioGroup | ||||
| import com.simplemobiletools.notes.Config | ||||
| import com.simplemobiletools.notes.R | ||||
| import com.simplemobiletools.notes.databases.DBHelper | ||||
|  | ||||
| class OpenNoteDialog(val activity: Activity) : AlertDialog.Builder(activity), RadioGroup.OnCheckedChangeListener { | ||||
|     val dialog: AlertDialog? | ||||
|  | ||||
|     init { | ||||
|         val config = Config.newInstance(context) | ||||
|         val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) as RadioGroup | ||||
|         view.setOnCheckedChangeListener(this) | ||||
|  | ||||
|         val db = DBHelper.newInstance(context) | ||||
|         val notes = db.notes | ||||
|         notes.forEach { | ||||
|             val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton | ||||
|             radioButton.apply { | ||||
|                 text = it.title | ||||
|                 isChecked = it.id == config.currentNoteId | ||||
|                 id = it.id | ||||
|             } | ||||
|             view.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) | ||||
|         } | ||||
|  | ||||
|         dialog = AlertDialog.Builder(activity) | ||||
|                 .setTitle(activity.resources.getString(R.string.pick_a_note)) | ||||
|                 .setView(view) | ||||
|                 .create() | ||||
|  | ||||
|         dialog?.show() | ||||
|     } | ||||
|  | ||||
|     override fun onCheckedChanged(group: RadioGroup, checkedId: Int) { | ||||
|         (activity as OpenNoteListener).noteSelected(checkedId) | ||||
|         dialog?.dismiss() | ||||
|     } | ||||
|  | ||||
|     interface OpenNoteListener { | ||||
|         fun noteSelected(id: Int) | ||||
|     } | ||||
| } | ||||
| @@ -15,7 +15,7 @@ class WidgetNoteDialog(val activity: Activity) : AlertDialog.Builder(activity), | ||||
|  | ||||
|     init { | ||||
|         mConfig = Config.newInstance(context) | ||||
|         val view = activity.layoutInflater.inflate(R.layout.dialog_change_widget_note, null) as RadioGroup | ||||
|         val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) as RadioGroup | ||||
|         view.setOnCheckedChangeListener(this) | ||||
|  | ||||
|         val db = DBHelper.newInstance(context) | ||||
| @@ -24,7 +24,7 @@ class WidgetNoteDialog(val activity: Activity) : AlertDialog.Builder(activity), | ||||
|             val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton | ||||
|             radioButton.apply { | ||||
|                 text = it.title | ||||
|                 isChecked = it.id == mConfig.widgetNoteIndex | ||||
|                 isChecked = it.id == mConfig.widgetNoteId | ||||
|                 id = it.id | ||||
|             } | ||||
|             view.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) | ||||
| @@ -39,7 +39,7 @@ class WidgetNoteDialog(val activity: Activity) : AlertDialog.Builder(activity), | ||||
|     } | ||||
|  | ||||
|     override fun onCheckedChanged(group: RadioGroup, checkedId: Int) { | ||||
|         mConfig.widgetNoteIndex = checkedId | ||||
|         mConfig.widgetNoteId = checkedId | ||||
|         dialog?.dismiss() | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user