Skip to content

Commit

Permalink
Merge pull request #7 from Teknasyon-Teknoloji/fix/network
Browse files Browse the repository at this point in the history
Fix/network
  • Loading branch information
baranoguz authored Aug 14, 2022
2 parents 7d28b50 + b31fc69 commit 8e5d0c0
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 62 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

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

7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Changelog
All notable changes to this project will be documented in this file.

## 1.2.0"
## 1.2.1 (2022-08-12)
- Improve internet connection control in chat

## 1.2.0 (2022-03-30)
- Fixed online offline request timing.
- Add auto login concept
- Add canned response feature.
- Fixed general helper bug fix
- General refactor.


## 1.1.0"
## 1.1.0 (2022-02-03)
- Fixed to add file option for online chat case.
- Add custom fields for offline chat and dynamic offline chat screen
- Add base model refactor for isOffline mode properties check.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Add the dependency

```groovy
dependencies {
implementation 'com.github.Teknasyon-Teknoloji:desk360-livechat-android-sdk:1.0.8'
implementation 'com.github.Teknasyon-Teknoloji:desk360-livechat-android-sdk:1.2.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
Expand Down
4 changes: 2 additions & 2 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.5.20"
ext.kotlin_version = "1.7.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Apr 09 09:33:39 TRT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
22 changes: 10 additions & 12 deletions livechat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ plugins {

def artifactId = 'desk360-livechat-android-sdk'
def groupId = 'com.github.Teknasyon-Teknoloji'
def versionN = "1.2.0"
def versionN = "1.2.1"

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
Expand Down Expand Up @@ -55,7 +55,6 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api 'androidx.core:core-ktx:1.5.0'
api 'androidx.appcompat:appcompat:1.3.0'
api 'com.google.android.material:material:1.3.0'
Expand All @@ -78,34 +77,33 @@ dependencies {
api 'com.jakewharton.rxbinding2:rxbinding:2.0.0'

// lifecycle
def lifecycle_version = "2.3.1"
def lifecycle_version = "2.5.1"

// ViewModel
api "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
api "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
api 'androidx.lifecycle:lifecycle-extensions:2.2.0'

// glide
api 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

// firebase
api 'com.google.firebase:firebase-messaging-ktx:22.0.0'
api 'com.google.firebase:firebase-analytics-ktx:19.0.0'
api "com.google.firebase:firebase-common-ktx:20.0.0"
api "com.google.firebase:firebase-database-ktx:20.0.1"
api 'com.google.firebase:firebase-auth-ktx:21.0.1'
api 'com.google.firebase:firebase-messaging-ktx:23.0.7'
api 'com.google.firebase:firebase-analytics-ktx:21.1.0'
api "com.google.firebase:firebase-common-ktx:20.1.1"
api "com.google.firebase:firebase-database-ktx:20.0.5"
api 'com.google.firebase:firebase-auth-ktx:21.0.7'

//emoji
api 'com.vanniktech:emoji-google:0.7.0'

// balloon
api "com.github.skydoves:balloon:1.3.6"

def room_version = "2.3.0"
def room_version = "2.4.3"
api "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
api "androidx.room:room-rxjava2:$room_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,9 @@ package com.desk360.base.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

abstract class ConnectivityBroadcastReceiver : BroadcastReceiver() {
companion object {

fun registerToActivityAndAutoUnregister(
activity: AppCompatActivity,
connectionBroadcastReceiver: ConnectivityBroadcastReceiver
) {
activity.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
activity.unregisterReceiver(connectionBroadcastReceiver)
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
activity.registerReceiver(
connectionBroadcastReceiver,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
})
}
}

override fun onReceive(context: Context, intent: Intent) {
val hasConnection =
Expand Down
23 changes: 23 additions & 0 deletions livechat/src/main/java/com/desk360/base/util/ContextExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.desk360.base.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build

fun Context.isNetworkAvailable(): Boolean {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nw = connectivityManager?.activeNetwork ?: return false
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false
return when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
else -> false
}
} else {
return connectivityManager?.activeNetworkInfo?.isConnected ?: false
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.desk360.livechat.presentation.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.*
import android.os.Build
import android.os.Bundle
import android.view.View
Expand All @@ -19,6 +22,7 @@ import com.desk360.base.presentation.component.CustomProgressDialog
import com.desk360.base.presentation.popup.ChatPopup
import com.desk360.base.receiver.ConnectivityBroadcastReceiver
import com.desk360.base.util.Utils
import com.desk360.base.util.isNetworkAvailable
import com.desk360.livechat.BindingExt.binding
import com.desk360.livechat.R
import com.desk360.livechat.manager.LiveChatHelper
Expand All @@ -30,19 +34,47 @@ import java.net.ConnectException
import java.net.UnknownHostException
import java.util.concurrent.TimeUnit

abstract class BaseActivity<VB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity(), LifecycleObserver {
abstract class BaseActivity<VB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity(),
LifecycleObserver {
protected val activityLauncher: BaseActivityResult<Intent, ActivityResult> =
BaseActivityResult.registerActivityForResult(this)

val binding: VB by binding(getLayoutResId())
val viewModel: VM by lazy {
ViewModelProvider(this).get(getViewModelClass())
}

val viewModel: VM by lazy { ViewModelProvider(this).get(getViewModelClass()) }

val compositeDisposable = CompositeDisposable()

val customProgressDialog = CustomProgressDialog()

private val connectivityManager by lazy {
applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
}

private val connectivityCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
runOnUiThread { viewModel.setConnection(true) }
}

override fun onLost(network: Network) {
runOnUiThread { viewModel.setConnection(false) }
}

override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) = Unit

override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) =
Unit
}

private val connectionReceiver = object : ConnectivityBroadcastReceiver() {
override fun onConnectionChanged(hasConnection: Boolean) {
viewModel.setConnection(hasConnection)
}
}

@LayoutRes
abstract fun getLayoutResId(): Int

Expand All @@ -62,7 +94,7 @@ abstract class BaseActivity<VB : ViewDataBinding, VM : BaseViewModel> : AppCompa
initUI()
initObservers()

viewModel.errorMessage.observe(this, { message ->
viewModel.errorMessage.observe(this) { message ->
if (viewModel.error.value is ConnectException || viewModel.error.value is UnknownHostException) {
ChatPopup.Builder(this)
.setStatus(Utils.DialogStatus.CUSTOM)
Expand All @@ -77,29 +109,48 @@ abstract class BaseActivity<VB : ViewDataBinding, VM : BaseViewModel> : AppCompa
dialog.dismiss()
}
.build().show()
})
}

viewModel.isProgressDialogState.observe(this, {
viewModel.isProgressDialogState.observe(this) {
when (it) {
AutoLoginTasksImpl.LoginProgressDialogState.START -> customProgressDialog.show(this,"Connecting...")
AutoLoginTasksImpl.LoginProgressDialogState.START -> customProgressDialog.show(
this,
"Connecting..."
)
AutoLoginTasksImpl.LoginProgressDialogState.STOP -> customProgressDialog.dialog.hideDialog()
else -> {}
}
})
}

viewModel.checkOnlineStatus()
}

override fun onResume() {
super.onResume()
viewModel.setConnection(isNetworkAvailable())
}

private fun registerReceivers() {
ConnectivityBroadcastReceiver.registerToActivityAndAutoUnregister(this, object :
ConnectivityBroadcastReceiver() {
override fun onConnectionChanged(hasConnection: Boolean) {
viewModel.setConnection(hasConnection)
}
})
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager?.registerDefaultNetworkCallback(connectivityCallback)
} else {
registerReceiver(
connectionReceiver,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
}

private fun unRegisterReceivers() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager?.unregisterNetworkCallback(connectivityCallback)
} else {
unregisterReceiver(connectionReceiver)
}
}

override fun onDestroy() {
unRegisterReceivers()
compositeDisposable.dispose()
super.onDestroy()
}
Expand Down

0 comments on commit 8e5d0c0

Please sign in to comment.