mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
login/register cleanup
This commit is contained in:
parent
18a8ec2e3f
commit
ae829f8c54
@ -8,6 +8,7 @@ import androidx.fragment.app.DialogFragment
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.Login
|
||||||
import com.github.libretube.databinding.DialogLoginBinding
|
import com.github.libretube.databinding.DialogLoginBinding
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.util.PreferenceHelper
|
import com.github.libretube.util.PreferenceHelper
|
||||||
@ -23,27 +24,22 @@ class LoginDialog : DialogFragment() {
|
|||||||
binding = DialogLoginBinding.inflate(layoutInflater)
|
binding = DialogLoginBinding.inflate(layoutInflater)
|
||||||
|
|
||||||
binding.login.setOnClickListener {
|
binding.login.setOnClickListener {
|
||||||
if (binding.username.text.toString() != "" && binding.password.text.toString() != "") {
|
if (isInsertionValid()) {
|
||||||
val login =
|
signIn(
|
||||||
com.github.libretube.api.obj.Login(
|
binding.username.text.toString(),
|
||||||
binding.username.text.toString(),
|
binding.password.text.toString()
|
||||||
binding.password.text.toString()
|
)
|
||||||
)
|
|
||||||
login(login)
|
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binding.register.setOnClickListener {
|
binding.register.setOnClickListener {
|
||||||
if (
|
if (isInsertionValid()) {
|
||||||
binding.username.text.toString() != "" &&
|
signIn(
|
||||||
binding.password.text.toString() != ""
|
|
||||||
) {
|
|
||||||
val login = com.github.libretube.api.obj.Login(
|
|
||||||
binding.username.text.toString(),
|
binding.username.text.toString(),
|
||||||
binding.password.text.toString()
|
binding.password.text.toString(),
|
||||||
|
true
|
||||||
)
|
)
|
||||||
register(login)
|
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
@ -56,12 +52,20 @@ class LoginDialog : DialogFragment() {
|
|||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun login(login: com.github.libretube.api.obj.Login) {
|
private fun isInsertionValid(): Boolean {
|
||||||
|
return binding.username.text.toString() != "" && binding.password.text.toString() != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
|
||||||
|
val login = Login(username, password)
|
||||||
lifecycleScope.launchWhenCreated {
|
lifecycleScope.launchWhenCreated {
|
||||||
val response = try {
|
val response = try {
|
||||||
RetrofitInstance.authApi.login(login)
|
if (createNewAccount) {
|
||||||
|
RetrofitInstance.authApi.register(login)
|
||||||
|
} else {
|
||||||
|
RetrofitInstance.authApi.login(login)
|
||||||
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
println(e)
|
|
||||||
Log.e(TAG(), "IOException, you might not have internet connection")
|
Log.e(TAG(), "IOException, you might not have internet connection")
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
@ -70,50 +74,31 @@ class LoginDialog : DialogFragment() {
|
|||||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), "dafaq?$e")
|
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.error != null) {
|
if (response.error != null) {
|
||||||
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
|
||||||
} else if (response.token != null) {
|
return@launchWhenCreated
|
||||||
Toast.makeText(context, R.string.loggedIn, Toast.LENGTH_SHORT).show()
|
|
||||||
PreferenceHelper.setToken(response.token!!)
|
|
||||||
PreferenceHelper.setUsername(login.username!!)
|
|
||||||
|
|
||||||
activity?.recreate()
|
|
||||||
dialog?.dismiss()
|
|
||||||
}
|
}
|
||||||
|
if (response.token == null) return@launchWhenCreated
|
||||||
|
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
if (createNewAccount) R.string.registered else R.string.loggedIn,
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
|
||||||
|
PreferenceHelper.setToken(response.token!!)
|
||||||
|
PreferenceHelper.setUsername(login.username!!)
|
||||||
|
|
||||||
|
activity?.recreate()
|
||||||
|
dialog?.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun register(login: com.github.libretube.api.obj.Login) {
|
private fun finish() {
|
||||||
fun run() {
|
activity?.recreate()
|
||||||
lifecycleScope.launchWhenCreated {
|
dialog?.dismiss()
|
||||||
val response = try {
|
|
||||||
RetrofitInstance.authApi.register(login)
|
|
||||||
} catch (e: IOException) {
|
|
||||||
println(e)
|
|
||||||
Log.e(TAG(), "IOException, you might not have internet connection")
|
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
|
||||||
return@launchWhenCreated
|
|
||||||
} catch (e: HttpException) {
|
|
||||||
Log.e(TAG(), "HttpException, unexpected response")
|
|
||||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
|
||||||
return@launchWhenCreated
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG(), "dafaq?$e")
|
|
||||||
return@launchWhenCreated
|
|
||||||
}
|
|
||||||
if (response.error != null) {
|
|
||||||
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
|
|
||||||
} else if (response.token != null) {
|
|
||||||
Toast.makeText(context, R.string.registered, Toast.LENGTH_SHORT).show()
|
|
||||||
PreferenceHelper.setToken(response.token!!)
|
|
||||||
PreferenceHelper.setUsername(login.username!!)
|
|
||||||
dialog?.dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
run()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user