Skip to content

Commit

Permalink
Merge pull request #2 from muddassir235/gh_issue_1
Browse files Browse the repository at this point in the history
Resolve Issue#1
  • Loading branch information
muddassir235 authored Jul 15, 2021
2 parents f207017 + b90f36f commit bb8ed0c
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 111 deletions.
124 changes: 113 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,123 @@ dependencies {

## Use The Library

Instantiate an object
Use this library in one of the following three ways,

#### 1. Using a method and an interface

```kotlin
val connectionChecker = ConnectionChecker(this, lifecycle)
checkConnection(this) // this: lifecycleOwner (e.g. Activity) which has implemented ConnectivityListener
```
By default it will ping https://www.google.com. The user can set the url to ping.
```kotlin
val connectionChecker = ConnectionChecker(this, lifecycle, "https://www.site.url")
checkConnection(this, "https://www.site.url")
```
By default the least required lifecycle state is `Lifecycle.State.RESUMED`. The user can set it to what they require.
```kotlin
checkConnection(this, "https://www.site.url", Lifecycle.State.STARTED)
```

Example in an Android Activity.
```kotlin
class MainActivity : AppCompatActivity(), ConnectivityListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

checkConnection(this)
}

override fun onConnectionState(state: ConnectionState) {
connection_status_tv.text = when (state) {
ConnectionState.CONNECTED -> {
"Connected"
}
ConnectionState.SLOW -> {
"Slow Internet Connection"
}
else -> {
"Disconnected"
}
}
}
}
```



#### 2. [Or] Using a method and with a lambda callback

```kotlin
// this is a lifecycleOwner (e.g. Activity or ViewLifecycleOwner)
checkConnection(this) { connectionState ->
// Your logic here
}
```
By default it will ping https://www.google.com. The user can set the url to ping.
```kotlin
checkConnection(this, "https://www.site.url") { connectionState ->
// Your logic here
}
```
By default the least required lifecycle state is Lifecycle.State.RESUMED. The user can set it to what they require.
```kotlin
checkConnection(this, "https://www.site.url", Lifecycle.State.STARTED) { connectionState ->
// Your logic here
}
```

Example in an Android Activity.

```kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

checkConnection(this) { connectionState ->
connection_status_tv.text = when(connectionState) {
ConnectionState.CONNECTED -> {
"Connected"
}
ConnectionState.SLOW -> {
"Slow Internet Connection"
}
else -> {
"Disconnected"
}
}
}
}
}
```



#### 3. [Or] Using a class object

```kotlin
val connectionChecker = ConnectionChecker(this)
```
By default it will ping https://www.google.com. The user can set the url to ping.
```kotlin
val connectionChecker = ConnectionChecker(this, "https://www.site.url")
```
By default the least required lifecycle state is Lifecycle.State.RESUMED. The user can set it to what they require.
```kotlin
val connectionChecker = ConnectionChecker(this, "https://www.site.url", Lifecycle.State.STARTED)
```

Add connectivity listener
```kotlin
connectionChecker.connectivityListener = object: ConnectivityListener {
override fun onConnectionState(state: ConnectionState) {

// Your logic goes here
}
}
```

## Example
Example in an Android Activity.

```kotlin
class MainActivity : AppCompatActivity(), ConnectivityListener {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -55,18 +152,23 @@ class MainActivity : AppCompatActivity(), ConnectivityListener {
}

override fun onConnectionState(state: ConnectionState) {
connection_status_tv.text = if(state == ConnectionState.CONNECTED) {
"Connected"
} else if(state == ConnectionState.SLOW) {
"Slow Internet Connection"
} else {
"Disconnected"
connection_status_tv.text = when (state) {
ConnectionState.CONNECTED -> {
"Connected"
}
ConnectionState.SLOW -> {
"Slow Internet Connection"
}
else -> {
"Disconnected"
}
}
}
}
```

## Uses

* https://github.com/muddassir235/kmacros

## [Apps by Muddassir Ahmed](https://play.google.com/store/apps/developer?id=Muddassir+Khan):
Expand Down
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
applicationId "com.muddassir.connection_checker_app"
minSdkVersion 18
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -28,10 +28,10 @@ dependencies {
implementation project(":connection_checker")
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
package com.muddassir.connection_checker_app

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.muddassir.connection_checker.ConnectionChecker
import com.muddassir.connection_checker.ConnectionState
import com.muddassir.connection_checker.ConnectivityListener
import com.muddassir.connection_checker.checkConnection
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), ConnectivityListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val connectionChecker = ConnectionChecker(this, lifecycle)
checkConnectionFunctionAndInterface()
}

fun checkConnectionClassAndInterface() {
val connectionChecker = ConnectionChecker(this)
connectionChecker.connectivityListener = this
}

fun checkConnectionFunctionAndInterface() {
checkConnection(this)
}

fun checkConnectionFunctionAndLambda() {
checkConnection(this) {
onConnectionState(it)
}
}

override fun onConnectionState(state: ConnectionState) {
connection_status_tv.text = if(state == ConnectionState.CONNECTED) {
"Connected"
} else if(state == ConnectionState.SLOW) {
"Slow Internet Connection"
} else {
"Disconnected"
connection_status_tv.text = when (state) {
ConnectionState.CONNECTED -> {
"Connected"
}
ConnectionState.SLOW -> {
"Slow Internet Connection"
}
else -> {
"Disconnected"
}
}

// Test Lifecycle
// if(state == ConnectionState.DISCONNECTED) {
// startActivity(Intent(this, OtherActivity::class.java))
// finish()
// }
}
}

class OtherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
ext.kotlin_version = "1.5.20"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -17,7 +17,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Expand Down
18 changes: 10 additions & 8 deletions connection_checker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'maven-publish'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 18
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -42,12 +42,14 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.android.volley:volley:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.github.muddassir235:kmacros:1.8'
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@ class ConnectionCheckerTest {

@Test
fun checkConnectedState() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val connectionChecker = ConnectionChecker(context, null)
connectionChecker.connectivityListener = object: ConnectivityListener {
override fun onConnected() {
assertTrue(false)
}

override fun onDisconnected() {
assertTrue(true)
}

override fun onConnectionSlow() {
assertTrue(false)
}
}

// Disconnect from the internet. How do I do this?
}

Thread.sleep(30000)
// TODO: Add instrumented test
}
}
2 changes: 1 addition & 1 deletion connection_checker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.muddassir.connection_checker">

/
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Loading

0 comments on commit bb8ed0c

Please sign in to comment.