-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update the example project to show online offline feature
- Loading branch information
Showing
7 changed files
with
129 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 56 additions & 3 deletions
59
app/src/main/java/com/maku/networkutilexample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,79 @@ | ||
package com.maku.networkutilexample | ||
|
||
import android.animation.Animator | ||
import android.animation.AnimatorListenerAdapter | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.core.content.ContextCompat | ||
import androidx.databinding.DataBindingUtil | ||
import com.maku.networkutil.util.NetworkUtil | ||
import com.maku.networkutilexample.databinding.ActivityMainBinding | ||
import com.maku.networkutilexample.util.hide | ||
import com.maku.networkutilexample.util.show | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
//databinding | ||
private lateinit var mViewBinding: ActivityMainBinding | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
//initialize the binding | ||
mViewBinding = DataBindingUtil.setContentView(this, | ||
R.layout.activity_main | ||
) | ||
|
||
handleNetwork() | ||
} | ||
|
||
private fun handleNetwork() { | ||
NetworkUtil.getNetworkLiveData(applicationContext).observe(this, { isConnected -> | ||
if (!isConnected) { | ||
Toast.makeText(this, "no internet", Toast.LENGTH_SHORT).show() | ||
//Toast.makeText(this, "no internet", Toast.LENGTH_SHORT).show() | ||
mViewBinding.textViewNetworkStatus.text = getString(R.string.text_no_connectivity) | ||
mViewBinding.networkStatusLayout.apply { | ||
alpha = 0f | ||
show() | ||
setBackgroundColor( | ||
ContextCompat.getColor( | ||
context, | ||
R.color.colorStatusNotConnected | ||
)) | ||
animate() | ||
.alpha(1f) | ||
.setDuration(ANIMATION_DURATION) | ||
.setListener(null) | ||
} | ||
} else { | ||
Toast.makeText(this, "internet available", Toast.LENGTH_SHORT).show() | ||
//Toast.makeText(this, "internet available", Toast.LENGTH_SHORT).show() | ||
mViewBinding.textViewNetworkStatus.text = getString(R.string.text_connectivity) | ||
mViewBinding.networkStatusLayout.apply { | ||
setBackgroundColor( ContextCompat.getColor( | ||
context, | ||
R.color.colorStatusConnected | ||
)) | ||
|
||
animate() | ||
.alpha(0f) | ||
.setStartDelay(ANIMATION_DURATION) | ||
.setDuration(ANIMATION_DURATION) | ||
.setListener(object : AnimatorListenerAdapter() { | ||
override fun onAnimationEnd(animation: Animator) { | ||
hide() | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Companion object | ||
*/ | ||
companion object { | ||
const val ANIMATION_DURATION = 1000.toLong() | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/maku/networkutilexample/util/view.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.maku.networkutilexample.util | ||
|
||
import android.view.View | ||
|
||
fun View.show() { | ||
visibility = View.VISIBLE | ||
} | ||
|
||
fun View.hide() { | ||
visibility = View.GONE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<layout 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:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
<data> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</data> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
|
||
<LinearLayout | ||
android:id="@+id/networkStatusLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp" | ||
style="@style/StatusView" | ||
android:background="@color/colorStatusNotConnected" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent"> | ||
|
||
<TextView | ||
android:gravity="center" | ||
android:id="@+id/textViewNetworkStatus" | ||
style="@style/StatusTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:text="@string/text_no_connectivity" /> | ||
</LinearLayout> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<resources> | ||
<string name="app_name">NetworkUtilExample</string> | ||
<string name="text_no_connectivity">No Internet Connection, please connect …</string> | ||
<string name="text_connectivity">Back Online</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters