Skip to content

Commit

Permalink
[#285] [NF] Screen Widgets. View preset
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskouski committed Dec 3, 2024
1 parent 3e68628 commit 426c765
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 28 deletions.
8 changes: 8 additions & 0 deletions android/app/src/main/java/com/tercad/fingrom/PaymentItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tercad.fingrom

data class PaymentItem(
val type: String,
val amount: String,
val interval: String,
val color: String // Hex color string
)
29 changes: 13 additions & 16 deletions android/app/src/main/java/com/tercad/fingrom/PaymentsAppWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.tercad.fingrom
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import android.widget.AdapterView

/**
* Implementation of App Widget functionality.
Expand All @@ -14,9 +16,18 @@ class PaymentsAppWidget : AppWidgetProvider() {
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
val intent = Intent(context, PaymentsWidgetService::class.java)
val remoteViews = RemoteViews(context.packageName, R.layout.payments_app_widget)

// Set the adapter for the list view
remoteViews.setRemoteAdapter(R.id.list_view, intent)

// Set the empty view if no data is available
remoteViews.setEmptyView(R.id.list_view, R.id.empty_view)

// Update the widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
}
}

Expand All @@ -28,17 +39,3 @@ class PaymentsAppWidget : AppWidgetProvider() {
// Enter relevant functionality for when the last widget is disabled
}
}

internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetText = context.getString(R.string.appwidget_text)
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.payments_app_widget)
views.setTextViewText(R.id.appwidget_text, widgetText)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tercad.fingrom

import android.content.Context
import android.widget.RemoteViews
import android.widget.RemoteViewsService
import androidx.core.graphics.ColorUtils
import android.graphics.Color

class PaymentsWidgetFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {

private val items = mutableListOf<PaymentItem>()

override fun onCreate() {
// Initial data, including color in hex format
items.add(PaymentItem("Bill:", "-$100.00", "monthly", "#FFD700")) // Yellow for Bill
items.add(PaymentItem("Invoice:", "+$50.00", "weekly", "#FF0000")) // Red for Invoice
items.add(PaymentItem("Bill:", "-$20.00", "monthly", "#0000FF")) // Blue for Bill
}

override fun onDataSetChanged() {
// Refresh or update data if needed
}

override fun onDestroy() {
items.clear()
}

override fun getCount(): Int = items.size

override fun getViewAt(position: Int): RemoteViews {
val item = items[position]

val rv = RemoteViews(context.packageName, R.layout.payment_list_item)
rv.setTextViewText(R.id.item_type, item.type)
rv.setTextViewText(R.id.item_amount, item.amount)
rv.setTextViewText(R.id.item_interval, item.interval)

// Set background color directly using Color.parseColor
rv.setInt(R.id.color_bar, "setBackgroundColor", Color.parseColor(item.color))

return rv
}

override fun getLoadingView(): RemoteViews? = null

override fun getViewTypeCount(): Int = 1

override fun getItemId(position: Int): Long = position.toLong()

override fun hasStableIds(): Boolean = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tercad.fingrom

import android.content.Intent
import android.widget.RemoteViewsService

class PaymentsWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return PaymentsWidgetFactory(applicationContext)
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions android/app/src/main/res/layout/payment_list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Color bar for each item -->
<View
android:id="@+id/color_bar"
android:layout_width="match_parent"
android:layout_height="10dp" />

<!-- Title of the payment item (Bill/Invoice) -->
<TextView
android:id="@+id/item_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bill:"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
android:layout_below="@id/color_bar" />

<!-- Amount for the payment item -->
<TextView
android:id="@+id/item_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-$100.00"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
android:layout_below="@id/item_type" />

<!-- Interval for the payment item (monthly/weekly) -->
<TextView
android:id="@+id/item_interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monthly"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
android:layout_below="@id/item_amount" />

</RelativeLayout>
21 changes: 12 additions & 9 deletions android/app/src/main/res/layout/payments_app_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
android:layout_height="match_parent"
android:theme="@style/Theme.Android.AppWidgetContainer">

<!-- ListView to show payment items dynamically -->
<ListView
android:id="@+id/list_view"
android:layout_width="0dp"
android:layout_height="0dp" />

<!-- Empty view when there are no items to display -->
<TextView
android:id="@+id/appwidget_text"
style="@style/Widget.Android.AppWidget.InnerView"
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:textSize="24sp"
android:textStyle="bold|italic" />
android:text="No items to display"
android:visibility="gone"
android:textColor="@android:color/black" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<resources>
<string name="appwidget_text">EXAMPLE</string>
<string name="add_widget">Add widget</string>
<string name="app_widget_description">This is an app widget description</string>
<string name="app_widget_description">Add Payments List</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:initialLayout="@layout/payments_app_widget"
android:minWidth="180dp"
android:minHeight="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:previewImage="@drawable/payments_appwidget_preview"
android:previewLayout="@layout/payments_app_widget"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="3"
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/xml/payments_app_widget_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:initialLayout="@layout/payments_app_widget"
android:minWidth="180dp"
android:minHeight="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:previewImage="@drawable/payments_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="360000"
android:widgetCategory="home_screen" />

0 comments on commit 426c765

Please sign in to comment.