1
0
mirror of https://github.com/fumiama/copymanga.git synced 2026-06-12 19:58:35 +08:00

opt: async login (#36)

This commit is contained in:
Mrs4s
2023-11-06 01:19:18 +08:00
committed by GitHub
parent cc4c1fb064
commit 57e981356f
3 changed files with 44 additions and 32 deletions

View File

@@ -74,4 +74,5 @@ dependencies {
implementation 'com.liaoinstan.springview:library:1.7.0'
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:4.0.1'
implementation 'com.lapism:search:2.4.1@aar'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
}

View File

@@ -1,18 +1,19 @@
package top.fumiama.copymanga
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_login.*
import top.fumiama.copymanga.json.LoginInfoStructure
import top.fumiama.copymanga.tools.api.CMApi
import top.fumiama.copymanga.tools.http.DownloadTools
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import top.fumiama.dmzj.copymanga.R
import kotlin.random.Random
class LoginActivity:Activity() {
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
@@ -22,36 +23,41 @@ class LoginActivity:Activity() {
alblogin.setText(R.string.logout)
}
alblogin.setOnClickListener {
val salt = Random.nextInt(10000)
lifecycleScope.launch {
val salt = Random.nextInt(10000)
val username = altusrnm.text?.toString() ?: run {
Toast.makeText(this, R.string.login_null_username, Toast.LENGTH_SHORT).show()
return@setOnClickListener
Toast.makeText(
this@LoginActivity,
R.string.login_null_username,
Toast.LENGTH_SHORT
).show()
return@launch
}
val pwd = altpwd.text?.toString() ?: run {
Toast.makeText(this@LoginActivity, R.string.login_null_pwd, Toast.LENGTH_SHORT)
.show()
return@launch
}
val pwd = altpwd.text?.toString() ?: run {
Toast.makeText(this, R.string.login_null_pwd, Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
Thread {
if (isLogout) {
MainActivity.member?.logout()
runOnUiThread {
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
Toast.makeText(this@LoginActivity, R.string.login_restart_to_apply, Toast.LENGTH_SHORT).show()
finish()
}
return@Thread
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
Toast.makeText(
this@LoginActivity,
R.string.login_restart_to_apply,
Toast.LENGTH_SHORT
).show()
finish()
return@launch
}
val l = MainActivity.member?.login(username, pwd, salt)
Log.d("MyLA", "login return code: ${l?.code}")
if (l?.code == 200) {
runOnUiThread {
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
finish()
}
return@Thread
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
finish()
return@launch
}
runOnUiThread { Toast.makeText(this@LoginActivity, l?.message, Toast.LENGTH_SHORT).show() }
}.start()
Toast.makeText(this@LoginActivity, l?.message, Toast.LENGTH_SHORT).show()
}
}
}
}

View File

@@ -3,6 +3,9 @@ package top.fumiama.copymanga.user
import android.content.SharedPreferences
import android.widget.Toast
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import top.fumiama.copymanga.MainActivity
import top.fumiama.copymanga.json.LoginInfoStructure
import top.fumiama.copymanga.tools.api.CMApi
@@ -11,7 +14,7 @@ import top.fumiama.dmzj.copymanga.R
class Member(private val pref: SharedPreferences, private val getString: (Int) -> String) {
val hasLogin: Boolean get() = pref.getString("token", "")?.isNotEmpty()?:false
fun login(username: String, pwd: String, salt: Int): LoginInfoStructure {
suspend fun login(username: String, pwd: String, salt: Int): LoginInfoStructure = withContext(Dispatchers.IO) {
try {
CMApi.getLoginConnection(username, pwd, salt)?.apply {
Gson().fromJson(inputStream.reader(), LoginInfoStructure::class.java)?.let { data ->
@@ -23,24 +26,26 @@ class Member(private val pref: SharedPreferences, private val getString: (Int) -
putString("username", data.results?.username)
putString("nickname", data.results?.nickname)
apply()
return refreshAvatar()
return@withContext refreshAvatar()
}
}
return data
return@withContext data
}
}
val l = LoginInfoStructure()
l.code = 400
l.message = getString(R.string.login_get_conn_failed)
return l
return@withContext l
} catch (e: Exception) {
val l = LoginInfoStructure()
l.code = 400
l.message = e.localizedMessage
return l
return@withContext l
}
}
fun refreshAvatar() : LoginInfoStructure {
try {
DownloadTools.getHttpContent(getString(R.string.memberInfoApiUrl).format(