-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Home screen widget to display current active spot
- Loading branch information
1 parent
9354dae
commit a2ed821
Showing
10 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/src/main/java/dev/harsh/tradow/widget/TradowWidget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package dev.harsh.tradow.widget | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.glance.GlanceId | ||
import androidx.glance.GlanceModifier | ||
import androidx.glance.GlanceTheme | ||
import androidx.glance.Image | ||
import androidx.glance.ImageProvider | ||
import androidx.glance.action.clickable | ||
import androidx.glance.appwidget.GlanceAppWidget | ||
import androidx.glance.appwidget.provideContent | ||
import androidx.glance.background | ||
import androidx.glance.layout.Alignment | ||
import androidx.glance.layout.Box | ||
import androidx.glance.layout.Row | ||
import androidx.glance.layout.fillMaxWidth | ||
import androidx.glance.layout.padding | ||
import androidx.glance.state.GlanceStateDefinition | ||
import androidx.glance.state.PreferencesGlanceStateDefinition | ||
import androidx.glance.text.FontWeight | ||
import androidx.glance.text.Text | ||
import androidx.glance.text.TextStyle | ||
import androidx.glance.unit.ColorProvider | ||
import com.example.geofencing.R | ||
import dev.harsh.tradow.model.Spot | ||
import dev.harsh.tradow.util.SharedPreferencesHelper | ||
|
||
class TradowWidget : GlanceAppWidget() { | ||
|
||
/** GlanceStateDefinition - Telling GlanceState how to store-retrieve data. | ||
- Once the object is created, the data is updating using the state directly. | ||
* PreferencesGlanceStateDefinition - For creating a widget using datastore preference | ||
*/ | ||
override val stateDefinition: GlanceStateDefinition<*> = PreferencesGlanceStateDefinition | ||
private var activeSpot: Spot? = null | ||
|
||
override suspend fun provideGlance(context: Context, id: GlanceId) { | ||
provideContent { | ||
|
||
activeSpot = SharedPreferencesHelper.getActiveSpot(context) | ||
|
||
LaunchedEffect(activeSpot) { | ||
activeSpot = SharedPreferencesHelper.getActiveSpot(context) | ||
} | ||
|
||
GlanceTheme { | ||
WidgetCard(context, activeSpot) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun WidgetCard(context: Context, spot: Spot?) { | ||
Row( | ||
modifier = GlanceModifier | ||
.fillMaxWidth() | ||
.padding(12.dp) | ||
.background(Color.White), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
if (spot != null) { | ||
|
||
Text( | ||
text = spot.title, | ||
modifier = GlanceModifier.padding(4.dp), | ||
style = TextStyle( | ||
fontWeight = FontWeight.Bold, | ||
fontSize = 20.sp, | ||
color = ColorProvider(Color.Green.copy(0.8F)) | ||
) | ||
) | ||
|
||
Box( | ||
modifier = GlanceModifier | ||
.padding(horizontal = 8.dp) | ||
.clickable { | ||
activeSpot = SharedPreferencesHelper.getActiveSpot(context) | ||
} | ||
) { | ||
Image( | ||
provider = ImageProvider(R.drawable.refresh), contentDescription = "" | ||
) | ||
} | ||
|
||
} else { | ||
Row { | ||
Text( | ||
text = "Add/Select spot to continue", | ||
style = TextStyle( | ||
fontWeight = FontWeight.Bold, | ||
fontSize = 16.sp | ||
) | ||
) | ||
Box( | ||
modifier = GlanceModifier | ||
.clickable { | ||
activeSpot = SharedPreferencesHelper.getActiveSpot(context) | ||
} | ||
) { | ||
Image( | ||
provider = ImageProvider(R.drawable.location), contentDescription = "" | ||
) | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/dev/harsh/tradow/widget/TradowWidgetReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.harsh.tradow.widget | ||
|
||
import androidx.glance.appwidget.GlanceAppWidget | ||
import androidx.glance.appwidget.GlanceAppWidgetReceiver | ||
|
||
class TradowWidgetReceiver: GlanceAppWidgetReceiver() { | ||
override val glanceAppWidget: GlanceAppWidget | ||
get() = TradowWidget() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="30" android:viewportWidth="30" android:width="24dp"> | ||
|
||
<path android:fillColor="#FF000000" android:pathData="M15,3C12.031,3 9.303,4.083 7.207,5.875A1,1 0,1 0,8.506 7.395C10.254,5.9 12.517,5 15,5C20.197,5 24.451,8.938 24.951,14L22,14L26,20L30,14L26.949,14C26.438,7.852 21.278,3 15,3zM4,10L0,16L3.051,16C3.562,22.148 8.722,27 15,27C17.969,27 20.697,25.917 22.793,24.125A1,1 0,1 0,21.494 22.605C19.746,24.1 17.483,25 15,25C9.803,25 5.549,21.062 5.049,16L8,16L4,10z"/> | ||
|
||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:layout_marginStart="10dp" | ||
android:fontFamily="monospace" | ||
android:text="@string/app_name" | ||
android:textAlignment="center" | ||
android:textColor="@color/black" | ||
android:textSize="20sp" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:minWidth="180dp" | ||
android:minHeight="40dp" | ||
android:minResizeWidth="110dp" | ||
android:minResizeHeight="40dp" | ||
android:resizeMode="vertical|horizontal" /> | ||
|
||
<!-- | ||
# of Cells Available Size (dp) | ||
(Columns or Rows) (minWidth or minHeight) | ||
1 40dp | ||
2 110dp | ||
3 180dp | ||
4 250dp | ||
5 320dp | ||
n 70 × n − 30 | ||
https://developer.android.com/guide/practices/ui_guidelines/widget_design.html | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters