Skip to content

Commit

Permalink
implement suspend check internet example
Browse files Browse the repository at this point in the history
  • Loading branch information
raheemadamboev committed May 8, 2022
1 parent 94c04e1 commit 825184e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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)
}
}
}
}
}
18 changes: 14 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<com.google.android.material.button.MaterialButton
android:id="@+id/check_b"
android:id="@+id/check_callback_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="@string/check_internet"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@string/check_internet_callback"
app:layout_constraintBottom_toTopOf="@id/check_suspend_b"
app:layout_constraintTop_toBottomOf="@id/state_t" />

<com.google.android.material.button.MaterialButton
android:id="@+id/check_suspend_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:text="@string/check_internet_suspend"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/check_callback_b" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<string name="app_name">CheckInternetDemo</string>

<string name="check_internet">Check internet</string>
<string name="check_internet_callback">Check internet (callback)</string>
<string name="check_internet_suspend">Check internet (suspend)</string>
<string name="checking">Checking…</string>
<string name="no_internet">Internet NOT working</string>
<string name="internet">Internet working</string>
Expand Down

0 comments on commit 825184e

Please # to comment.