Skip to content

Instantly share code, notes, and snippets.

View Lavanyagaur22's full-sized avatar
🚩

Lavanya gaur Lavanyagaur22

🚩
View GitHub Profile
object Utils {
//To run on emulator use http://10.0.2.2:4000/graphql
const val BASE_URL = "http://10.0.2.2:4000/graphql"
private var apClient: ApolloClient? = null
private var httpClient: OkHttpClient? = null
@JvmStatic
fun getApolloClient(context: Context): ApolloClient? {
fun createTask(title: String, description: String, version: Int) {
val input =
TaskInput.builder().title(title).description(description).version(version).status("test")
.build()
val mutation = CreateTaskMutation.builder().input(input).build()
val mutationCall = Utils.getApolloClient(this)?.mutate(
mutation
//Create a mutation object
val input = TaskInput.builder().title(title).version(version).description(description).status("test").build()
val mutation = UpdateTaskMutation.builder().id(id).input(input).build()
//Create an object of apolloCall
val mutationCall = apolloClient.mutate(mutation)?.refetchQueries(apolloQueryWatcher?.operation()?.name())
//Create a callback object of type ApolloCall.Callback
val callback = object : ApolloCall.Callback<UpdateTaskMutation.Data>() {
fun getTasks() {
FindAllTasksQuery.builder()?.build()?.let {
Utils.getApolloClient(this)?.query(it)
?.responseFetcher(ApolloResponseFetchers.NETWORK_FIRST)
?.enqueue(object : ApolloCall.Callback<FindAllTasksQuery.Data>() {
override fun onFailure(e: ApolloException) {
e.printStackTrace()
Log.e(TAG, "getTasks ----$e ")
// NotificationRequestWorker extends the worker class which defines the work to be performed by the worker.
class NotificationRequestWorker(val context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
// Do the work here
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationCompat.Builder(context, "first").apply {
setContentTitle("Background task")
setContentText("Sample text")
setSmallIcon(R.drawable.ic_launcher_foreground)
private fun scheduleTask() {
// build an object of OneTimeWorkRequestBuilder
val workerRequest = OneTimeWorkRequestBuilder<NotificationRequestWorker>()
.setInitialDelay(20, TimeUnit.SECONDS)
.build()
// Enqueue the above workrequest object to the WorkManager
WorkManager.getInstance(this).enqueue(workerRequest)
}
private fun scheduleRepeatingTasks() {
/*Setting up different constraints on the work request.
*/
val constraints = Constraints.Builder().apply {
setRequiredNetworkType(NetworkType.CONNECTED)
setRequiresCharging(true)
setRequiresStorageNotLow(true)
}.build()
/*For scheduling a one time work request.
*/
button.setOnClickListener {
scheduleTask()
}
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
val KEY_DATA = "data"
val KEY_APP_OPEN = "app_open"
var appOpenCount = 0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">