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.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())
}
}
}

View File

@ -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<ApiDetailsService>() with singleton { ApiDetailsServiceImpl() }
import(networkModule)
bind<Repository>() with singleton { RepositoryImpl(instance()) }
}
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>
// API
fun getItems(): List<SelfossModel.Item>
fun getMoreItems(): List<SelfossModel.Item>
fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag>
@ -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()
}

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.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<SelfossModel.Item>
override lateinit var selectedItems: List<SelfossModel.Item>
override fun getItems(): List<SelfossModel.Item> {
return items
}
override fun getMoreItems(): List<SelfossModel.Item> {
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()
}
}

View File

@ -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")) {