mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
moving a couple files in Helpers folder
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
package com.simplemobiletools.notes.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||
import com.simplemobiletools.notes.*
|
||||
|
||||
class Config(context: Context) : BaseConfig(context) {
|
||||
companion object {
|
||||
fun newInstance(context: Context) = Config(context)
|
||||
}
|
||||
|
||||
var fontSize: Int
|
||||
get() = prefs.getInt(FONT_SIZE, FONT_SIZE_MEDIUM)
|
||||
set(size) = prefs.edit().putInt(FONT_SIZE, size).apply()
|
||||
|
||||
var gravity: Int
|
||||
get() = prefs.getInt(GRAVITY, GRAVITY_LEFT)
|
||||
set(size) = prefs.edit().putInt(GRAVITY, size).apply()
|
||||
|
||||
var currentNoteId: Int
|
||||
get() = prefs.getInt(CURRENT_NOTE_ID, 1)
|
||||
set(id) = prefs.edit().putInt(CURRENT_NOTE_ID, id).apply()
|
||||
|
||||
var widgetNoteId: Int
|
||||
get() = prefs.getInt(WIDGET_NOTE_ID, 1)
|
||||
set(id) = prefs.edit().putInt(WIDGET_NOTE_ID, id).apply()
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.simplemobiletools.notes.helpers
|
||||
|
||||
val TEXT = "text"
|
||||
val NOTE_ID = "note_id"
|
||||
|
||||
// shared preferences
|
||||
val PREFS_KEY = "Notes"
|
||||
val IS_FIRST_RUN = "is_first_run"
|
||||
val IS_DARK_THEME = "is_dark_theme"
|
||||
val CURRENT_NOTE_ID = "current_note_id"
|
||||
val WIDGET_NOTE_ID = "widget_note_id"
|
||||
val FONT_SIZE = "font_size"
|
||||
val GRAVITY = "gravity"
|
||||
val WIDGET_BG_COLOR = "widget_bg_color"
|
||||
val WIDGET_TEXT_COLOR = "widget_text_color"
|
||||
|
||||
// gravity
|
||||
val GRAVITY_LEFT = 0
|
||||
val GRAVITY_CENTER = 1
|
||||
val GRAVITY_RIGHT = 2
|
||||
|
||||
// font sizes
|
||||
val FONT_SIZE_SMALL = 0
|
||||
val FONT_SIZE_MEDIUM = 1
|
||||
val FONT_SIZE_LARGE = 2
|
||||
val FONT_SIZE_EXTRA_LARGE = 3
|
||||
|
||||
// note types
|
||||
val TYPE_NOTE = 0
|
||||
val TYPE_CHECKLIST = 1
|
@ -0,0 +1,78 @@
|
||||
package com.simplemobiletools.notes.helpers
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.Color
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.notes.R
|
||||
import com.simplemobiletools.notes.R.layout.widget
|
||||
import com.simplemobiletools.notes.activities.MainActivity
|
||||
import com.simplemobiletools.notes.databases.DBHelper
|
||||
import com.simplemobiletools.notes.extensions.getTextSize
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
lateinit var mDb: DBHelper
|
||||
var textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right)
|
||||
|
||||
companion object {
|
||||
lateinit var mPrefs: SharedPreferences
|
||||
lateinit var mRemoteViews: RemoteViews
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
initVariables(context)
|
||||
val defaultColor = Color.BLACK
|
||||
val newBgColor = mPrefs.getInt(WIDGET_BG_COLOR, defaultColor)
|
||||
val newTextColor = mPrefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE)
|
||||
|
||||
for (id in textIds) {
|
||||
mRemoteViews.apply {
|
||||
setInt(id, "setBackgroundColor", newBgColor)
|
||||
setInt(id, "setTextColor", newTextColor)
|
||||
setFloat(id, "setTextSize", context.getTextSize() / context.resources.displayMetrics.density)
|
||||
setViewVisibility(id, View.GONE)
|
||||
}
|
||||
}
|
||||
|
||||
mRemoteViews.setViewVisibility(getProperTextView(context), View.VISIBLE)
|
||||
|
||||
for (widgetId in appWidgetIds) {
|
||||
updateWidget(appWidgetManager, widgetId, mRemoteViews)
|
||||
}
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
}
|
||||
|
||||
private fun getProperTextView(context: Context): Int {
|
||||
return when (Config.newInstance(context).gravity) {
|
||||
GRAVITY_CENTER -> R.id.notes_view_center
|
||||
GRAVITY_RIGHT -> R.id.notes_view_right
|
||||
else -> R.id.notes_view_left
|
||||
}
|
||||
}
|
||||
|
||||
private fun initVariables(context: Context) {
|
||||
mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
|
||||
mDb = DBHelper.newInstance(context)
|
||||
mRemoteViews = RemoteViews(context.packageName, widget)
|
||||
setupAppOpenIntent(R.id.notes_holder, context)
|
||||
}
|
||||
|
||||
private fun setupAppOpenIntent(id: Int, context: Context) {
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews) {
|
||||
val widgetNoteId = mPrefs.getInt(WIDGET_NOTE_ID, 1)
|
||||
val note = mDb.getNote(widgetNoteId)
|
||||
for (id in textIds)
|
||||
remoteViews.setTextViewText(id, note?.value ?: "")
|
||||
widgetManager.updateAppWidget(widgetId, remoteViews)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user