mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	properly update the gravity of widgets too
This commit is contained in:
		| @@ -7,6 +7,7 @@ import android.content.Context | |||||||
| import android.content.Intent | import android.content.Intent | ||||||
| import android.content.SharedPreferences | import android.content.SharedPreferences | ||||||
| import android.graphics.Color | import android.graphics.Color | ||||||
|  | import android.view.View | ||||||
| import android.widget.RemoteViews | import android.widget.RemoteViews | ||||||
| import com.simplemobiletools.notes.R.layout.widget | import com.simplemobiletools.notes.R.layout.widget | ||||||
| import com.simplemobiletools.notes.activities.MainActivity | import com.simplemobiletools.notes.activities.MainActivity | ||||||
| @@ -15,6 +16,7 @@ import com.simplemobiletools.notes.extensions.getTextSize | |||||||
|  |  | ||||||
| class MyWidgetProvider : AppWidgetProvider() { | class MyWidgetProvider : AppWidgetProvider() { | ||||||
|     lateinit var mDb: DBHelper |     lateinit var mDb: DBHelper | ||||||
|  |     var textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right) | ||||||
|  |  | ||||||
|     companion object { |     companion object { | ||||||
|         lateinit var mPrefs: SharedPreferences |         lateinit var mPrefs: SharedPreferences | ||||||
| @@ -26,18 +28,32 @@ class MyWidgetProvider : AppWidgetProvider() { | |||||||
|         val defaultColor = context.resources.getColor(R.color.dark_grey) |         val defaultColor = context.resources.getColor(R.color.dark_grey) | ||||||
|         val newBgColor = mPrefs.getInt(WIDGET_BG_COLOR, defaultColor) |         val newBgColor = mPrefs.getInt(WIDGET_BG_COLOR, defaultColor) | ||||||
|         val newTextColor = mPrefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE) |         val newTextColor = mPrefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE) | ||||||
|         mRemoteViews.apply { |  | ||||||
|             setInt(R.id.notes_view, "setBackgroundColor", newBgColor) |         for (id in textIds) { | ||||||
|             setInt(R.id.notes_view, "setTextColor", newTextColor) |             mRemoteViews.apply { | ||||||
|             setFloat(R.id.notes_view, "setTextSize", context.getTextSize() / context.resources.displayMetrics.density) |                 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) { |         for (widgetId in appWidgetIds) { | ||||||
|             updateWidget(appWidgetManager, widgetId, mRemoteViews) |             updateWidget(appWidgetManager, widgetId, mRemoteViews) | ||||||
|         } |         } | ||||||
|         super.onUpdate(context, appWidgetManager, appWidgetIds) |         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) { |     private fun initVariables(context: Context) { | ||||||
|         mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) |         mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) | ||||||
|         mDb = DBHelper.newInstance(context) |         mDb = DBHelper.newInstance(context) | ||||||
| @@ -54,7 +70,8 @@ class MyWidgetProvider : AppWidgetProvider() { | |||||||
|     private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews) { |     private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews) { | ||||||
|         val widgetNoteId = mPrefs.getInt(WIDGET_NOTE_ID, 1) |         val widgetNoteId = mPrefs.getInt(WIDGET_NOTE_ID, 1) | ||||||
|         val note = mDb.getNote(widgetNoteId) |         val note = mDb.getNote(widgetNoteId) | ||||||
|         remoteViews.setTextViewText(R.id.notes_view, note?.value ?: "") |         for (id in textIds) | ||||||
|  |             remoteViews.setTextViewText(id, note?.value ?: "") | ||||||
|         widgetManager.updateAppWidget(widgetId, remoteViews) |         widgetManager.updateAppWidget(widgetId, remoteViews) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -72,6 +72,7 @@ class SettingsActivity : SimpleActivity() { | |||||||
|         settings_gravity.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { |         settings_gravity.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | ||||||
|             override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { |             override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { | ||||||
|                 config.gravity = settings_gravity.selectedItemPosition |                 config.gravity = settings_gravity.selectedItemPosition | ||||||
|  |                 updateWidget() | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             override fun onNothingSelected(parent: AdapterView<*>?) { |             override fun onNothingSelected(parent: AdapterView<*>?) { | ||||||
|   | |||||||
| @@ -6,11 +6,30 @@ | |||||||
|     android:layout_height="match_parent"> |     android:layout_height="match_parent"> | ||||||
|  |  | ||||||
|     <TextView |     <TextView | ||||||
|         android:id="@+id/notes_view" |         android:id="@+id/notes_view_left" | ||||||
|         android:layout_width="match_parent" |         android:layout_width="match_parent" | ||||||
|         android:layout_height="match_parent" |         android:layout_height="match_parent" | ||||||
|         android:background="@null" |         android:background="@null" | ||||||
|         android:gravity="start" |         android:gravity="left" | ||||||
|         android:padding="@dimen/activity_margin"/> |         android:padding="@dimen/activity_margin" | ||||||
|  |         android:visibility="gone"/> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/notes_view_center" | ||||||
|  |         android:layout_width="match_parent" | ||||||
|  |         android:layout_height="match_parent" | ||||||
|  |         android:background="@null" | ||||||
|  |         android:gravity="center_horizontal" | ||||||
|  |         android:padding="@dimen/activity_margin" | ||||||
|  |         android:visibility="gone"/> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/notes_view_right" | ||||||
|  |         android:layout_width="match_parent" | ||||||
|  |         android:layout_height="match_parent" | ||||||
|  |         android:background="@null" | ||||||
|  |         android:gravity="right" | ||||||
|  |         android:padding="@dimen/activity_margin" | ||||||
|  |         android:visibility="gone"/> | ||||||
|  |  | ||||||
| </RelativeLayout> | </RelativeLayout> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user