From 825184e7c275eda068cd4244b2f7c4b413092a96 Mon Sep 17 00:00:00 2001 From: raheemadamboev Date: Sun, 8 May 2022 19:23:49 +0500 Subject: [PATCH] implement suspend check internet example --- .idea/misc.xml | 1 + app/build.gradle | 3 ++- .../checkinternetdemo/MainActivity.kt | 21 ++++++++++++++++--- app/src/main/res/layout/activity_main.xml | 18 ++++++++++++---- app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 8376b62..6088867 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,6 +4,7 @@ diff --git a/app/build.gradle b/app/build.gradle index 800919e..8a0c67e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,6 +42,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1" - implementation 'com.github.raheemadamboev:check-internet-android:1.0.1' + implementation project(path: ':check_internet') } \ No newline at end of file diff --git a/app/src/main/java/xyz/teamgravity/checkinternetdemo/MainActivity.kt b/app/src/main/java/xyz/teamgravity/checkinternetdemo/MainActivity.kt index 4b40afa..3113dac 100644 --- a/app/src/main/java/xyz/teamgravity/checkinternetdemo/MainActivity.kt +++ b/app/src/main/java/xyz/teamgravity/checkinternetdemo/MainActivity.kt @@ -2,6 +2,8 @@ package xyz.teamgravity.checkinternetdemo import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.launch import xyz.teamgravity.checkinternet.CheckInternet import xyz.teamgravity.checkinternetdemo.databinding.ActivityMainBinding @@ -18,12 +20,13 @@ class MainActivity : AppCompatActivity() { } private fun button() { - onCheck() + onCheckCallback() + onCheckSuspend() } - private fun onCheck() { + private fun onCheckCallback() { binding.apply { - checkB.setOnClickListener { + checkCallbackB.setOnClickListener { stateT.text = getString(R.string.checking) @@ -37,4 +40,16 @@ class MainActivity : AppCompatActivity() { } } } + + private fun onCheckSuspend() { + binding.apply { + checkSuspendB.setOnClickListener { + lifecycleScope.launch { + stateT.text = getString(R.string.checking) + val connected = CheckInternet().check() + stateT.text = getString(if (connected) R.string.internet else R.string.no_internet) + } + } + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e4cbfee..a5d3827 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,18 +16,28 @@ android:textAlignment="center" android:textColor="@color/black" android:textSize="22sp" - app:layout_constraintBottom_toTopOf="@id/check_b" + app:layout_constraintBottom_toTopOf="@id/check_callback_b" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_chainStyle="packed" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dff3f7e..cd7a22e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,8 @@ CheckInternetDemo Check internet + Check internet (callback) + Check internet (suspend) Checking… Internet NOT working Internet working