mirror of
https://github.com/YTVanced/VancedManager
synced 2024-12-22 16:30:00 +00:00
improvement/library-network create library-network and add base functional
This commit is contained in:
parent
feeaadc497
commit
81607b3b2c
12 changed files with 229 additions and 3 deletions
|
@ -52,6 +52,11 @@ android {
|
|||
viewBinding true
|
||||
}
|
||||
|
||||
packagingOptions {
|
||||
exclude 'META-INF/DEPENDENCIES'
|
||||
exclude "META-INF/*.kotlin_module"
|
||||
}
|
||||
|
||||
// To inline the bytecode built with JVM target 1.8 into
|
||||
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)
|
||||
|
||||
|
@ -101,6 +106,7 @@ dependencies {
|
|||
implementation project(':core-presentation')
|
||||
implementation project(":core-ui")
|
||||
|
||||
implementation project(':library-network')
|
||||
// Kotlin
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
|
|
1
library-network/.gitignore
vendored
Normal file
1
library-network/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/build
|
55
library-network/build.gradle
Normal file
55
library-network/build.gradle
Normal file
|
@ -0,0 +1,55 @@
|
|||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'kotlin-android'
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "30.0.2"
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 30
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'androidx.core:core-ktx:1.3.2'
|
||||
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
|
||||
|
||||
implementation "com.squareup.moshi:moshi-kotlin:1.11.0"
|
||||
implementation "com.squareup.moshi:moshi-kotlin-codegen:1.11.0"
|
||||
implementation "com.squareup.moshi:moshi-adapters:1.11.0"
|
||||
|
||||
|
||||
|
||||
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
|
||||
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"
|
||||
|
||||
implementation "org.koin:koin-android:2.2.1"
|
||||
implementation "org.koin:koin-android-viewmodel:2.2.1"
|
||||
implementation "org.koin:koin-android-ext:2.2.1"
|
||||
}
|
6
library-network/src/main/AndroidManifest.xml
Normal file
6
library-network/src/main/AndroidManifest.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.vanced.manager.library.network">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
</manifest>
|
|
@ -0,0 +1,48 @@
|
|||
package com.vanced.manager.library.network.di
|
||||
|
||||
import com.vanced.manager.library.network.okhttp.interceptors.LOG_INTERCEPTOR
|
||||
import com.vanced.manager.library.network.okhttp.interceptors.NO_CONNECT_INTERCEPTOR
|
||||
import com.vanced.manager.library.network.okhttp.interceptors.loggingInterceptor
|
||||
import com.vanced.manager.library.network.okhttp.interceptors.noConnectionInterceptor
|
||||
import com.vanced.manager.library.network.providers.provideMoshi
|
||||
import com.vanced.manager.library.network.providers.provideOkHttpClient
|
||||
import com.vanced.manager.library.network.providers.provideRetrofit
|
||||
import org.koin.android.BuildConfig
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.core.qualifier.named
|
||||
import org.koin.dsl.module
|
||||
|
||||
|
||||
const val ORIGINAL = "ORIGINAL"
|
||||
|
||||
val NetworkModule = module {
|
||||
factory(named(LOG_INTERCEPTOR)) { loggingInterceptor() }
|
||||
factory(named(NO_CONNECT_INTERCEPTOR)) { noConnectionInterceptor(androidContext()) }
|
||||
|
||||
factory(named(ORIGINAL)) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
provideOkHttpClient(
|
||||
interceptors = listOf(
|
||||
get(named(LOG_INTERCEPTOR)),
|
||||
get(named(NO_CONNECT_INTERCEPTOR))
|
||||
)
|
||||
)
|
||||
} else {
|
||||
provideOkHttpClient(
|
||||
interceptors = listOf(
|
||||
get(named(NO_CONNECT_INTERCEPTOR))
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
factory { provideMoshi() }
|
||||
|
||||
factory(named(ORIGINAL)) {
|
||||
provideRetrofit(
|
||||
moshi = get(),
|
||||
okHttpClient = get(named(ORIGINAL)),
|
||||
url = "https://www.haliksar.fun"
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.vanced.manager.library.network.okhttp.interceptors
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import okhttp3.logging.HttpLoggingInterceptor.Level
|
||||
|
||||
const val LOG_INTERCEPTOR = "LOG_INTERCEPTOR"
|
||||
|
||||
internal fun loggingInterceptor(): Interceptor =
|
||||
HttpLoggingInterceptor().setLevel(Level.BODY)
|
|
@ -0,0 +1,42 @@
|
|||
package com.vanced.manager.library.network.okhttp.interceptors
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.os.Build
|
||||
import okhttp3.Interceptor
|
||||
|
||||
class NoConnectException : Exception()
|
||||
|
||||
const val NO_CONNECT_INTERCEPTOR = "NO_CONNECT_INTERCEPTOR"
|
||||
|
||||
internal fun noConnectionInterceptor(context: Context) = Interceptor { chain ->
|
||||
if (!isInternetAvailable(context)) {
|
||||
throw NoConnectException()
|
||||
} else {
|
||||
chain.proceed(chain.request())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun isInternetAvailable(context: Context): Boolean {
|
||||
with(context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
val networkCapabilities = activeNetwork ?: return false
|
||||
val actNw = getNetworkCapabilities(networkCapabilities) ?: return false
|
||||
when {
|
||||
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
|
||||
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
|
||||
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
|
||||
else -> false
|
||||
}
|
||||
} else {
|
||||
when (activeNetworkInfo?.type) {
|
||||
ConnectivityManager.TYPE_WIFI -> true
|
||||
ConnectivityManager.TYPE_MOBILE -> true
|
||||
ConnectivityManager.TYPE_ETHERNET -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.vanced.manager.library.network.providers
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import java.util.*
|
||||
|
||||
internal fun provideMoshi(): Moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||
.build()
|
|
@ -0,0 +1,24 @@
|
|||
package com.vanced.manager.library.network.providers
|
||||
|
||||
import okhttp3.Authenticator
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
private const val WaitTime = 0L
|
||||
private const val ConnTime = 0L
|
||||
|
||||
fun provideOkHttpClient(
|
||||
interceptors: List<Interceptor> = emptyList(),
|
||||
authenticators: List<Authenticator> = emptyList()
|
||||
): OkHttpClient =
|
||||
OkHttpClient.Builder()
|
||||
.callTimeout(WaitTime, TimeUnit.SECONDS)
|
||||
.readTimeout(WaitTime, TimeUnit.MILLISECONDS)
|
||||
.connectTimeout(ConnTime, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(WaitTime, TimeUnit.MILLISECONDS)
|
||||
.apply {
|
||||
interceptors.forEach { addInterceptor(it) }
|
||||
authenticators.forEach { authenticator(it) }
|
||||
}
|
||||
.build()
|
|
@ -0,0 +1,16 @@
|
|||
package com.vanced.manager.library.network.providers
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
|
||||
fun provideRetrofit(
|
||||
okHttpClient: OkHttpClient,
|
||||
moshi: Moshi,
|
||||
url: String
|
||||
): Retrofit = Retrofit.Builder()
|
||||
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
||||
.client(okHttpClient)
|
||||
.baseUrl(url)
|
||||
.build()
|
|
@ -0,0 +1,6 @@
|
|||
package com.vanced.manager.library.network.service
|
||||
|
||||
import retrofit2.Retrofit
|
||||
|
||||
inline fun <reified T> createRetrofitService(retrofit: Retrofit): T =
|
||||
retrofit.create(T::class.java)
|
|
@ -1,7 +1,8 @@
|
|||
rootProject.name='Vanced Manager'
|
||||
include ':app'
|
||||
|
||||
include ':core-presentation'
|
||||
include ':core-ui'
|
||||
include ':core-presentation', ':core-ui'
|
||||
|
||||
include ':feature-home'
|
||||
include ':feature-home'
|
||||
|
||||
include ':library-network'
|
||||
|
|
Loading…
Reference in a new issue