Skip to content

Commit

Permalink
Implemented Home screen widget to display current active spot
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshPanchal18 committed Mar 10, 2024
1 parent 9354dae commit a2ed821
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ dependencies {
implementation(libs.accompanist.permissions)
implementation(libs.accompanist.systemuicontroller)
implementation(libs.gson)

implementation(libs.androidx.glance)
implementation(libs.androidx.glance.appwidget)
}
19 changes: 16 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,28 @@
<service
android:name="dev.harsh.tradow.quicksetting.TrackingQsTile"
android:enabled="true"
android:icon="@drawable/location"
android:roundIcon="@drawable/location"
android:exported="true"
android:icon="@drawable/location"
android:label="Start Tradow tracking"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:roundIcon="@drawable/location">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<receiver
android:name="dev.harsh.tradow.widget.TradowWidgetReceiver"
android:exported="true"
android:label="Tradow tracking">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider" />
</receiver>

</application>

</manifest>
11 changes: 10 additions & 1 deletion app/src/main/java/dev/harsh/tradow/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
Expand Down Expand Up @@ -110,6 +111,7 @@ class MainActivity : ComponentActivity() {

var pressedTime: Long = 0
BackHandler(enabled = true) {
sharedPref.edit(commit = true) {}
if (pressedTime + 1800 > System.currentTimeMillis())
finish()
else
Expand Down Expand Up @@ -142,10 +144,17 @@ class MainActivity : ComponentActivity() {
override fun onPause() {
super.onPause()

sharedPref.edit(commit = true) {}

if (checkLocationPermission())
moveTaskToBack(true)
}

override fun onDestroy() {
super.onDestroy()
sharedPref.edit(commit = true) {}
}

@Composable
fun HomeScreen(forOpenBottomSheet: () -> Unit) {
var hasLocationPermission = false
Expand Down Expand Up @@ -179,7 +188,7 @@ class MainActivity : ComponentActivity() {
}
}

else -> this@MainActivity.showShortToast("Kindly choose a site to monitor!")
else -> this@MainActivity.showShortToast("Kindly choose a spot to track!")
}
} else {
locationPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
Expand Down
116 changes: 116 additions & 0 deletions app/src/main/java/dev/harsh/tradow/widget/TradowWidget.kt
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 = ""
)
}
}
}

}
}

}
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()
}
12 changes: 6 additions & 6 deletions app/src/main/res/drawable/location.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
android:height="32dp"
android:viewportWidth="8.467"
android:viewportHeight="8.467">
<path
android:fillColor="#FF000000"
android:strokeWidth="1"
android:pathData="M4.2333,0.2646C3.7032,0.2646 3.2052,0.4109 2.7771,0.6604A0.2646,0.2646 135,1 0,3.0437 1.1172C3.3957,0.9121 3.8002,0.7937 4.2333,0.7937C5.5439,0.7937 6.6145,1.8644 6.6145,3.175C6.6145,3.7964 6.315,4.3675 5.8998,4.9211C5.4854,5.4736 4.9499,6.0074 4.4203,6.537C4.3148,6.6425 4.1517,6.6425 4.0462,6.537C3.5166,6.0074 2.9811,5.4736 2.5667,4.9211C2.1516,4.3675 1.8521,3.7964 1.8521,3.175C1.8521,2.7419 1.9704,2.3374 2.1756,1.9854A0.2646,0.2646 0,1 0,1.7187 1.7187C1.4692,2.1469 1.3229,2.6449 1.3229,3.175C1.3229,3.9647 1.6967,4.6433 2.143,5.2384C2.5901,5.8345 3.1433,6.3824 3.6721,6.9111C3.9798,7.2188 4.4867,7.2188 4.7945,6.9111C5.3233,6.3824 5.8765,5.8345 6.3236,5.2384C6.7699,4.6433 7.1437,3.9647 7.1437,3.175C7.1437,1.5756 5.8326,0.2646 4.2333,0.2646zM4.2322,1.8515C3.5048,1.8515 2.9099,2.448 2.9099,3.1755C2.9099,3.903 3.5048,4.4979 4.2322,4.4979C4.9597,4.4979 5.5567,3.903 5.5567,3.1755C5.5567,2.448 4.9597,1.8515 4.2322,1.8515zM4.2322,2.3807C4.6737,2.3807 5.0275,2.734 5.0275,3.1755C5.0275,3.617 4.6738,3.9687 4.2322,3.9687C3.7907,3.9687 3.4395,3.617 3.4395,3.1755C3.4395,2.734 3.7907,2.3807 4.2322,2.3807zM3.1491,7.6733A0.2646,0.2646 45,0 0,3.1486 7.6739A0.265,0.265 45,0 0,3.176 8.203L5.2911,8.203A0.2646,0.2646 0,1 0,5.2911 7.6739L3.176,7.6739A0.2646,0.2646 45,0 0,3.1491 7.6733z"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:fillColor="#FF000000"
android:fillType="evenOdd"
android:pathData="M4.2333,0.2646C3.7032,0.2646 3.2052,0.4109 2.7771,0.6604A0.2646,0.2646 135,1 0,3.0437 1.1172C3.3957,0.9121 3.8002,0.7937 4.2333,0.7937C5.5439,0.7937 6.6145,1.8644 6.6145,3.175C6.6145,3.7964 6.315,4.3675 5.8998,4.9211C5.4854,5.4736 4.9499,6.0074 4.4203,6.537C4.3148,6.6425 4.1517,6.6425 4.0462,6.537C3.5166,6.0074 2.9811,5.4736 2.5667,4.9211C2.1516,4.3675 1.8521,3.7964 1.8521,3.175C1.8521,2.7419 1.9704,2.3374 2.1756,1.9854A0.2646,0.2646 0,1 0,1.7187 1.7187C1.4692,2.1469 1.3229,2.6449 1.3229,3.175C1.3229,3.9647 1.6967,4.6433 2.143,5.2384C2.5901,5.8345 3.1433,6.3824 3.6721,6.9111C3.9798,7.2188 4.4867,7.2188 4.7945,6.9111C5.3233,6.3824 5.8765,5.8345 6.3236,5.2384C6.7699,4.6433 7.1437,3.9647 7.1437,3.175C7.1437,1.5756 5.8326,0.2646 4.2333,0.2646zM4.2322,1.8515C3.5048,1.8515 2.9099,2.448 2.9099,3.1755C2.9099,3.903 3.5048,4.4979 4.2322,4.4979C4.9597,4.4979 5.5567,3.903 5.5567,3.1755C5.5567,2.448 4.9597,1.8515 4.2322,1.8515zM4.2322,2.3807C4.6737,2.3807 5.0275,2.734 5.0275,3.1755C5.0275,3.617 4.6738,3.9687 4.2322,3.9687C3.7907,3.9687 3.4395,3.617 3.4395,3.1755C3.4395,2.734 3.7907,2.3807 4.2322,2.3807zM3.1491,7.6733A0.2646,0.2646 45,0 0,3.1486 7.6739A0.265,0.265 45,0 0,3.176 8.203L5.2911,8.203A0.2646,0.2646 0,1 0,5.2911 7.6739L3.176,7.6739A0.2646,0.2646 45,0 0,3.1491 7.6733z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/refresh.xml
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>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/preview_widget.xml
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>
21 changes: 21 additions & 0 deletions app/src/main/res/xml/app_widget_provider.xml
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
-->
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ activity-compose = "1.8.2"
androidx-junit = "1.1.5"
core-ktx = "1.12.0"
espresso-core = "3.5.1"
glanceVersion = "1.1.0-alpha01"
gson = "2.10"
junit = "4.13.2"
lifecycle-runtime-ktx = "2.7.0"
Expand All @@ -21,6 +22,8 @@ accompanist-permissions = { module = "com.google.accompanist:accompanist-permiss
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity-compose" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" }
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso-core" }
androidx-glance = { module = "androidx.glance:glance", version.ref = "glanceVersion" }
androidx-glance-appwidget = { module = "androidx.glance:glance-appwidget", version.ref = "glanceVersion" }
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-junit" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle-runtime-ktx" }
androidx-material-icons-extended = { module = "androidx.compose.material:material-icons-extended", version.ref = "material-icons-extended" }
Expand Down

0 comments on commit a2ed821

Please sign in to comment.