Skip to content

Commit

Permalink
Add Thank You notification
Browse files Browse the repository at this point in the history
  • Loading branch information
aimproxy committed Feb 15, 2023
1 parent 3beac4c commit 6fe8126
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
12 changes: 3 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
Expand All @@ -19,7 +18,8 @@
tools:targetApi="31">
<activity
android:name=".GoogleSignInActivity"
android:exported="true">
android:exported="true"
android:theme="@style/Theme.Chargify">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -28,13 +28,7 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Chargify">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:theme="@style/Theme.Chargify" />
</application>

</manifest>
43 changes: 42 additions & 1 deletion app/src/main/java/com/aimproxy/chargify/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.aimproxy.chargify

import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.Manifest.permission.POST_NOTIFICATIONS
import android.annotation.SuppressLint
import android.app.*
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
Expand All @@ -24,6 +26,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat.startActivity
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
Expand Down Expand Up @@ -54,6 +58,15 @@ class MainActivity : ComponentActivity() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var listeningToUpdates = false

companion object {
const val APP_NAME = "Chargify"
const val CHANNEL_NAME = "Chargify Channel"
const val CHANNEL_ID = "chargify_channel"
const val NOTIFICATION_ID = 12345
const val REQUEST_CODE_LOCATION = 0
const val REQUEST_CODE_NOTIFICATION = 1
}

private val locationCallback: LocationCallback = object : LocationCallback() {
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onLocationResult(locationResult: LocationResult) {
Expand Down Expand Up @@ -96,20 +109,48 @@ class MainActivity : ComponentActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onStart() {
super.onStart()
if (checkSelfPermission(ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(ACCESS_FINE_LOCATION), 0)
requestPermissions(arrayOf(ACCESS_FINE_LOCATION), REQUEST_CODE_LOCATION)
}
if (checkSelfPermission(POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(POST_NOTIFICATIONS), REQUEST_CODE_NOTIFICATION)
}
createNotificationChannel()
}

@SuppressLint("MissingPermission")
override fun onStop() {
super.onStop()
if (listeningToUpdates) {
fusedLocationClient.removeLocationUpdates(locationCallback)
}

// Notification Alert
val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(APP_NAME)
.setContentText("Thank you for using Chargify!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setOngoing(true)
.setAutoCancel(true)

with(NotificationManagerCompat.from(applicationContext)) {
notify(NOTIFICATION_ID, builder.build())
}
}

private fun createNotificationChannel() {
val channel =
NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}


@SuppressLint("MissingPermission")
private fun startUpdatingLocation() {
val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 2000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.aimproxy.chargify.components

import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.FavoriteBorder
Expand All @@ -18,7 +16,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.aimproxy.chargify.firestore.BookmarksAggregation.*
import com.aimproxy.chargify.viewmodels.BookmarksViewModel

@RequiresApi(Build.VERSION_CODES.S)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BookmarkedEvStationItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.aimproxy.chargify.screens

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -15,6 +17,7 @@ import com.aimproxy.chargify.components.BookmarkedEvStationItem
import com.aimproxy.chargify.components.BookmarksEmptyState
import com.aimproxy.chargify.viewmodels.BookmarksViewModel

@RequiresApi(Build.VERSION_CODES.S)
@Composable
fun BookmarksScreen(
bookmarksViewModel: BookmarksViewModel = viewModel(),
Expand Down

0 comments on commit 6fe8126

Please sign in to comment.