move the notes in a viewpager

This commit is contained in:
tibbi 2016-11-26 16:32:10 +01:00
parent 0f2430e633
commit 93ed653206
15 changed files with 144 additions and 101 deletions

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.notes
val TEXT = "text"
val NOTE_ID = "note_id"
// shared preferences
val PREFS_KEY = "Notes"

View File

@ -1,34 +1,29 @@
package com.simplemobiletools.notes.activities
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.simplemobiletools.filepicker.dialogs.ConfirmationDialog
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.filepicker.extensions.value
import com.simplemobiletools.notes.MyWidgetProvider
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.TYPE_NOTE
import com.simplemobiletools.notes.adapters.NotesPagerAdapter
import com.simplemobiletools.notes.databases.DBHelper
import com.simplemobiletools.notes.dialogs.NewNoteDialog
import com.simplemobiletools.notes.dialogs.OpenNoteDialog
import com.simplemobiletools.notes.dialogs.RenameNoteDialog
import com.simplemobiletools.notes.dialogs.WidgetNoteDialog
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.extensions.dpToPx
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_note.*
class MainActivity : SimpleActivity() {
private var mCurrentNote: Note? = null
lateinit var mCurrentNote: Note
lateinit var mAdapter: NotesPagerAdapter
lateinit var mDb: DBHelper
lateinit var mNotes: List<Note>
@ -38,26 +33,28 @@ class MainActivity : SimpleActivity() {
mDb = DBHelper.newInstance(applicationContext)
mNotes = mDb.getNotes()
updateSelectedNote(config.currentNoteId)
mCurrentNote = mNotes[0]
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes)
view_pager.apply {
adapter = mAdapter
}
notes_fab.setOnClickListener { displayNewNoteDialog() }
notes_fab.viewTreeObserver.addOnGlobalLayoutListener {
val heightDiff = notes_coordinator.rootView.height - notes_coordinator.height
notes_fab.visibility = if (heightDiff > dpToPx(this, 200f)) View.INVISIBLE else View.VISIBLE
notes_fab.visibility = if (heightDiff > dpToPx(200f)) View.INVISIBLE else View.VISIBLE
}
}
fun dpToPx(context: Context, valueInDp: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, context.resources.displayMetrics)
override fun onResume() {
super.onResume()
invalidateOptionsMenu()
notes_view.setTextSize(TypedValue.COMPLEX_UNIT_PX, applicationContext.getTextSize())
}
override fun onPause() {
super.onPause()
saveText()
mAdapter.saveNote(mCurrentNote.id)
}
override fun onDestroy() {
@ -121,30 +118,22 @@ class MainActivity : SimpleActivity() {
}
private fun displayRenameDialog() {
RenameNoteDialog(this, mDb, mCurrentNote!!) {
RenameNoteDialog(this, mDb, mCurrentNote) {
mCurrentNote = it
current_note_title.text = it.title
}
}
private fun updateSelectedNote(id: Int) {
saveText()
mCurrentNote = mDb.getNote(id)
mNotes = mDb.getNotes()
if (mCurrentNote != null) {
config.currentNoteId = id
notes_view.setText(mCurrentNote!!.value)
current_note_title.text = mCurrentNote!!.title
}
current_note_label.visibility = if (mNotes.size <= 1) View.GONE else View.VISIBLE
config.currentNoteId = id
notes_view.setText(mCurrentNote.value)
current_note_title.text = mCurrentNote.title
current_note_title.visibility = if (mNotes.size <= 1) View.GONE else View.VISIBLE
updateWidget(applicationContext)
}
fun displayNewNoteDialog() {
NewNoteDialog(this, mDb) {
saveText()
val newNote = Note(0, it, "", TYPE_NOTE)
val id = mDb.insertNote(newNote)
updateSelectedNote(id)
@ -154,7 +143,7 @@ class MainActivity : SimpleActivity() {
}
private fun displayDeleteNotePrompt() {
val message = String.format(getString(R.string.delete_note_prompt_message), mCurrentNote!!.title)
val message = String.format(getString(R.string.delete_note_prompt_message), mCurrentNote.title)
ConfirmationDialog(this, message) {
deleteNote()
}
@ -164,7 +153,7 @@ class MainActivity : SimpleActivity() {
if (mNotes.size <= 1)
return
mDb.deleteNote(mCurrentNote!!.id)
mDb.deleteNote(mCurrentNote.id)
mNotes = mDb.getNotes()
val firstNoteId = mNotes[0].id
@ -179,22 +168,6 @@ class MainActivity : SimpleActivity() {
}
}
private fun saveText() {
if (mCurrentNote == null)
return
val newText = notes_view.value
val oldText = mCurrentNote!!.value
if (newText != oldText) {
toast(R.string.note_saved)
mCurrentNote!!.value = newText
mDb.updateNote(mCurrentNote!!)
}
hideKeyboard()
updateWidget(applicationContext)
}
private fun shareText() {
val text = notes_view.value
if (text.isEmpty()) {
@ -212,20 +185,4 @@ class MainActivity : SimpleActivity() {
startActivity(Intent.createChooser(this, shareTitle))
}
}
private fun hideKeyboard() {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(notes_view.windowToken, 0)
}
fun updateWidget(context: Context) {
val widgetManager = AppWidgetManager.getInstance(context)
val ids = widgetManager.getAppWidgetIds(ComponentName(context, MyWidgetProvider::class.java))
Intent(context, MyWidgetProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
context.sendBroadcast(this)
}
}
}

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.notes.adapters
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray
import com.simplemobiletools.notes.NOTE_ID
import com.simplemobiletools.notes.fragments.NoteFragment
import com.simplemobiletools.notes.models.Note
class NotesPagerAdapter(fm: FragmentManager, private val notes: List<Note>) : FragmentStatePagerAdapter(fm) {
lateinit var fragments: SparseArray<NoteFragment>
init {
fragments = SparseArray(10)
}
override fun getCount() = notes.size
override fun getItem(position: Int): Fragment {
val bundle = Bundle()
val id = notes[position].id
bundle.putInt(NOTE_ID, id)
if (fragments.get(position) != null)
return fragments[position]
val fragment = NoteFragment()
fragment.arguments = bundle
fragments.put(position, fragment)
return fragment
}
fun saveNote(pos: Int) {
fragments.get(pos)?.saveText()
}
}

View File

@ -0,0 +1,30 @@
package com.simplemobiletools.notes.extensions
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.TypedValue
import com.simplemobiletools.notes.*
fun Context.getTextSize() =
when (Config.newInstance(this).fontSize) {
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.small_text_size)
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.large_text_size)
FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_large_text_size)
else -> resources.getDimension(R.dimen.medium_text_size)
}
fun Context.updateWidget() {
val widgetManager = AppWidgetManager.getInstance(this)
val ids = widgetManager.getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
Intent(this, MyWidgetProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(this)
}
}
fun Context.dpToPx(valueInDp: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, resources.displayMetrics)

View File

@ -1,12 +0,0 @@
package com.simplemobiletools.notes.extensions
import android.content.Context
import com.simplemobiletools.notes.*
fun Context.getTextSize() =
when (Config.newInstance(this).fontSize) {
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.small_text_size)
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.large_text_size)
FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_large_text_size)
else -> resources.getDimension(R.dimen.medium_text_size)
}

