Skip to content

Commit

Permalink
Merge pull request #11 from stslex/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
stslex committed Aug 7, 2023
2 parents 9044348 + f12b5c8 commit 891fa94
Show file tree
Hide file tree
Showing 36 changed files with 602 additions and 271 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/android_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
branches: [ master ]
workflow_dispatch:

permissions:
checks: write
pull-requests: write

jobs:
build:

Expand Down Expand Up @@ -37,4 +41,10 @@ jobs:
run: ./gradlew build

- name: Junit tests with Gradle
run: ./gradlew testDebugUnitTest
run: ./gradlew testDebugUnitTest

- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: "**/build/test-results/**/*.xml"
11 changes: 10 additions & 1 deletion app/src/main/java/com/stslex/aproselection/SelectApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package com.stslex.aproselection

import android.app.Application
import com.stslex.aproselection.controller.AuthController
import com.stslex.aproselection.core.datastore.AppDataStore
import com.stslex.aproselection.core.datastore.coreDataStoreModule
import com.stslex.aproselection.core.network.di.ModuleCoreNetwork.moduleCoreNetwork
import com.stslex.aproselection.core.user.di.moduleCoreUser
import com.stslex.aproselection.di.appModule
import com.stslex.aproselection.feature.auth.di.ModuleFeatureAuth.moduleFeatureAuth
import com.stslex.aproselection.feature.home.di.moduleFeatureHome
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class SelectApplication : Application() {

private val coroutineScope = CoroutineScope(SupervisorJob())
private val appController: AuthController by inject()
private val dataStore: AppDataStore by inject()

override fun onCreate() {
super.onCreate()
Expand All @@ -24,7 +30,10 @@ class SelectApplication : Application() {
}

private fun initControllers() {
appController.init()
coroutineScope.launch {
appController.init()
dataStore.init()
}
}

private fun setupDependencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ interface AuthController {

val isAuth: StateFlow<Boolean?>

fun init()
suspend fun init()
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,36 @@ package com.stslex.aproselection.controller

import com.stslex.aproselection.core.datastore.AppDataStore
import com.stslex.aproselection.core.network.client.NetworkClient
import com.stslex.aproselection.core.core.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlin.coroutines.coroutineContext

class AuthControllerImpl(
private val dataStore: AppDataStore,
private val networkClient: NetworkClient,
) : AuthController {

private val scope = CoroutineScope(SupervisorJob())
private val _isAuth = MutableStateFlow<Boolean?>(null)
override val isAuth: StateFlow<Boolean?>
get() = _isAuth.asStateFlow()

override fun init() {
override suspend fun init() {
dataStore.token
.catch { error ->
Logger.exception(error)
}
.onEach { token ->
if (token.isBlank()) {
networkClient.regenerateToken()
}
}
.launchIn(scope)
.launchIn(CoroutineScope(coroutineContext))

dataStore.uuid
.catch { error ->
Logger.exception(error)
}
.onEach { uuid ->
_isAuth.emit(uuid.isNotBlank())
}
.launchIn(scope)
.launchIn(CoroutineScope(coroutineContext))
}
}
16 changes: 8 additions & 8 deletions app/src/test/java/com/stslex/aproselection/DiKoinModuleTest.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.stslex.aproselection

import android.content.Context
import androidx.navigation.NavHostController
import com.stslex.aproselection.core.datastore.coreDataStoreModule
import com.stslex.aproselection.core.navigation.di.moduleCoreNavigation
import com.stslex.aproselection.core.network.di.ModuleCoreNetwork.moduleCoreNetwork
import com.stslex.aproselection.core.navigation.destination.NavigationScreen
import com.stslex.aproselection.core.user.di.moduleCoreUser
import com.stslex.aproselection.di.appModule
import com.stslex.aproselection.di.initialAppModule
import com.stslex.aproselection.feature.auth.di.ModuleFeatureAuth.moduleFeatureAuth
import com.stslex.aproselection.feature.home.di.moduleFeatureHome
import org.junit.Test
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.mockito.Mockito
Expand All @@ -19,19 +21,17 @@ class DiKoinModuleTest : KoinTest {

@Test
fun checkKoinModules() {
val navigator: (screen: com.stslex.aproselection.core.navigation.destination.NavigationScreen) -> Unit = {}
val navController = Mockito.mock(NavHostController::class.java)

koinApplication {

androidContext(Mockito.mock(Context::class.java))
modules(
module {
single {
navigator
}
},
moduleCoreNavigation(navController),
initialAppModule,
appModule,
moduleCoreNetwork,
moduleCoreUser,
coreDataStoreModule,
moduleFeatureAuth,
moduleFeatureHome,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.stslex.aproselection.core.datastore

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

interface AppDataStore {

val uuid: Flow<String>
val token: Flow<String>
val uuid: StateFlow<String>
val token: StateFlow<String>

suspend fun setUuid(uuid: String)

suspend fun setToken(token: String)

suspend fun clear()

suspend fun init()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.stslex.aproselection.core.core.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlin.coroutines.coroutineContext

class AppDataStoreImpl(
private val context: Context
Expand All @@ -21,19 +28,13 @@ class AppDataStoreImpl(
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(DATA_STORE_KEY)
}

override val uuid: Flow<String>
get() = context.dataStore.data
.map { prefs ->
prefs[UUID_KEY].orEmpty()
}
.flowOn(Dispatchers.IO)
private val _uuid = MutableStateFlow("")
override val uuid: StateFlow<String>
get() = _uuid.asStateFlow()

override val token: Flow<String>
get() = context.dataStore.data
.map { prefs ->
prefs[TOKEN_KEY].orEmpty()
}
.flowOn(Dispatchers.IO)
private val _token = MutableStateFlow("")
override val token: StateFlow<String>
get() = _token.asStateFlow()

override suspend fun setUuid(uuid: String) {
context.dataStore.updateData { prefs ->
Expand All @@ -51,6 +52,19 @@ class AppDataStoreImpl(
}
}

override suspend fun init() {
context.dataStore.data
.catch { error ->
Logger.exception(error)
}
.onEach { prefs ->
_uuid.value = prefs[UUID_KEY].orEmpty()
_token.value = prefs[TOKEN_KEY].orEmpty()
}
.flowOn(Dispatchers.IO)
.launchIn(CoroutineScope(coroutineContext))
}

override suspend fun clear() {
setUuid("")
setToken("")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.stslex.aproselection.core.datastore

import android.content.Context
import org.junit.Test
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.koinApplication
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.mockito.Mockito

class CoreDataStoreModuleTest : KoinTest {

@Test
fun checkKoinModules() {

koinApplication {
androidContext(Mockito.mock(Context::class.java))
modules(coreDataStoreModule)
checkModules()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.stslex.aproselection.core.navigation

import android.content.Context
import androidx.navigation.NavHostController
import com.stslex.aproselection.core.navigation.di.moduleCoreNavigation
import org.junit.Test
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.koinApplication
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.mockito.Mockito

class CoreNavigationModuleTest : KoinTest {

@Test
fun checkKoinModules() {
val navController = Mockito.mock(NavHostController::class.java)

koinApplication {
androidContext(Mockito.mock(Context::class.java))
modules(moduleCoreNavigation(navController))
checkModules()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import io.ktor.client.HttpClient

interface NetworkClient {

val client: HttpClient

val apiClient: HttpClient

suspend fun regenerateToken()
suspend fun regenerateToken(): String
}
Loading

0 comments on commit 891fa94

Please sign in to comment.