@@ -60,58 +60,74 @@ class TaskWarriorWidgetProvider : AppWidgetProvider() {
6060 }
6161 return layoutId
6262 }
63- @TargetApi(Build .VERSION_CODES .DONUT )
64- override fun onUpdate (context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ) {
65- appWidgetIds.forEach { widgetId ->
66- val sharedPrefs = HomeWidgetPlugin .getData(context)
67- val tasks = sharedPrefs.getString(" tasks" , " " )
68- val intent = Intent (context, ListViewRemoteViewsService ::class .java).apply {
69- putExtra(" tasksJsonString" , tasks)
70- data = Uri .parse(toUri(Intent .URI_INTENT_SCHEME ))
71- }
72- val views = RemoteViews (context.packageName, getLayoutId(context)).apply {
73- val pendingIntent: PendingIntent = HomeWidgetLaunchIntent .getActivity(
74- context,
75- MainActivity ::class .java
76- )
77- setOnClickPendingIntent(R .id.logo, pendingIntent)
78- val intent_for_add = Intent (context, TaskWarriorWidgetProvider ::class .java).apply {
79- setAction(" TASK_ACTION" )
80- data= Uri .parse(toUri(Intent .URI_INTENT_SCHEME ))
81- putExtra(" launchedFor" , " ADD_TASK" )
82- }
83- val pendingIntentAdd: PendingIntent = PendingIntent .getBroadcast(
84- context,
85- 0 , // requestCode, can be any unique integer
86- intent_for_add,
87- PendingIntent .FLAG_MUTABLE or PendingIntent .FLAG_UPDATE_CURRENT or Intent .FILL_IN_COMPONENT // Use appropriate flags
88- )
89- setOnClickPendingIntent(R .id.add_btn, pendingIntentAdd)
90- setRemoteAdapter(R .id.list_view, intent)
91- }
63+ @TargetApi(Build .VERSION_CODES .DONUT )
64+ override fun onUpdate (context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ) {
65+ appWidgetIds.forEach { widgetId ->
66+ // 1. Get the latest data from HomeWidget/SharedPrefs
67+ val sharedPrefs = HomeWidgetPlugin .getData(context)
68+ val tasks = sharedPrefs.getString(" tasks" , " " )
69+
70+ // 2. Create the Intent for the ListView service
71+ // We add the widgetId to the data URI to make it unique, preventing caching issues
72+ val intent = Intent (context, ListViewRemoteViewsService ::class .java).apply {
73+ putExtra(" tasksJsonString" , tasks)
74+ data = Uri .parse(toUri(Intent .URI_INTENT_SCHEME ) + widgetId)
75+ }
9276
93- val clickPendingIntent: PendingIntent = Intent (
94- context,
95- TaskWarriorWidgetProvider ::class .java
96- ).run {
97- setAction(" TASK_ACTION" )
98- setIdentifier(" uuid" )
99- data = Uri .parse(toUri(Intent .URI_INTENT_SCHEME ))
100-
101- PendingIntent .getBroadcast(
102- context,
103- 0 ,
104- this ,
105- PendingIntent .FLAG_MUTABLE or PendingIntent .FLAG_UPDATE_CURRENT or Intent .FILL_IN_COMPONENT
106- )
107- }
77+ // 3. Initialize RemoteViews with the THEMED layout (getLayoutId handles dark/light logic)
78+ val views = RemoteViews (context.packageName, getLayoutId(context)).apply {
79+
80+ // Set up the Logo click (Open App)
81+ val pendingIntent: PendingIntent = HomeWidgetLaunchIntent .getActivity(
82+ context,
83+ MainActivity ::class .java
84+ )
85+ setOnClickPendingIntent(R .id.logo, pendingIntent)
86+
87+ // Set up the Add Button click (Custom Action)
88+ val intent_for_add = Intent (context, TaskWarriorWidgetProvider ::class .java).apply {
89+ action = " TASK_ACTION"
90+ putExtra(" launchedFor" , " ADD_TASK" )
91+ // Unique data to ensure the broadcast is fresh
92+ data = Uri .parse(" taskwarrior://addtask/$widgetId " )
93+ }
94+
95+ val pendingIntentAdd: PendingIntent = PendingIntent .getBroadcast(
96+ context,
97+ widgetId,
98+ intent_for_add,
99+ PendingIntent .FLAG_MUTABLE or PendingIntent .FLAG_UPDATE_CURRENT
100+ )
101+ setOnClickPendingIntent(R .id.add_btn, pendingIntentAdd)
102+
103+ // Attach the adapter to the ListView
104+ setRemoteAdapter(R .id.list_view, intent)
105+ }
108106
109- views.setPendingIntentTemplate(R .id.list_view, clickPendingIntent)
110- appWidgetManager.updateAppWidget(widgetId, views)
111- }
112- super .onUpdate(context, appWidgetManager, appWidgetIds)
107+ // 4. Set up the Click Template for List Items (Deep Linking)
108+ val clickPendingIntent: PendingIntent = Intent (
109+ context,
110+ TaskWarriorWidgetProvider ::class .java
111+ ).run {
112+ action = " TASK_ACTION"
113+ // Important: Use widgetId as requestCode to keep it unique
114+ PendingIntent .getBroadcast(
115+ context,
116+ widgetId,
117+ this ,
118+ PendingIntent .FLAG_MUTABLE or PendingIntent .FLAG_UPDATE_CURRENT
119+ )
113120 }
121+ views.setPendingIntentTemplate(R .id.list_view, clickPendingIntent)
122+
123+ // 5. THE THEME FIX: Notify the manager that the list data/layout needs a refresh
124+ appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R .id.list_view)
125+
126+ // 6. Push the update to the widget
127+ appWidgetManager.updateAppWidget(widgetId, views)
114128 }
129+ super .onUpdate(context, appWidgetManager, appWidgetIds)
130+ } }
115131class ListViewRemoteViewsFactory (
116132 private val context : Context ,
117133 private val tasksJsonString : String?
@@ -121,18 +137,22 @@ class ListViewRemoteViewsFactory(
121137
122138 override fun onCreate () {}
123139
124- override fun onDataSetChanged () {
125- if (tasksJsonString != null ) {
126- try {
127- val jsonArray = OrgJSONArray (tasksJsonString as String )
128- for (i in 0 until jsonArray.length()) {
129- tasks.add(Task .fromJson(jsonArray.getJSONObject(i)))
130- }
131- } catch (e: JSONException ) {
132- e.printStackTrace()
133- }
134- }
135- }
140+ override fun onDataSetChanged () {
141+ tasks.clear() // Add this!
142+ val sharedPrefs = HomeWidgetPlugin .getData(context)
143+ val latestTasksJson = sharedPrefs.getString(" tasks" , " " )
144+
145+ if (! latestTasksJson.isNullOrEmpty()) {
146+ try {
147+ val jsonArray = OrgJSONArray (latestTasksJson)
148+ for (i in 0 until jsonArray.length()) {
149+ tasks.add(Task .fromJson(jsonArray.getJSONObject(i)))
150+ }
151+ } catch (e: JSONException ) {
152+ e.printStackTrace()
153+ }
154+ }
155+ }
136156
137157 override fun onDestroy () {}
138158
@@ -193,7 +213,7 @@ class ListViewRemoteViewsFactory(
193213 }
194214 override fun getLoadingView (): RemoteViews ? = null
195215
196- override fun getViewTypeCount (): Int = 1
216+ override fun getViewTypeCount (): Int = 2
197217
198218 override fun getItemId (position : Int ): Long = position.toLong()
199219
0 commit comments