Skip to content

Commit 450842a

Browse files
committed
feat: Add "Pin Widget" option to settings
This commit introduces a new "Pin Widget" option in the settings menu. - A new menu item `settings_widget_icon` has been added to `settings_menu.xml`. - A corresponding vector drawable `ic_widgets.xml` has been created for the icon. - In `SettingsFragment.kt`: - Necessary imports for `AppWidgetManager`, `ComponentName`, and `PendingIntent` were added. - `handleOnClickWidgetOption()` function was implemented to handle the widget pinning request using `AppWidgetManager.requestPinAppWidget()`. - The `setOnMenuItemClickListener` in `setupListeners()` now handles the new `R.id.settings_widget_icon` to trigger `handleOnClickWidgetOption()`. - The `Uri.parse()` call for the language settings intent was updated to use the `String.toUri()` extension function. - A new string resource `widget_icon` was added to `strings.xml`.
1 parent 0b20f1d commit 450842a

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

app/src/main/java/com/warbler/ui/settings/SettingsFragment.kt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.warbler.ui.settings
22

3+
import android.app.PendingIntent
4+
import android.appwidget.AppWidgetManager
5+
import android.content.ComponentName
36
import android.content.Intent
4-
import android.net.Uri
57
import android.os.Bundle
68
import android.provider.Settings
79
import android.view.View
@@ -12,8 +14,10 @@ import androidx.fragment.app.viewModels
1214
import androidx.navigation.findNavController
1315
import com.google.android.material.snackbar.Snackbar
1416
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.Session.Event.Log
17+
import com.warbler.MainActivity
1518
import com.warbler.R
1619
import com.warbler.databinding.FragmentSettingsBinding
20+
import com.warbler.ui.widget.WeatherWidgetProvider
1721
import com.warbler.utilities.Constants
1822
import dagger.hilt.android.AndroidEntryPoint
1923

@@ -53,14 +57,19 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
5357
private fun setupListeners() {
5458
binding.topAppBar.setOnMenuItemClickListener { menuItem ->
5559
when (menuItem.itemId) {
60+
R.id.settings_widget_icon -> {
61+
handleOnClickWidgetOption()
62+
true
63+
}
64+
5665
R.id.settings_share_icon -> {
5766
handleShareClick(Constants.WEATHER_WARBLER_URL)
5867
true
5968
}
6069

6170
R.id.settings_language_icon -> {
6271
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
63-
intent.data = Uri.parse("package:" + context?.packageName)
72+
intent.data = ("package:" + context?.packageName).toUri()
6473
startActivity(intent)
6574
true
6675
}
@@ -191,6 +200,32 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
191200
}
192201
}
193202

203+
private fun handleOnClickWidgetOption() {
204+
val appWidgetManager = AppWidgetManager.getInstance(context)
205+
val myProvider = ComponentName(requireContext(), WeatherWidgetProvider::class.java)
206+
val intent = Intent(context, MainActivity::class.java)
207+
208+
if (appWidgetManager.isRequestPinAppWidgetSupported) {
209+
// Create the PendingIntent object only if your app needs to be notified
210+
// when the user chooses to pin the widget. Note that if the pinning
211+
// operation fails, your app isn't notified. This callback receives the ID
212+
// of the newly pinned widget (EXTRA_APPWIDGET_ID).
213+
val successCallback =
214+
PendingIntent.getBroadcast(
215+
// context =
216+
context,
217+
// requestCode =
218+
0,
219+
// intent =
220+
intent,
221+
// flags =
222+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
223+
)
224+
225+
appWidgetManager.requestPinAppWidget(myProvider, null, successCallback)
226+
}
227+
}
228+
194229
override fun onDestroyView() {
195230
super.onDestroyView()
196231
_binding = null
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="960"
5+
android:viewportHeight="960"
6+
android:tint="@color/colorPrimary">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M638,492L468,322Q462,316 459.5,309Q457,302 457,294Q457,286 459.5,279Q462,272 468,266L638,96Q644,90 651,87.5Q658,85 666,85Q674,85 681,87.5Q688,90 694,96L864,266Q870,272 872.5,279Q875,286 875,294Q875,302 872.5,309Q870,316 864,322L694,492Q688,498 681,500.5Q674,503 666,503Q658,503 651,500.5Q644,498 638,492ZM120,400L120,160Q120,143 131.5,131.5Q143,120 160,120L400,120Q417,120 428.5,131.5Q440,143 440,160L440,400Q440,417 428.5,428.5Q417,440 400,440L160,440Q143,440 131.5,428.5Q120,417 120,400ZM520,800L520,560Q520,543 531.5,531.5Q543,520 560,520L800,520Q817,520 828.5,531.5Q840,543 840,560L840,800Q840,817 828.5,828.5Q817,840 800,840L560,840Q543,840 531.5,828.5Q520,817 520,800ZM120,800L120,560Q120,543 131.5,531.5Q143,520 160,520L400,520Q417,520 428.5,531.5Q440,543 440,560L440,800Q440,817 428.5,828.5Q417,840 400,840L160,840Q143,840 131.5,828.5Q120,817 120,800ZM200,360L360,360L360,200L200,200L200,360ZM667,408L780,295L667,182L554,295L667,408ZM600,760L760,760L760,600L600,600L600,760ZM200,760L360,760L360,600L200,600L200,760ZM360,360L360,360L360,360L360,360ZM554,295L554,295L554,295L554,295ZM360,600L360,600L360,600L360,600ZM600,600L600,600L600,600L600,600Z"/>
10+
</vector>

app/src/main/res/menu/settings_menu.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
<menu xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto">
44

5+
<item
6+
android:id="@+id/settings_widget_icon"
7+
android:contentDescription="@string/widget_icon"
8+
android:icon="@drawable/ic_widgets"
9+
android:title="@string/share_icon"
10+
android:iconTint="@color/colorPrimary"
11+
android:iconTintMode="screen"
12+
app:showAsAction="ifRoom" />
13+
514
<item
615
android:id="@+id/settings_language_icon"
716
android:contentDescription="@string/share_icon"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@
121121
<!-- Widget strings -->
122122
<string name="refresh_weather" translatable="false">Refresh weather</string>
123123
<string name="weather_icon" translatable="false">Weather icon</string>
124+
<string name="widget_icon" translatable="false">Widget Icon</string>
124125
</resources>

0 commit comments

Comments
 (0)