Skip to content

Commit

Permalink
ongoing background search check integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
UKMIITB committed May 27, 2021
1 parent 3f6befa commit dda5fcb
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class AppConstants {
val DISTRICT_ID = "districtId"
val STATE_ID = "stateId"
val PINCODE = "pincode"
val DISTRICT_NAME = "districtName"
val STATE_NAME = "stateName"

val SERVICE_REPEAT_INTERVAL = 15L // minutes

Expand All @@ -14,6 +16,7 @@ class AppConstants {
val NOTIFICATION_CHANNEL_ID = "101"

val UNIQUE_WORK_NAME = "Cowin Search Work"
val JOB_SCHEDULER_ID = 100

val ALERT_DIALOG_MESSAGE =
"Do you want to continue search in background and send notification when slots are available"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,58 @@ package com.example.cowinnotifier.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.example.cowinnotifier.R
import com.example.cowinnotifier.helper.AppConstants
import com.example.cowinnotifier.utils.SchedulerUtil
import com.example.cowinnotifier.viewmodel.ActivityViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.activity_current_search.*

@AndroidEntryPoint
class CurrentSearchActivity : AppCompatActivity() {

private val viewModel: ActivityViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_current_search)

setupView()
}

private fun setupView() {
if (SchedulerUtil.isAnyJobScheduled(applicationContext)) {
val pincode = viewModel.getStringSharedPreferenceValue(AppConstants.PINCODE)
val districtName = viewModel.getStringSharedPreferenceValue(AppConstants.DISTRICT_NAME)
val stateName = viewModel.getStringSharedPreferenceValue(AppConstants.STATE_NAME)
val vaccine = viewModel.getStringSharedPreferenceValue(AppConstants.VACCINE)
val age = viewModel.getLongSharedPreferenceValue(AppConstants.AGE_LIMIT)

val searchParameter =
if (pincode.isNullOrEmpty()) "$districtName, $stateName" else "$pincode"
val ageLimit = if (age == 0L) "None" else age.toString()
val vaccineFilter = if (vaccine.isNullOrEmpty()) "None" else vaccine

setDisplayData(searchParameter, ageLimit, vaccineFilter)
} else {
displayNoDataView()
}
}

private fun displayNoDataView() {
setDisplayData("N/A", "N/A", "N/A")
imageview_no_data.visibility = View.VISIBLE
textview_no_job_scheduled.visibility = View.VISIBLE
}

private fun setDisplayData(searchParameter: String, ageLimit: String, vaccineFilter: String) {
val locationData = "Search Location: $searchParameter"
val ageLimitData = "Age Limit: $ageLimit"
val vaccineFilterData = "Vaccine Filter: $vaccineFilter"
textview_location.text = locationData
textview_age_limit.text = ageLimitData
textview_vaccine_name.text = vaccineFilterData
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class DistrictSearchFragment : Fragment() {
viewModel.updateSharedPreferenceValue(AppConstants.STATE_ID, districtList[spinner_district.selectedItemPosition].state_id.toString())
viewModel.updateSharedPreferenceValue(AppConstants.AGE_LIMIT, ageFilter)
viewModel.updateSharedPreferenceValue(AppConstants.VACCINE, vaccineFilter)
viewModel.updateSharedPreferenceValue(AppConstants.DISTRICT_NAME, spinner_district.selectedItem.toString())
viewModel.updateSharedPreferenceValue(AppConstants.STATE_NAME, spinner_state.selectedItem.toString())

viewModel.startActivityFromIntent(AppConstants.DISTRICT_ID, districtId.toString(), mContext, ageFilter, vaccineFilter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import com.example.cowinnotifier.R
import com.example.cowinnotifier.helper.AppConstants
import com.example.cowinnotifier.utils.FilterUtil
import com.example.cowinnotifier.viewmodel.ActivityViewModel
import kotlinx.android.synthetic.main.fragment_district_search.*
import kotlinx.android.synthetic.main.fragment_pincode_search.*
import kotlinx.android.synthetic.main.fragment_pincode_search.spinner_age_filter
import kotlinx.android.synthetic.main.fragment_pincode_search.spinner_vaccine_filter


class PincodeSearchFragment : Fragment() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.cowinnotifier.utils

import android.app.NotificationManager
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class SchedulerUtil {
val jobScheduler =
context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val jobInfo =
JobInfo.Builder(100, ComponentName(context, BackgroundSearchService::class.java))
JobInfo.Builder(
AppConstants.JOB_SCHEDULER_ID,
ComponentName(context, BackgroundSearchService::class.java)
)

val job = jobInfo.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(AppConstants.SERVICE_REPEAT_INTERVAL)
Expand All @@ -24,5 +27,16 @@ class SchedulerUtil {
jobScheduler.cancelAll() // Canceling all previously added job if any
jobScheduler.schedule(job)
}

fun isAnyJobScheduled(context: Context): Boolean {
val jobScheduler =
context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler

for (eachJobInfo in jobScheduler.allPendingJobs) {
if (eachJobInfo.id == AppConstants.JOB_SCHEDULER_ID)
return true
}
return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ class ActivityViewModel @Inject constructor(private val apiRepository: APIReposi
}
}

fun getSharedPreferenceValue(key: String): String? {
return MyApplication.sharedPreferences.getString(key, "-1")
fun getStringSharedPreferenceValue(key: String): String? {
return MyApplication.sharedPreferences.getString(key, "")
}

fun getLongSharedPreferenceValue(key: String): Long {
return MyApplication.sharedPreferences.getLong(key, 0L)
}

fun updateSharedPreferenceValue(key: String, value: String) {
Expand Down
76 changes: 76 additions & 0 deletions app/src/main/res/layout/activity_current_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,80 @@
android:layout_height="match_parent"
tools:context=".ui.CurrentSearchActivity">

<ImageView
android:id="@+id/imageview_no_data"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_baseline_sentiment_very_dissatisfied_24"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/textview_no_job_scheduled"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_vaccine_name" />

<TextView
android:id="@+id/textview_no_job_scheduled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/no_job_scheduled_message"
android:textSize="18sp"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageview_no_data" />

<TextView
android:id="@+id/textview_background_search_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="Ongoing background search"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textview_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="8dp"
android:text="@string/search_location"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_background_search_header" />

<TextView
android:id="@+id/textview_age_limit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="8dp"
android:text="@string/age_limit"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_location" />

<TextView
android:id="@+id/textview_vaccine_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="8dp"
android:text="@string/vaccine_filter"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_age_limit" />

</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">CowinNotifier</string>
<string name="no_data_message">Sorry we couldn\'t find any vaccination center.
<string name="no_data_message">Sorry we couldn\'t find any vaccination center.<br/>
Try searching for some other area</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
Expand All @@ -13,5 +13,9 @@
<string name="how_to_use_point_5">5. For <b>Xiaomi</b>, <b>OPPO</b> &amp; <b>VIVO</b> users, Please click on settings link below and <b>enable autostart</b> permission for background search to work properly</string>
<string name="how_to_use_section_header">How to use section</string>
<string name="open_app_settings">Open App Settings</string>

<string name="no_job_scheduled_message">Sorry no background search scheduled.<br/>
Search for center to enable background search</string>
<string name="search_location">Search Location:</string>
<string name="age_limit">Age Limit:</string>
<string name="vaccine_filter">Vaccine Filter:</string>
</resources>

0 comments on commit dda5fcb

Please sign in to comment.