From dc10cafb1bf6cd11efe245671fe023ee322774d5 Mon Sep 17 00:00:00 2001 From: davide Date: Wed, 20 Jul 2022 13:43:00 +0200 Subject: [PATCH] Use the repository for the login --- .../android/LoginActivity.kt | 40 ++++------- .../apps/readerforselfossv2/android/MyApp.kt | 8 ++- .../apps/readerforselfossv2/DI/modules.kt | 15 +++++ .../repository/Repository.kt | 5 +- .../repository/RepositoryImpl.kt | 25 ++++--- .../readerforselfossv2/rest/SelfossApiImpl.kt | 66 +++++++++++-------- 6 files changed, 89 insertions(+), 70 deletions(-) create mode 100644 shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/DI/modules.kt diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt index 519b758..e78f2e1 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt @@ -4,28 +4,27 @@ import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.content.Intent import android.os.Bundle -import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity import android.text.TextUtils -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View import android.view.inputmethod.EditorInfo import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity import bou.amine.apps.readerforselfossv2.android.databinding.ActivityLoginBinding import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable -import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl -import bou.amine.apps.readerforselfossv2.service.ApiDetailsService +import bou.amine.apps.readerforselfossv2.repository.Repository import com.mikepenz.aboutlibraries.LibsBuilder import com.russhwolf.settings.Settings import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import org.kodein.di.* +import org.kodein.di.DIAware import org.kodein.di.android.closestDI +import org.kodein.di.instance class LoginActivity() : AppCompatActivity(), DIAware { @@ -40,7 +39,7 @@ class LoginActivity() : AppCompatActivity(), DIAware { private lateinit var binding: ActivityLoginBinding override val di by closestDI() - private val apiDetailsService : ApiDetailsService by instance() + private val repository : Repository by instance() override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@LoginActivity) @@ -210,30 +209,17 @@ class LoginActivity() : AppCompatActivity(), DIAware { settings.putString("password", password) settings.putString("httpPassword", httpPassword) settings.putBoolean("isSelfSignedCert", isWithSelfSignedCert) - apiDetailsService.refresh() - - val api = SelfossApiImpl( -// this, -// this@LoginActivity, -// isWithSelfSignedCert, -// -1L - apiDetailsService - ) + repository.refreshLoginInformation() if (this@LoginActivity.isNetworkAvailable(this@LoginActivity.findViewById(R.id.loginForm))) { CoroutineScope(Dispatchers.IO).launch { - try { - val result = api.login() - if (result != null && result.isSuccess) { - goToMain() - } else { - CoroutineScope(Dispatchers.Main).launch { - preferenceError(Exception("Not success")) - } + val result = repository.login() + if (result) { + goToMain() + } else { + CoroutineScope(Dispatchers.Main).launch { + preferenceError(Exception("Not success")) } - } catch (cause: Throwable) { - Log.e("1", cause.message!!) - Log.e("1", cause.stackTraceToString()) } } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt index 65bc23b..c9b2052 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt @@ -9,10 +9,11 @@ import android.os.Build import android.widget.ImageView import androidx.multidex.MultiDexApplication import androidx.preference.PreferenceManager +import bou.amine.apps.readerforselfossv2.DI.networkModule import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth -import bou.amine.apps.readerforselfossv2.service.ApiDetailsService -import bou.amine.apps.readerforselfossv2.service.ApiDetailsServiceImpl +import bou.amine.apps.readerforselfossv2.repository.Repository +import bou.amine.apps.readerforselfossv2.repository.RepositoryImpl import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions import com.ftinc.scoop.Scoop @@ -25,7 +26,8 @@ import java.util.UUID.randomUUID class MyApp : MultiDexApplication(), DIAware { override val di by DI.lazy { - bind() with singleton { ApiDetailsServiceImpl() } + import(networkModule) + bind() with singleton { RepositoryImpl(instance()) } } private lateinit var config: Config diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/DI/modules.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/DI/modules.kt new file mode 100644 index 0000000..c18e33d --- /dev/null +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/DI/modules.kt @@ -0,0 +1,15 @@ +package bou.amine.apps.readerforselfossv2.DI + +import bou.amine.apps.readerforselfossv2.rest.SelfossApi +import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl +import bou.amine.apps.readerforselfossv2.service.ApiDetailsService +import bou.amine.apps.readerforselfossv2.service.ApiDetailsServiceImpl +import org.kodein.di.DI +import org.kodein.di.bind +import org.kodein.di.instance +import org.kodein.di.singleton + +val networkModule by DI.Module { + bind() with singleton { ApiDetailsServiceImpl() } + bind() with singleton { SelfossApiImpl(instance()) } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index 36d795d..d9ad93b 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -9,7 +9,6 @@ interface Repository { var selectedItems: List // API - fun getItems(): List fun getMoreItems(): List fun stats(): SelfossModel.Stats fun getTags(): List @@ -27,6 +26,6 @@ interface Repository { filter: String): Boolean fun deleteSource(id: Int): Boolean fun updateRemote(): Boolean - fun login(): Boolean - fun refreshLoginInformation(): Boolean + suspend fun login(): Boolean + fun refreshLoginInformation() } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index d1b5a5e..3432dde 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -3,17 +3,14 @@ package bou.amine.apps.readerforselfossv2.repository import bou.amine.apps.readerforselfossv2.rest.SelfossApi import bou.amine.apps.readerforselfossv2.rest.SelfossModel import com.russhwolf.settings.Settings +import io.github.aakira.napier.Napier -class RepositoryImpl(api: SelfossApi) : Repository { +class RepositoryImpl(private val api: SelfossApi) : Repository { val settings = Settings() override lateinit var items: List override lateinit var selectedItems: List - override fun getItems(): List { - return items - } - override fun getMoreItems(): List { TODO("Not yet implemented") } @@ -72,11 +69,21 @@ class RepositoryImpl(api: SelfossApi) : Repository { TODO("Not yet implemented") } - override fun login(): Boolean { - TODO("Not yet implemented") + override suspend fun login(): Boolean { + var result = false + try { + val response = api.login() + if (response != null && response.isSuccess) { + result = true + } + } catch (cause: Throwable) { + Napier.e(cause.message!!, tag = "1") + Napier.e(cause.stackTraceToString(),tag = "1") + } + return result } - override fun refreshLoginInformation(): Boolean { - TODO("Not yet implemented") + override fun refreshLoginInformation() { + api.refreshLoginInformation() } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApiImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApiImpl.kt index d01fc94..1bfa932 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApiImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApiImpl.kt @@ -16,6 +16,8 @@ interface SelfossApi { val client: HttpClient fun url(path: String): String + fun refreshLoginInformation() + suspend fun login(): SelfossModel.SuccessResponse? suspend fun getItems( @@ -64,43 +66,51 @@ interface SelfossApi { class SelfossApiImpl(private val apiDetailsService: ApiDetailsService) : SelfossApi { - override val client = HttpClient() { - install(ContentNegotiation) { - install(HttpCache) - json(Json { - prettyPrint = true - isLenient = true - ignoreUnknownKeys = true - }) - } - install(Logging) { - logger = object: Logger { - override fun log(message: String) { - apiDetailsService.logApiCalls(message) - } - } - level = LogLevel.ALL - } - /* TODO: Auth as basic - if (apiDetailsService.getUserName().isNotEmpty() && apiDetailsService.getPassword().isNotEmpty()) { + override var client = createHttpClient() - install(Auth) { - basic { - credentials { - BasicAuthCredentials(username = apiDetailsService.getUserName(), password = apiDetailsService.getPassword()) - } - sendWithoutRequest { - true + private fun createHttpClient(): HttpClient { + return HttpClient { + install(ContentNegotiation) { + install(HttpCache) + json(Json { + prettyPrint = true + isLenient = true + ignoreUnknownKeys = true + }) + } + install(Logging) { + logger = object : Logger { + override fun log(message: String) { + apiDetailsService.logApiCalls(message) } } + level = LogLevel.ALL } - }*/ - expectSuccess = false + /* TODO: Auth as basic + if (apiDetailsService.getUserName().isNotEmpty() && apiDetailsService.getPassword().isNotEmpty()) { + + install(Auth) { + basic { + credentials { + BasicAuthCredentials(username = apiDetailsService.getUserName(), password = apiDetailsService.getPassword()) + } + sendWithoutRequest { + true + } + } + } + }*/ + expectSuccess = false + } } override fun url(path: String) = "${apiDetailsService.getBaseUrl()}$path" + override fun refreshLoginInformation() { + apiDetailsService.refresh() + client = createHttpClient() + } override suspend fun login(): SelfossModel.SuccessResponse? = client.get(url("/login")) {