app/src/main/java/com/geekorum/ttrss/accounts/LoginViewModel.kt
changeset 109 eb12523cbc81
parent 108 849c50d5d2b3
child 137 5464f07a306c
equal deleted inserted replaced
108:849c50d5d2b3 109:eb12523cbc81
    32 import com.geekorum.ttrss.R
    32 import com.geekorum.ttrss.R
    33 import com.geekorum.ttrss.network.ApiCallException
    33 import com.geekorum.ttrss.network.ApiCallException
    34 import com.geekorum.ttrss.network.impl.LoginRequestPayload
    34 import com.geekorum.ttrss.network.impl.LoginRequestPayload
    35 import kotlinx.coroutines.launch
    35 import kotlinx.coroutines.launch
    36 import okhttp3.HttpUrl
    36 import okhttp3.HttpUrl
       
    37 import retrofit2.HttpException
    37 import javax.inject.Inject
    38 import javax.inject.Inject
    38 
    39 
    39 /**
    40 /**
    40  * ViewModel for LoginActivity
    41  * ViewModel for LoginActivity
    41  */
    42  */
   122         return handleAction
   123         return handleAction
   123     }
   124     }
   124 
   125 
   125     @VisibleForTesting
   126     @VisibleForTesting
   126     internal suspend fun doLogin() {
   127     internal suspend fun doLogin() {
   127         val serverInformation = DataServerInformation(httpUrl!!.toString())
   128         val serverInformation = DataServerInformation(httpUrl!!.toString(), http_auth_username, http_auth_password)
   128         val urlModule = TinyRssServerInformationModule(serverInformation)
   129         val urlModule = TinyRssServerInformationModule(serverInformation)
   129         val networkComponent = networkComponentBuilder
   130         val networkComponent = networkComponentBuilder
   130             .tinyRssServerInformationModule(urlModule)
   131             .tinyRssServerInformationModule(urlModule)
   131             .build()
   132             .build()
   132         val tinyRssApi = networkComponent.getTinyRssApi()
   133         val tinyRssApi = networkComponent.getTinyRssApi()
   144                     loginFailedEvent.value = LoginFailedEvent(R.string.error_login_failed)
   145                     loginFailedEvent.value = LoginFailedEvent(R.string.error_login_failed)
   145 
   146 
   146                 response.error == ApiCallException.ApiError.API_DISABLED ->
   147                 response.error == ApiCallException.ApiError.API_DISABLED ->
   147                     loginFailedEvent.value = LoginFailedEvent(R.string.error_api_disabled)
   148                     loginFailedEvent.value = LoginFailedEvent(R.string.error_api_disabled)
   148 
   149 
       
   150                 else -> loginFailedEvent.value = LoginFailedEvent(R.string.error_unknown)
       
   151             }
       
   152         } catch (e: HttpException) {
       
   153             when (e.code()) {
       
   154                 401 -> loginFailedEvent.value = LoginFailedEvent(R.string.error_http_unauthorized)
       
   155                 403 -> loginFailedEvent.value = LoginFailedEvent(R.string.error_http_forbidden)
       
   156                 404 -> loginFailedEvent.value = LoginFailedEvent(R.string.error_http_not_found)
   149                 else -> loginFailedEvent.value = LoginFailedEvent(R.string.error_unknown)
   157                 else -> loginFailedEvent.value = LoginFailedEvent(R.string.error_unknown)
   150             }
   158             }
   151         } catch (e: Exception) {
   159         } catch (e: Exception) {
   152             loginFailedEvent.value = LoginFailedEvent(R.string.error_unknown)
   160             loginFailedEvent.value = LoginFailedEvent(R.string.error_unknown)
   153         } finally {
   161         } finally {