Pixelcat-App-Android/app/src/main/kotlin/at/connyduck/pixelcat/components/login/LoginActivity.kt

181 lines
5.9 KiB
Kotlin
Raw Normal View History

2020-06-17 19:07:47 +02:00
/*
* Copyright (C) 2020 Conny Duck
*
* This file is part of Pixelcat.
*
* Pixelcat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pixelcat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-12 15:44:45 +02:00
package at.connyduck.pixelcat.components.login
import android.app.Activity
2020-09-21 16:38:22 +02:00
import android.content.Context
2020-06-12 15:44:45 +02:00
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsCompat.Type.systemBars
2020-09-21 16:38:22 +02:00
import androidx.lifecycle.lifecycleScope
2020-06-12 15:44:45 +02:00
import at.connyduck.pixelcat.R
import at.connyduck.pixelcat.components.about.AboutActivity
import at.connyduck.pixelcat.components.general.BaseActivity
2020-09-29 20:42:01 +02:00
import at.connyduck.pixelcat.components.main.MainActivity
2020-06-12 15:44:45 +02:00
import at.connyduck.pixelcat.components.settings.SettingsActivity
2020-09-21 16:38:22 +02:00
import at.connyduck.pixelcat.components.util.extension.visible
2020-06-12 15:44:45 +02:00
import at.connyduck.pixelcat.dagger.ViewModelFactory
import at.connyduck.pixelcat.databinding.ActivityLoginBinding
import at.connyduck.pixelcat.util.viewBinding
2020-09-21 16:38:22 +02:00
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
2020-06-12 15:44:45 +02:00
import javax.inject.Inject
2020-09-21 16:38:22 +02:00
@FlowPreview
@ExperimentalCoroutinesApi
class LoginActivity : BaseActivity() {
2020-06-12 15:44:45 +02:00
@Inject
lateinit var viewModelFactory: ViewModelFactory
2020-09-21 16:38:22 +02:00
private val viewModel: LoginViewModel by viewModels { viewModelFactory }
2020-06-12 15:44:45 +02:00
private val binding by viewBinding(ActivityLoginBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
2020-09-21 16:38:22 +02:00
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, insets ->
val top = insets.getInsets(systemBars()).top
2020-06-12 15:44:45 +02:00
val toolbarParams = binding.loginToolbar.layoutParams as ViewGroup.MarginLayoutParams
toolbarParams.topMargin = top
WindowInsetsCompat.CONSUMED
2020-06-12 15:44:45 +02:00
}
setSupportActionBar(binding.loginToolbar)
supportActionBar?.run {
setDisplayShowTitleEnabled(false)
}
2020-09-21 16:38:22 +02:00
lifecycleScope.launch {
viewModel.observe().collect { loginModel ->
onChanged(loginModel)
}
}
2020-06-12 15:44:45 +02:00
binding.loginButton.setOnClickListener {
2020-09-21 16:38:22 +02:00
viewModel.startLogin(binding.loginInput.text.toString())
2020-06-12 15:44:45 +02:00
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val authCode = data?.getStringExtra(LoginWebViewActivity.RESULT_AUTHORIZATION_CODE)
2020-06-12 19:58:15 +02:00
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && !authCode.isNullOrEmpty()) {
2020-09-21 16:38:22 +02:00
viewModel.authCode(authCode)
2020-06-12 15:44:45 +02:00
return
}
super.onActivityResult(requestCode, resultCode, data)
}
2020-09-21 16:38:22 +02:00
override fun onStart() {
super.onStart()
if (!intent.hasExtra(LoginWebViewActivity.RESULT_AUTHORIZATION_CODE)) {
viewModel.removeError()
}
}
2020-06-12 15:44:45 +02:00
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.login, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
2020-06-12 19:58:15 +02:00
when (item.itemId) {
2020-06-12 15:44:45 +02:00
R.id.navigation_settings -> {
startActivity(SettingsActivity.newIntent(this))
return true
}
R.id.navigation_info -> {
startActivity(AboutActivity.newIntent(this))
return true
}
}
return super.onOptionsItemSelected(item)
}
2020-09-21 16:38:22 +02:00
private fun onChanged(loginModel: LoginModel?) {
2020-06-12 15:44:45 +02:00
binding.loginInput.setText(loginModel?.input)
2020-06-12 19:58:15 +02:00
if (loginModel == null) {
2020-06-12 15:44:45 +02:00
return
}
2020-06-12 19:58:15 +02:00
when (loginModel.state) {
2020-09-21 16:38:22 +02:00
LoginState.NO_ERROR -> {
binding.loginInputLayout.error = null
setLoading(false)
}
LoginState.AUTH_ERROR -> {
binding.loginInputLayout.error = "auth error"
setLoading(false)
}
LoginState.INVALID_DOMAIN -> {
binding.loginInputLayout.error = "invalid domain"
setLoading(false)
}
LoginState.NETWORK_ERROR -> {
binding.loginInputLayout.error = "network error"
setLoading(false)
}
2020-06-12 15:44:45 +02:00
LoginState.LOADING -> {
2020-09-21 16:38:22 +02:00
setLoading(true)
2020-06-12 15:44:45 +02:00
}
LoginState.SUCCESS -> {
2020-09-21 16:38:22 +02:00
setLoading(true)
2020-06-12 15:44:45 +02:00
startActivityForResult(LoginWebViewActivity.newIntent(loginModel.domain!!, loginModel.clientId!!, loginModel.clientSecret!!, this), REQUEST_CODE)
}
LoginState.SUCCESS_FINAL -> {
2020-09-21 16:38:22 +02:00
setLoading(true)
startActivity(MainActivity.newIntent(this))
2020-06-12 15:44:45 +02:00
finish()
}
}
}
2020-09-21 16:38:22 +02:00
private fun setLoading(loading: Boolean) {
binding.loginLoading.visible = loading
binding.loginImageView.visible = !loading
binding.loginInputLayout.visible = !loading
binding.loginButton.visible = !loading
}
2020-06-12 15:44:45 +02:00
companion object {
private const val REQUEST_CODE = 14
2020-09-21 16:38:22 +02:00
fun newIntent(context: Context) = Intent(context, LoginActivity::class.java)
2020-06-12 15:44:45 +02:00
}
}