Use the repository for the login

This commit is contained in:
davide 2022-07-20 13:43:00 +02:00
parent 3be942a807
commit dc10cafb1b
6 changed files with 89 additions and 70 deletions

View File

@ -4,28 +4,27 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter import android.animation.AnimatorListenerAdapter
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.text.TextUtils import android.text.TextUtils
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.widget.TextView 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.databinding.ActivityLoginBinding
import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.AppColors
import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid
import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable
import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import com.mikepenz.aboutlibraries.LibsBuilder import com.mikepenz.aboutlibraries.LibsBuilder
import com.russhwolf.settings.Settings import com.russhwolf.settings.Settings
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.kodein.di.* import org.kodein.di.DIAware
import org.kodein.di.android.closestDI import org.kodein.di.android.closestDI
import org.kodein.di.instance
class LoginActivity() : AppCompatActivity(), DIAware { class LoginActivity() : AppCompatActivity(), DIAware {
@ -40,7 +39,7 @@ class LoginActivity() : AppCompatActivity(), DIAware {
private lateinit var binding: ActivityLoginBinding private lateinit var binding: ActivityLoginBinding
override val di by closestDI() override val di by closestDI()
private val apiDetailsService : ApiDetailsService by instance() private val repository : Repository by instance()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
appColors = AppColors(this@LoginActivity) appColors = AppColors(this@LoginActivity)
@ -210,31 +209,18 @@ class LoginActivity() : AppCompatActivity(), DIAware {
settings.putString("password", password) settings.putString("password", password)
settings.putString("httpPassword", httpPassword) settings.putString("httpPassword", httpPassword)
settings.putBoolean("isSelfSignedCert", isWithSelfSignedCert) settings.putBoolean("isSelfSignedCert", isWithSelfSignedCert)
apiDetailsService.refresh() repository.refreshLoginInformation()
val api = SelfossApiImpl(
// this,
// this@LoginActivity,
// isWithSelfSignedCert,
// -1L
apiDetailsService
)
if (this@LoginActivity.isNetworkAvailable(this@LoginActivity.findViewById(R.id.loginForm))) { if (this@LoginActivity.isNetworkAvailable(this@LoginActivity.findViewById(R.id.loginForm))) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { val result = repository.login()
val result = api.login() if (result) {
if (result != null && result.isSuccess) {
goToMain() goToMain()
} else { } else {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
preferenceError(Exception("Not success")) preferenceError(Exception("Not success"))
} }
} }
} catch (cause: Throwable) {
Log.e("1", cause.message!!)
Log.e("1", cause.stackTraceToString())
}
} }
} }
showProgress(false) showProgress(false)

View File

@ -9,10 +9,11 @@ import android.os.Build
import android.widget.ImageView import android.widget.ImageView
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
import androidx.preference.PreferenceManager 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.Config
import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.service.ApiDetailsServiceImpl import bou.amine.apps.readerforselfossv2.repository.RepositoryImpl
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.ftinc.scoop.Scoop import com.ftinc.scoop.Scoop
@ -25,7 +26,8 @@ import java.util.UUID.randomUUID
class MyApp : MultiDexApplication(), DIAware { class MyApp : MultiDexApplication(), DIAware {
override val di by DI.lazy { override val di by DI.lazy {
bind<ApiDetailsService>() with singleton { ApiDetailsServiceImpl() } import(networkModule)
bind<Repository>() with singleton { RepositoryImpl(instance()) }
} }
private lateinit var config: Config private lateinit var config: Config

View File

@ -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<ApiDetailsService>() with singleton { ApiDetailsServiceImpl() }
bind<SelfossApi>() with singleton { SelfossApiImpl(instance()) }
}

View File

@ -9,7 +9,6 @@ interface Repository {
var selectedItems: List<SelfossModel.Item> var selectedItems: List<SelfossModel.Item>
// API // API
fun getItems(): List<SelfossModel.Item>
fun getMoreItems(): List<SelfossModel.Item> fun getMoreItems(): List<SelfossModel.Item>
fun stats(): SelfossModel.Stats fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag> fun getTags(): List<SelfossModel.Tag>
@ -27,6 +26,6 @@ interface Repository {
filter: String): Boolean filter: String): Boolean
fun deleteSource(id: Int): Boolean fun deleteSource(id: Int): Boolean
fun updateRemote(): Boolean fun updateRemote(): Boolean
fun login(): Boolean suspend fun login(): Boolean
fun refreshLoginInformation(): Boolean fun refreshLoginInformation()
} }

View File

@ -3,17 +3,14 @@ package bou.amine.apps.readerforselfossv2.repository
import bou.amine.apps.readerforselfossv2.rest.SelfossApi import bou.amine.apps.readerforselfossv2.rest.SelfossApi
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import com.russhwolf.settings.Settings 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() val settings = Settings()
override lateinit var items: List<SelfossModel.Item> override lateinit var items: List<SelfossModel.Item>
override lateinit var selectedItems: List<SelfossModel.Item> override lateinit var selectedItems: List<SelfossModel.Item>
override fun getItems(): List<SelfossModel.Item> {
return items
}
override fun getMoreItems(): List<SelfossModel.Item> { override fun getMoreItems(): List<SelfossModel.Item> {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
@ -72,11 +69,21 @@ class RepositoryImpl(api: SelfossApi) : Repository {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun login(): Boolean { override suspend fun login(): Boolean {
TODO("Not yet implemented") 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 { override fun refreshLoginInformation() {
TODO("Not yet implemented") api.refreshLoginInformation()
} }
} }

View File

@ -16,6 +16,8 @@ interface SelfossApi {
val client: HttpClient val client: HttpClient
fun url(path: String): String fun url(path: String): String
fun refreshLoginInformation()
suspend fun login(): SelfossModel.SuccessResponse? suspend fun login(): SelfossModel.SuccessResponse?
suspend fun getItems( suspend fun getItems(
@ -64,7 +66,10 @@ interface SelfossApi {
class SelfossApiImpl(private val apiDetailsService: ApiDetailsService) : SelfossApi { class SelfossApiImpl(private val apiDetailsService: ApiDetailsService) : SelfossApi {
override val client = HttpClient() { override var client = createHttpClient()
private fun createHttpClient(): HttpClient {
return HttpClient {
install(ContentNegotiation) { install(ContentNegotiation) {
install(HttpCache) install(HttpCache)
json(Json { json(Json {
@ -97,10 +102,15 @@ class SelfossApiImpl(private val apiDetailsService: ApiDetailsService) : Selfoss
}*/ }*/
expectSuccess = false expectSuccess = false
} }
}
override fun url(path: String) = override fun url(path: String) =
"${apiDetailsService.getBaseUrl()}$path" "${apiDetailsService.getBaseUrl()}$path"
override fun refreshLoginInformation() {
apiDetailsService.refresh()
client = createHttpClient()
}
override suspend fun login(): SelfossModel.SuccessResponse? = override suspend fun login(): SelfossModel.SuccessResponse? =
client.get(url("/login")) { client.get(url("/login")) {