Skip to content

Instantly share code, notes, and snippets.

View ibrahimbroachwala's full-sized avatar
🎯
Focusing

Ibrahim Broachwala ibrahimbroachwala

🎯
Focusing
View GitHub Profile
data class GeofenceModel(var id: String,var lat : Double, var lng: Double, var radius: Double)
@Entity(tableName = "toll")
data class Toll(
@PrimaryKey
var id: Int = 0,
var name: String = "",
var lat: String = "",
var lng: String = ""
)
@ibrahimbroachwala
ibrahimbroachwala / MainActivity.kt
Last active February 15, 2020 15:05
Step 2, Dynamic Geofencing
private fun setupGeofence() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext)
//Get user's current location
fusedLocationClient.lastLocation.addOnSuccessListener {
val userLat = it.latitude
val userLong = it.longitude
@ibrahimbroachwala
ibrahimbroachwala / MainActivity.kt
Last active February 15, 2020 15:17
Step 1, Dynamic Geofencing
private fun setupGeofence() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext)
//Get user's current location
fusedLocationClient.lastLocation.addOnSuccessListener {
val userLat = it.latitude
val userLong = it.longitude
//Create an instance of our GeofenceHelper Class
@ibrahimbroachwala
ibrahimbroachwala / GeofenceHelper.kt
Last active February 15, 2020 15:28
Step 1, Geofence Helper
class GeofenceHelper(context: Context) {
fun buildGeofencingRequest(geofences: List<Geofence>): GeofencingRequest {
return GeofencingRequest.Builder()
.setInitialTrigger(0)
.addGeofences(geofences)
.build()
}
val geofencePendingIntent: PendingIntent by lazy {
var tollsList: LiveData<List<GeofenceModel>> =
Transformations.map(DataRepository.getAllTolls(), ::getTolls)
private fun getTolls(tolls: List<Toll>): List<GeofenceModel> {
val list = ArrayList<GeofenceModel>()
tolls.forEach {
list.add(
GeofenceModel(
@ibrahimbroachwala
ibrahimbroachwala / GeofenceHelper.kt
Created February 15, 2020 16:43
step 2, Geofence Helper
fun setupChildGeofences(
context: Context,
geofenceModelList: List<GeofenceModel>,
currentLat: Double,
currentLong: Double,
geofencingClient: GeofencingClient
) {
//Filter list as per user's current location
class GeofenceTransitionsJobIntentService : JobIntentService() {
companion object {
private const val JOB_ID = 101
fun enqueueWork(context: Context, intent: Intent) {
enqueueWork(
context,
GeofenceTransitionsJobIntentService::class.java, JOB_ID,
intent
)
class GeofenceBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
GeofenceTransitionsJobIntentService.enqueueWork(context, intent)
}
}