View File

@ -0,0 +1,50 @@
package com.simplemobiletools.notes.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.filepicker.extensions.value
import com.simplemobiletools.notes.NOTE_ID
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.databases.DBHelper
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.extensions.updateWidget
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.fragment_note.view.*
class NoteFragment : Fragment() {
var noteId = 0
lateinit var view: ViewGroup
lateinit var note: Note
lateinit var mDb: DBHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
noteId = arguments.getInt(NOTE_ID)
mDb = DBHelper.newInstance(context)
note = mDb.getNote(noteId) ?: return view
view.notes_view.setText(note.value)
view.current_note_title.text = note.title
view.notes_view.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize())
return view
}
fun saveText() {
val newText = view.notes_view.value
val oldText = note.value
if (newText != oldText) {
note.value = newText
mDb.updateNote(note)
context.updateWidget()
}
}
override fun onPause() {
super.onPause()
saveText()
}
}

View File

@ -6,7 +6,11 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/fragment_note"/>
<com.simplemobiletools.notes.views.MyViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_margin"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/notes_fab"

View File

@ -5,30 +5,19 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/current_note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginTop="@dimen/activity_margin"
android:alpha=".6"
android:text="@string/current_note"/>
<TextView
android:id="@+id/current_note_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/normal_padding"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginRight="@dimen/activity_margin"
android:layout_marginTop="@dimen/activity_margin"
android:layout_toRightOf="@+id/current_note_label"
android:alpha=".6"/>
<ScrollView
android:id="@+id/notes_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/current_note_label"
android:layout_below="@+id/current_note_title"
android:fillViewport="true">
<EditText

View File

@ -6,7 +6,6 @@
<string name="share">Teilen</string>
<string name="share_via">Teilen via</string>
<string name="cannot_share_empty_text">Leerer Text kann nicht geteilt werden</string>
<string name="note_saved">Text gespeichert</string>
<string name="simple_note">Einfache Notiz</string>
<string name="new_note">Neue Notiz hinzufügen</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Notiz löschen</string>
<string name="delete_note_prompt_message">Bist du sicher, dass du Notiz \"%1$s\" löschen willst?</string>
<string name="pick_a_note">Notiz auswählen</string>
<string name="current_note">Aktuelle Notiz:</string>
<string name="change_widget_note">Wechsel Notiz des Widgets</string>
<string name="pick_a_note_for_widget">Wähle Notiz für das Widget</string>
<string name="rename">Umbennen</string>

