Skip to content

Commit

Permalink
chore: code cleanup and minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Jul 16, 2024
1 parent 9ddcb19 commit 0b840f0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:

jobs:
test:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<application
Expand All @@ -12,8 +11,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UnleashAndroid"
tools:targetApi="21">
android:theme="@style/Theme.UnleashAndroid">

<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MainActivity : ComponentActivity() {
Log.i("MAIN","MainActivity.onCreate | lifecycle ${lifecycle.currentState}")

setContent {
unleashAndroidTheme {
UnleashAndroidTheme {
var userId by remember { mutableStateOf(initialUserId) }
val flag by isEnabledViewModel.flagName.observeAsState()
if (!initialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.res.colorResource

@Composable
fun unleashAndroidTheme(content: @Composable () -> Unit) {
fun UnleashAndroidTheme(content: @Composable () -> Unit) {

MaterialTheme(
colorScheme = MaterialTheme.colorScheme.copy(
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ agp = "8.4.2"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
androidxTestExt = "1.1.5"
espressoCore = "3.5.1"
androidxTestExt = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
material = "1.12.0"
work = "2.9.0"
okhttp = "4.12.0"
jackson = "2.17.1"
assertj = "3.26.0"
lifecycleProcess = "2.8.2"
kotlinxCoroutinesTest = "1.7.1"
activityCompose = "1.8.0"
lifecycleProcess = "2.8.3"
kotlinxCoroutinesTest = "1.7.3"
activityCompose = "1.9.0"
runtimeLivedata = "1.6.8"
material3 = "1.0.0"
material3 = "1.2.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import io.getunleash.android.cache.InMemoryToggleCache
import io.getunleash.android.cache.ObservableCache
import io.getunleash.android.cache.ObservableToggleCache
import io.getunleash.android.cache.ToggleCache
import io.getunleash.android.data.DataStrategy
import io.getunleash.android.data.UnleashContext
import io.getunleash.android.data.Variant
import io.getunleash.android.events.UnleashEventListener
import io.getunleash.android.http.ClientBuilder
import io.getunleash.android.http.NetworkStatusHelper
import io.getunleash.android.metrics.MetricsCollector
import io.getunleash.android.metrics.MetricsSender
Expand All @@ -34,10 +34,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.internal.toImmutableList
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicBoolean

Expand Down Expand Up @@ -69,17 +66,18 @@ class DefaultUnleash(
private val networkStatusHelper = NetworkStatusHelper(androidContext)

init {
val httpClientBuilder = ClientBuilder(unleashConfig, androidContext)
val metricsSender =
if (unleashConfig.metricsStrategy.enabled)
MetricsSender(
unleashConfig,
buildHttpClient("metrics", androidContext, unleashConfig.metricsStrategy)
httpClientBuilder.build("metrics", unleashConfig.metricsStrategy)
)
else NoOpMetrics()
fetcher = if (unleashConfig.pollingStrategy.enabled)
UnleashFetcher(
unleashConfig,
buildHttpClient("poller", androidContext, unleashConfig.pollingStrategy),
httpClientBuilder.build("poller", unleashConfig.pollingStrategy),
unleashContextState.asStateFlow()
) else null
metrics = metricsSender
Expand Down Expand Up @@ -146,27 +144,6 @@ class DefaultUnleash(
return localBackup
}

private fun buildHttpClient(
clientName: String,
androidContext: Context,
strategy: DataStrategy
): OkHttpClient {
return OkHttpClient.Builder()
.readTimeout(strategy.httpReadTimeout, TimeUnit.MILLISECONDS)
.connectTimeout(strategy.httpConnectionTimeout, TimeUnit.MILLISECONDS)
.cache(
Cache(
directory = CacheDirectoryProvider(
unleashConfig.localStorageConfig,
androidContext
).getCacheDirectory(
"unleash_${clientName}_http_cache", true
),
maxSize = strategy.httpCacheSize
)
).build()
}

override fun isEnabled(toggleName: String, defaultValue: Boolean): Boolean {
val enabled = cache.get(toggleName)?.enabled ?: defaultValue
metrics.count(toggleName, enabled)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.getunleash.android.http

import android.content.Context
import io.getunleash.android.UnleashConfig
import io.getunleash.android.cache.CacheDirectoryProvider
import io.getunleash.android.data.DataStrategy
import okhttp3.Cache
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

class ClientBuilder(private val unleashConfig: UnleashConfig, private val androidContext: Context) {
fun build(
clientName: String,
strategy: DataStrategy
): OkHttpClient {
return OkHttpClient.Builder()
.readTimeout(strategy.httpReadTimeout, TimeUnit.MILLISECONDS)
.connectTimeout(strategy.httpConnectionTimeout, TimeUnit.MILLISECONDS)
.cache(
Cache(
directory = CacheDirectoryProvider(
unleashConfig.localStorageConfig,
androidContext
).getCacheDirectory(
"unleash_${clientName}_http_cache", true
),
maxSize = strategy.httpCacheSize
)
).build()
}
}

0 comments on commit 0b840f0

Please sign in to comment.