mirror of https://github.com/tasks/tasks
Add "Add widget to home screen" to list settings
parent
1d74527bc4
commit
f4863b813a
@ -0,0 +1,69 @@
|
||||
package org.tasks.compose.settings
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Home
|
||||
import androidx.compose.material.icons.outlined.Widgets
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalInspectionMode
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo
|
||||
import org.tasks.R
|
||||
import org.tasks.compose.Constants
|
||||
import org.tasks.kmp.org.tasks.compose.settings.SettingRow
|
||||
import org.tasks.themes.TasksTheme
|
||||
|
||||
@Composable
|
||||
fun AddWidgetToHomeRow(onClick: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val isRequestPinAppWidgetSupported = LocalInspectionMode.current || remember {
|
||||
atLeastOreo() &&
|
||||
context.getSystemService(AppWidgetManager::class.java).isRequestPinAppWidgetSupported
|
||||
}
|
||||
if (isRequestPinAppWidgetSupported) {
|
||||
SettingRow(
|
||||
modifier = Modifier.clickable(onClick = onClick),
|
||||
left = {
|
||||
Box(
|
||||
modifier = Modifier.size(48.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Widgets,
|
||||
contentDescription = null,
|
||||
tint = colorResource(R.color.icon_tint_with_alpha),
|
||||
)
|
||||
}
|
||||
},
|
||||
center = {
|
||||
Text(
|
||||
text = stringResource(R.string.add_widget_to_home_screen),
|
||||
modifier = Modifier.padding(start = Constants.KEYLINE_FIRST)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview(showBackground = true)
|
||||
private fun AddToHomePreview() {
|
||||
TasksTheme {
|
||||
AddShortcutToHomeRow(onClick = {})
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package org.tasks.widget
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.tasks.preferences.Preferences
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class RequestPinWidgetReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var preferences: Preferences
|
||||
@Inject lateinit var appWidgetManager: AppWidgetManager
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val widgetId = intent.getIntExtra(android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
|
||||
if (widgetId == -1) {
|
||||
Timber.e("Missing widgetId")
|
||||
return
|
||||
}
|
||||
val filter = intent.getStringExtra(EXTRA_FILTER)
|
||||
val color = intent.getIntExtra(EXTRA_COLOR, 0)
|
||||
val widgetPreferences = WidgetPreferences(context, preferences, widgetId)
|
||||
widgetPreferences.setFilter(filter)
|
||||
widgetPreferences.setColor(color)
|
||||
appWidgetManager.reconfigureWidgets()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val ACTION_CONFIGURE_WIDGET = "org.tasks.CONFIGURE_WIDGET"
|
||||
const val EXTRA_FILTER = "extra_filter"
|
||||
const val EXTRA_COLOR = "extra_color"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue