mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-01-06 05:57:59 -05:00
Initial widget work
Co-authored-by: Sylvia van Os <sylvia@hackerchick.me>
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package protect.card_locker
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.Icon
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.RemoteViewsService
|
||||
import protect.card_locker.DBHelper.LoyaltyCardArchiveFilter
|
||||
import kotlin.math.max
|
||||
|
||||
class CatimaWidgetRemoteViewsFactory(private var context: Context) :
|
||||
RemoteViewsService.RemoteViewsFactory {
|
||||
|
||||
private var mDatabase: SQLiteDatabase = DBHelper(context).readableDatabase
|
||||
|
||||
override fun onCreate() {
|
||||
onDataSetChanged()
|
||||
}
|
||||
|
||||
private var mCards: ArrayList<LoyaltyCard> = ArrayList()
|
||||
|
||||
override fun onDataSetChanged() {
|
||||
val mOrder = Utils.getLoyaltyCardOrder(context);
|
||||
val mOrderDirection = Utils.getLoyaltyCardOrderDirection(context);
|
||||
|
||||
val cur = DBHelper.getLoyaltyCardCursor(
|
||||
mDatabase,
|
||||
"",
|
||||
null,
|
||||
mOrder,
|
||||
mOrderDirection,
|
||||
LoyaltyCardArchiveFilter.Unarchived
|
||||
)
|
||||
|
||||
mCards.clear()
|
||||
if (cur.moveToFirst()) {
|
||||
do {
|
||||
val item = LoyaltyCard.fromCursor(context, cur)
|
||||
mCards.add(item)
|
||||
} while (cur.moveToNext())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {}
|
||||
|
||||
override fun getCount(): Int {
|
||||
return max(1, mCards.count())
|
||||
}
|
||||
|
||||
private fun createRemoteView(item: LoyaltyCard): RemoteViews
|
||||
{
|
||||
val rv = RemoteViews(context.packageName, R.layout.catima_widget_item).apply {
|
||||
val headerColor = Utils.getHeaderColor(context, item)
|
||||
val foreground = if (Utils.needsDarkForeground(headerColor)) Color.BLACK else Color.WHITE
|
||||
setInt(R.id.item_container, "setBackgroundColor", headerColor)
|
||||
val icon = item.getImageThumbnail(context)
|
||||
// setImageViewIcon is not supported on Android 5, so force Android 5 down the text path
|
||||
if (icon != null && Build.VERSION.SDK_INT >= 23) {
|
||||
setInt(R.id.item_container, "setBackgroundColor", foreground)
|
||||
setImageViewIcon(R.id.item_image, Icon.createWithBitmap(icon))
|
||||
setViewVisibility(R.id.item_text, View.INVISIBLE)
|
||||
setViewVisibility(R.id.item_image, View.VISIBLE)
|
||||
} else {
|
||||
setImageViewBitmap(R.id.item_image, null)
|
||||
setTextViewText(R.id.item_text, item.store)
|
||||
setViewVisibility(R.id.item_text, View.VISIBLE)
|
||||
setViewVisibility(R.id.item_image, View.INVISIBLE)
|
||||
setTextColor(
|
||||
R.id.item_text,
|
||||
foreground
|
||||
)
|
||||
}
|
||||
|
||||
val fillInIntent = Intent().apply {
|
||||
putExtra(LoyaltyCardViewActivity.BUNDLE_ID, item.id)
|
||||
}
|
||||
|
||||
setOnClickFillInIntent(R.id.item_container, fillInIntent)
|
||||
}
|
||||
|
||||
return rv
|
||||
}
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews {
|
||||
if (mCards.isEmpty()) {
|
||||
return RemoteViews(context.packageName, R.layout.catima_widget_empty)
|
||||
}
|
||||
|
||||
val item = mCards[position]
|
||||
return createRemoteView(item)
|
||||
}
|
||||
|
||||
override fun getLoadingView(): RemoteViews? = null
|
||||
|
||||
override fun getViewTypeCount(): Int = 1
|
||||
|
||||
override fun getItemId(position: Int): Long = position.toLong()
|
||||
|
||||
override fun hasStableIds(): Boolean = true
|
||||
}
|
||||
|
||||
class CatimaRemoteViewsService : RemoteViewsService() {
|
||||
override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory {
|
||||
return CatimaWidgetRemoteViewsFactory(applicationContext)
|
||||
}
|
||||
}
|
||||
34
app/src/main/java/protect/card_locker/CatimaWidget.kt
Normal file
34
app/src/main/java/protect/card_locker/CatimaWidget.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package protect.card_locker
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViews
|
||||
|
||||
class CatimaWidget : AppWidgetProvider() {
|
||||
override fun onUpdate(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetIds: IntArray
|
||||
) {
|
||||
for (appWidgetId in appWidgetIds) {
|
||||
val views = RemoteViews(context.packageName, R.layout.catima_widget)
|
||||
val intent = Intent(context, CatimaRemoteViewsService::class.java)
|
||||
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
views.setRemoteAdapter(R.id.grid_view, intent)
|
||||
|
||||
val templateIntent = Intent(context, LoyaltyCardViewActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
0,
|
||||
templateIntent,
|
||||
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
views.setPendingIntentTemplate(R.id.grid_view, pendingIntent)
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package protect.card_locker;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.SearchManager;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
@@ -330,22 +332,8 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
|
||||
selectedTab = activeTabPref.getInt(getString(R.string.sharedpreference_active_tab), 0);
|
||||
|
||||
// Restore sort preferences from Shared Preferences
|
||||
// If one of the sorting prefererences has never been set or is set to an invalid value,
|
||||
// stick to the defaults.
|
||||
SharedPreferences sortPref = getApplicationContext().getSharedPreferences(
|
||||
getString(R.string.sharedpreference_sort),
|
||||
Context.MODE_PRIVATE);
|
||||
|
||||
String orderString = sortPref.getString(getString(R.string.sharedpreference_sort_order), null);
|
||||
String orderDirectionString = sortPref.getString(getString(R.string.sharedpreference_sort_direction), null);
|
||||
|
||||
if (orderString != null && orderDirectionString != null) {
|
||||
try {
|
||||
mOrder = DBHelper.LoyaltyCardOrder.valueOf(orderString);
|
||||
mOrderDirection = DBHelper.LoyaltyCardOrderDirection.valueOf(orderDirectionString);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
mOrder = Utils.getLoyaltyCardOrder(this);
|
||||
mOrderDirection = Utils.getLoyaltyCardOrderDirection(this);
|
||||
|
||||
mGroup = null;
|
||||
|
||||
@@ -442,6 +430,14 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
|
||||
if (mCurrentActionMode != null) {
|
||||
mCurrentActionMode.finish();
|
||||
}
|
||||
|
||||
updateWidget(mAdapter.mContext);
|
||||
}
|
||||
|
||||
private void updateWidget(Context context) {
|
||||
AppWidgetManager manager = AppWidgetManager.getInstance(context);
|
||||
int[] ids = manager.getAppWidgetIds(new ComponentName(context, CatimaWidget.class));
|
||||
manager.notifyAppWidgetViewDataChanged(ids, R.id.grid_view);
|
||||
}
|
||||
|
||||
private void processParseResultList(List<ParseResult> parseResultList, String group, boolean closeAppOnNoBarcode) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
@@ -1176,4 +1177,40 @@ public class Utils {
|
||||
|
||||
return ImageView.ScaleType.FIT_CENTER;
|
||||
}
|
||||
|
||||
public static DBHelper.LoyaltyCardOrder getLoyaltyCardOrder(Context context) {
|
||||
SharedPreferences sortPref = context.getSharedPreferences(
|
||||
"sharedpreference_sort",
|
||||
Context.MODE_PRIVATE
|
||||
);
|
||||
|
||||
String orderString = sortPref.getString("sharedpreference_sort_order", null);
|
||||
|
||||
if (orderString != null) {
|
||||
try {
|
||||
return DBHelper.LoyaltyCardOrder.valueOf(orderString);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return DBHelper.LoyaltyCardOrder.Alpha;
|
||||
}
|
||||
|
||||
public static DBHelper.LoyaltyCardOrderDirection getLoyaltyCardOrderDirection(Context context) {
|
||||
SharedPreferences sortPref = context.getSharedPreferences(
|
||||
"sharedpreference_sort",
|
||||
Context.MODE_PRIVATE
|
||||
);
|
||||
|
||||
String orderDirectionString = sortPref.getString("sharedpreference_sort_direction", null);
|
||||
|
||||
if (orderDirectionString != null) {
|
||||
try {
|
||||
return DBHelper.LoyaltyCardOrderDirection.valueOf(orderDirectionString);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return DBHelper.LoyaltyCardOrderDirection.Ascending;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user