| author | Da Risk <da_risk@geekorum.com> |
| Wed, 12 Jan 2022 17:29:59 -0400 | |
| changeset 882 | 7a74abf66c49 |
| parent 846 | ac0863af5ef6 |
| child 941 | dd7a7a2adb1c |
| permissions | -rw-r--r-- |
|
137
5464f07a306c
Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents:
109
diff
changeset
|
1 |
/* |
| 0 | 2 |
* Geekttrss is a RSS feed reader application on the Android Platform. |
3 |
* |
|
| 882 | 4 |
* Copyright (C) 2017-2022 by Frederic-Charles Barthelery. |
| 0 | 5 |
* |
6 |
* This file is part of Geekttrss. |
|
7 |
* |
|
8 |
* Geekttrss is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* Geekttrss is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with Geekttrss. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
|
21 |
package com.geekorum.ttrss.accounts |
|
22 |
||
23 |
import android.accounts.Account |
|
24 |
import android.accounts.AccountManager |
|
25 |
import android.app.Activity |
|
26 |
import android.os.Bundle |
|
27 |
import android.transition.TransitionManager |
|
28 |
import android.view.View |
|
29 |
import android.view.ViewGroup |
|
|
421
859638996c86
LoginActivity: fix a few warnings
Da Risk <da_risk@geekorum.com>
parents:
164
diff
changeset
|
30 |
import androidx.activity.viewModels |
|
164
2d6e9f2063cd
Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
31 |
import androidx.databinding.DataBindingUtil |
|
2d6e9f2063cd
Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
32 |
import androidx.lifecycle.Observer |
| 0 | 33 |
import com.geekorum.geekdroid.accounts.AccountAuthenticatorAppCompatActivity |
34 |
import com.geekorum.geekdroid.app.lifecycle.EventObserver |
|
|
164
2d6e9f2063cd
Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
35 |
import com.geekorum.geekdroid.dagger.DaggerDelegateViewModelsFactory |
| 0 | 36 |
import com.geekorum.ttrss.R |
37 |
import com.geekorum.ttrss.databinding.ActivityLoginAccountBinding |
|
|
164
2d6e9f2063cd
Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
38 |
import com.google.android.material.snackbar.Snackbar |
|
107
3fa8423d4de4
LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents:
62
diff
changeset
|
39 |
import com.google.android.material.textfield.TextInputLayout |
| 734 | 40 |
import dagger.hilt.android.AndroidEntryPoint |
| 0 | 41 |
import javax.inject.Inject |
42 |
||
43 |
/** |
|
44 |
* A Login screen to a Tinytinyrss server. |
|
45 |
*/ |
|
| 734 | 46 |
@AndroidEntryPoint |
| 0 | 47 |
class LoginActivity : AccountAuthenticatorAppCompatActivity() {
|
48 |
||
49 |
companion object {
|
|
50 |
const val ACTION_ADD_ACCOUNT = "add_account" |
|
51 |
const val ACTION_CONFIRM_CREDENTIALS = "confirm_credentials" |
|
52 |
||
53 |
const val EXTRA_ACCOUNT = "account" |
|
54 |
} |
|
55 |
||
56 |
@Inject |
|
57 |
lateinit var accountManager: AndroidTinyrssAccountManager |
|
58 |
||
59 |
private lateinit var binding: ActivityLoginAccountBinding |
|
60 |
||
| 734 | 61 |
private val loginViewModel: LoginViewModel by viewModels() |
| 0 | 62 |
|
63 |
override fun onCreate(savedInstanceState: Bundle?) {
|
|
64 |
super.onCreate(savedInstanceState) |
|
65 |
binding = DataBindingUtil.setContentView(this, R.layout.activity_login_account) |
|
|
421
859638996c86
LoginActivity: fix a few warnings
Da Risk <da_risk@geekorum.com>
parents:
164
diff
changeset
|
66 |
binding.lifecycleOwner = this |
|
62
c9d5f3546fc1
LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents:
0
diff
changeset
|
67 |
setSupportActionBar(binding.toolbar) |
| 0 | 68 |
val account = intent.getParcelableExtra<Account>(EXTRA_ACCOUNT)?.let {
|
69 |
accountManager.fromAndroidAccount(it) |
|
70 |
} |
|
71 |
val action = requireNotNull(intent?.action) { "Invalid intent action passed to $this"}
|
|
72 |
loginViewModel.initialize(action, account) |
|
73 |
binding.viewModel = loginViewModel |
|
74 |
||
75 |
loginViewModel.loginInProgress.observe(this, Observer { inProgress ->
|
|
76 |
showProgress(inProgress!!) |
|
77 |
}) |
|
78 |
loginViewModel.loginFailedEvent.observe(this, EventObserver { event ->
|
|
|
109
eb12523cbc81
LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents:
107
diff
changeset
|
79 |
if (event.errorMsgId in listOf(R.string.error_http_forbidden, R.string.error_http_unauthorized)) {
|
|
eb12523cbc81
LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents:
107
diff
changeset
|
80 |
binding.form.useHttpAuth.isChecked = true |
|
eb12523cbc81
LoginActivity: use http authentication fields
Da Risk <da_risk@geekorum.com>
parents:
107
diff
changeset
|
81 |
} |
| 0 | 82 |
Snackbar.make(binding.root, event.errorMsgId, Snackbar.LENGTH_SHORT).show() |
83 |
}) |
|
84 |
||
85 |
loginViewModel.actionCompleteEvent.observe(this, EventObserver { event->
|
|
86 |
when (event) {
|
|
87 |
is LoginViewModel.ActionCompleteEvent.Failed -> handleActionFailed() |
|
88 |
is LoginViewModel.ActionCompleteEvent.Success -> handleActionSuccess(event) |
|
89 |
||
90 |
} |
|
91 |
}) |
|
92 |
||
93 |
loginViewModel.fieldErrors.observe(this, Observer { errorStatus ->
|
|
94 |
val error = checkNotNull(errorStatus) |
|
|
107
3fa8423d4de4
LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents:
62
diff
changeset
|
95 |
setFieldError(binding.form.urlField, error.invalidUrlMsgId) |
| 0 | 96 |
if (error.hasAttemptLogin) {
|
|
107
3fa8423d4de4
LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents:
62
diff
changeset
|
97 |
setFieldError(binding.form.usernameField, error.invalidNameMsgId) |
|
3fa8423d4de4
LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents:
62
diff
changeset
|
98 |
setFieldError(binding.form.passwordField, error.invalidPasswordMsgId) |
| 0 | 99 |
} |
100 |
}) |
|
101 |
} |
|
102 |
||
103 |
private fun handleActionSuccess(event: LoginViewModel.ActionCompleteEvent.Success) {
|
|
104 |
accountAuthenticatorResult = Bundle().apply {
|
|
105 |
when (intent.action) {
|
|
106 |
ACTION_ADD_ACCOUNT -> {
|
|
107 |
putString(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.TTRSS_ACCOUNT_TYPE) |
|
108 |
putString(AccountManager.KEY_ACCOUNT_NAME, event.account.username) |
|
109 |
} |
|
110 |
ACTION_CONFIRM_CREDENTIALS -> putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true) |
|
111 |
} |
|
112 |
} |
|
113 |
setResult(Activity.RESULT_OK) |
|
114 |
finish() |
|
115 |
} |
|
116 |
||
117 |
private fun handleActionFailed() {
|
|
118 |
accountAuthenticatorResult = Bundle().apply {
|
|
119 |
when (intent.action) {
|
|
120 |
ACTION_ADD_ACCOUNT -> {
|
|
121 |
putInt(AccountManager.KEY_ERROR_CODE, AccountAuthenticator.ERROR_CODE_AUTHENTICATOR_FAILURE) |
|
122 |
putString(AccountManager.KEY_ERROR_MESSAGE, "Unable to add account") |
|
123 |
} |
|
124 |
ACTION_CONFIRM_CREDENTIALS -> putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false) |
|
125 |
} |
|
126 |
} |
|
127 |
finish() |
|
128 |
} |
|
129 |
||
|
107
3fa8423d4de4
LoginActivity: Add fields for http authentication
Da Risk <da_risk@geekorum.com>
parents:
62
diff
changeset
|
130 |
private fun setFieldError(view: TextInputLayout, errorId: Int?) {
|
| 0 | 131 |
val errorMsg = if (errorId != null) getString(errorId) else null |
132 |
view.error = errorMsg |
|
133 |
} |
|
134 |
||
135 |
||
136 |
private fun showProgress(show: Boolean) {
|
|
137 |
TransitionManager.beginDelayedTransition(binding.root as ViewGroup) |
|
|
62
c9d5f3546fc1
LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents:
0
diff
changeset
|
138 |
binding.form.loginForm.visibility = if (show) View.GONE else View.VISIBLE |
|
c9d5f3546fc1
LoginActivity: rework the layout to be better for large screen
Da Risk <da_risk@geekorum.com>
parents:
0
diff
changeset
|
139 |
binding.form.loginProgress.visibility = if (show) View.VISIBLE else View.GONE |
| 0 | 140 |
} |
141 |
||
142 |
||
143 |
} |