Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: redesigned home screen widget for android #417

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@


<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
</application>
</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package com.ccextractor.taskwarriorflutter

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity: FlutterActivity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package com.ccextractor.taskwarriorflutter
import android.annotation.TargetApi
import android.appwidget.AppWidgetManager
import android.content.Context
import android.net.Uri
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
import es.antonborri.home_widget.HomeWidgetLaunchIntent
import es.antonborri.home_widget.HomeWidgetProvider
import es.antonborri.home_widget.HomeWidgetPlugin
import org.json.JSONException
import android.content.Intent
import android.widget.RemoteViewsService
import org.json.JSONObject
import org.json.JSONArray as OrgJSONArray
import android.os.Bundle
import android.app.PendingIntent
import android.appwidget.AppWidgetProvider
import android.os.Build


@TargetApi(Build.VERSION_CODES.CUPCAKE)
class TaskWarriorWidgetProvider : AppWidgetProvider() {

override fun onReceive(context: Context, intent: Intent) {
// val myaction = intent.action
if (intent.action == "TASK_ACTION") {
val extras = intent.extras
if(extras!=null){
val uuid = extras.getString("uuid")?:""
val add_task = extras.getString("launchedFor")
val host = if(add_task == "ADD_TASK"){context.getString(R.string.app_widget_add_clicked_uri)}else{context.getString(R.string.app_widget_card_clicked_uri)}
val launchIntent = Intent(context, MainActivity::class.java).apply {
action = context.getString(R.string.app_widget_launch_action)
data = Uri.parse("$host?uuid=$uuid")
flags = Intent. FLAG_ACTIVITY_NEW_TASK
context?.startActivity(this)
}
// HomeWidgetLaunchIntent.getActivity(context, MainActivity::class.java, Uri.parse("TaskWarrior://taskView?taskId=$uuid"))
}
}
super.onReceive(context, intent)
}
fun getLayoutId(context: Context) : Int{
val sharedPrefs = HomeWidgetPlugin.getData(context)
val theme = sharedPrefs.getString("themeMode", "")
val layoutId = if (theme.equals("dark")) {
R.layout.taskwarrior_layout_dark // Define a dark mode layout in your resources
} else {
R.layout.taskwarrior_layout
}
return layoutId
}
@TargetApi(Build.VERSION_CODES.DONUT)
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
appWidgetIds.forEach { widgetId ->
val sharedPrefs = HomeWidgetPlugin.getData(context)
val tasks = sharedPrefs.getString("tasks", "")
val intent = Intent(context, ListViewRemoteViewsService::class.java).apply {
putExtra("tasksJsonString", tasks)
data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
}
val views = RemoteViews(context.packageName, getLayoutId(context)).apply {
val pendingIntent: PendingIntent = HomeWidgetLaunchIntent.getActivity(
context,
MainActivity::class.java
)
setOnClickPendingIntent(R.id.logo, pendingIntent)
val intent_for_add = Intent(context, TaskWarriorWidgetProvider::class.java).apply {
setAction("TASK_ACTION")
data=Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
putExtra("launchedFor", "ADD_TASK")
}
val pendingIntentAdd: PendingIntent = PendingIntent.getBroadcast(
context,
0, // requestCode, can be any unique integer
intent_for_add,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT or Intent.FILL_IN_COMPONENT // Use appropriate flags
)
setOnClickPendingIntent(R.id.add_btn, pendingIntentAdd)
setRemoteAdapter(R.id.list_view, intent)
}

val clickPendingIntent: PendingIntent = Intent(
context,
TaskWarriorWidgetProvider::class.java
).run {
setAction("TASK_ACTION")
setIdentifier("uuid")
data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))

PendingIntent.getBroadcast(
context,
0,
this,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT or Intent.FILL_IN_COMPONENT
)
}

views.setPendingIntentTemplate(R.id.list_view, clickPendingIntent)
appWidgetManager.updateAppWidget(widgetId, views)
}
super.onUpdate(context, appWidgetManager, appWidgetIds)
}
}
class ListViewRemoteViewsFactory(
private val context: Context,
private val tasksJsonString: String?
) : RemoteViewsService.RemoteViewsFactory {

private val tasks = mutableListOf<Task>()

override fun onCreate() {}

override fun onDataSetChanged() {
if (tasksJsonString != null) {
try {
val jsonArray = OrgJSONArray(tasksJsonString as String)
for (i in 0 until jsonArray.length()) {
tasks.add(Task.fromJson(jsonArray.getJSONObject(i)))
}
} catch (e: JSONException) {
e.printStackTrace()
}
}
}

override fun onDestroy() {}

override fun getCount(): Int = tasks.size

fun getListItemLayoutId(): Int{
val sharedPrefs = HomeWidgetPlugin.getData(context)
val theme = sharedPrefs.getString("themeMode", "")
val layoutId = if (theme.equals("dark")) {
R.layout.listitem_layout_dark // Define a dark mode layout in your resources
} else {
R.layout.listitem_layout
}
return layoutId
}
fun getListItemLayoutIdForR1(): Int{
val sharedPrefs = HomeWidgetPlugin.getData(context)
val theme = sharedPrefs.getString("themeMode", "")
val layoutId = if (theme.equals("dark")) {
R.layout.no_tasks_found_li_dark // Define a dark mode layout in your resources
} else {
R.layout.no_tasks_found_li
}
return layoutId
}
fun getDotIdByPriority(p: String) : Int{
println("PRIORITY: "+p)
if(p.equals("L")) return R.drawable.low_priority_dot
if(p.equals("M")) return R.drawable.mid_priority_dot
if(p.equals("H")) return R.drawable.high_priority_dot
return R.drawable.no_priority_dot
}

override fun getViewAt(position: Int): RemoteViews {
val task = tasks[position]
if(task.uuid.equals("NO_TASK"))
return RemoteViews(context.packageName, getListItemLayoutIdForR1()).apply {
if(task.priority.equals("1"))
setTextViewText(R.id.tv, "No tasks added yet")
if(task.priority.equals("2"))
setTextViewText(R.id.tv, "Filters applied are hiding all tasks")
}
return RemoteViews(context.packageName, getListItemLayoutId()).apply {
setTextViewText(R.id.todo__title, task.title)
setImageViewResource(R.id.dot, getDotIdByPriority(task.priority))
val a = Intent().apply {

Bundle().also { extras ->
extras.putString("action", "show_task")
extras.putString("uuid", tasks[position].uuid)
putExtras(extras)
}

}
setOnClickFillInIntent(R.id.list_item_container,a)
}

}
override fun getLoadingView(): RemoteViews? = null

override fun getViewTypeCount(): Int = 1

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

override fun hasStableIds(): Boolean = true
}
class ListViewRemoteViewsService : RemoteViewsService() {

override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
val tasksJsonString = intent.getStringExtra("tasksJsonString")
return ListViewRemoteViewsFactory(applicationContext, tasksJsonString)
}
}
data class Task(val title: String, val urgencyLevel: String,val uuid:String, val priority: String) {
companion object {
fun fromJson(json: JSONObject): Task {
val title = json.optString("description", "")
val urgencyLevel = json.optString("urgency", "")
val uuid = json.optString("uuid","")
val priority = json.optString("priority", "")
return Task(title, urgencyLevel, uuid, priority)
}
}
}

This file was deleted.

Loading
Loading