View File

@ -6,7 +6,6 @@
<string name="share">Compartir</string>
<string name="share_via">Compartir vía</string>
<string name="cannot_share_empty_text">No se puede compartir una nota vacía</string>
<string name="note_saved">Nota guardada</string>
<string name="simple_note">Nota simple</string>
<string name="new_note">Añadir una nueva nota</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Eliminar nota</string>
<string name="delete_note_prompt_message">¿Está seguro de querer eliminar la nota \"%1$s\"?</string>
<string name="pick_a_note">Seleccione una nota</string>
<string name="current_note">Nota actual:</string>
<string name="change_widget_note">Cambiar nota del widget</string>
<string name="pick_a_note_for_widget">Seleccione una nota para el widget</string>
<string name="rename">Renombrar</string>

View File

@ -6,7 +6,6 @@
<string name="share">Condividi</string>
<string name="share_via">Condividi via</string>
<string name="cannot_share_empty_text">Impossibile condividere un testo vuoto</string>
<string name="note_saved">Testo salvato</string>
<string name="simple_note">Simple Note</string>
<string name="new_note">Add a new note</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Delete note</string>
<string name="delete_note_prompt_message">Are you sure you want to delete note \"%1$s\"?</string>
<string name="pick_a_note">Pick a note</string>
<string name="current_note">Current note:</string>
<string name="change_widget_note">Change widget\'s note</string>
<string name="pick_a_note_for_widget">Pick a note for the widget</string>
<string name="rename">Rename</string>

View File

@ -6,7 +6,6 @@
<string name="share">共有</string>
<string name="share_via">共有&#8230;</string>
<string name="cannot_share_empty_text">空のテキストは共有できません</string>
<string name="note_saved">テキストを保存しました</string>
<string name="simple_note">シンプル メモ</string>
<string name="new_note">新しいメモを追加</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">メモを削除</string>
<string name="delete_note_prompt_message">メモ \"%1$s\" を削除してもよろしいですか?</string>
<string name="pick_a_note">メモを選択</string>
<string name="current_note">現在のメモ:</string>
<string name="change_widget_note">ウィジェットのメモを変更</string>
<string name="pick_a_note_for_widget">ウィジェットのメモを選択</string>
<string name="rename">Rename</string>

View File

@ -6,7 +6,6 @@
<string name="share">Partilhar</string>
<string name="share_via">Partilhar via</string>
<string name="cannot_share_empty_text">Não pode partilhar texto em branco</string>
<string name="note_saved">Nota guardada</string>
<string name="simple_note">Nota simples</string>
<string name="new_note">Adicionar uma nota</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Apagar nota</string>
<string name="delete_note_prompt_message">Deseja mesmo apagar a nota \"%1$s\"?</string>
<string name="pick_a_note">Selecione uma nota</string>
<string name="current_note">Nota atual:</string>
<string name="change_widget_note">Alterar nota do widget</string>
<string name="pick_a_note_for_widget">Escolha uma nota para o widget</string>
<string name="rename">Renomear</string>

View File

@ -6,7 +6,6 @@
<string name="share">Dela</string>
<string name="share_via">Dela via</string>
<string name="cannot_share_empty_text">Det går inte att dela utan text</string>
<string name="note_saved">Text sparad</string>
<string name="simple_note">Simple Note</string>
<string name="new_note">Add a new note</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Delete note</string>
<string name="delete_note_prompt_message">Are you sure you want to delete note \"%1$s\"?</string>
<string name="pick_a_note">Pick a note</string>
<string name="current_note">Current note:</string>
<string name="change_widget_note">Change widget\'s note</string>
<string name="pick_a_note_for_widget">Pick a note for the widget</string>
<string name="rename">Rename</string>

View File

@ -6,7 +6,6 @@
<string name="share">Share</string>
<string name="share_via">Share via</string>
<string name="cannot_share_empty_text">Cannot share empty text</string>
<string name="note_saved">Note saved</string>
<string name="simple_note">Simple Note</string>
<string name="new_note">Add a new note</string>
<string name="ok">OK</string>
@ -18,7 +17,6 @@
<string name="delete_note_prompt_title">Delete note</string>
<string name="delete_note_prompt_message">Are you sure you want to delete note \"%1$s\"?</string>
<string name="pick_a_note">Pick a note</string>
<string name="current_note">Current note:</string>
<string name="change_widget_note">Change widget\'s note</string>
<string name="pick_a_note_for_widget">Pick a note for the widget</string>
<string name="rename">Rename</string>