mirror of
https://github.com/fumiama/copymanga.git
synced 2026-06-29 23:30:41 +08:00
opt: async login (#36)
This commit is contained in:
@@ -74,4 +74,5 @@ dependencies {
|
|||||||
implementation 'com.liaoinstan.springview:library:1.7.0'
|
implementation 'com.liaoinstan.springview:library:1.7.0'
|
||||||
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:4.0.1'
|
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:4.0.1'
|
||||||
implementation 'com.lapism:search:2.4.1@aar'
|
implementation 'com.lapism:search:2.4.1@aar'
|
||||||
|
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
package top.fumiama.copymanga
|
package top.fumiama.copymanga
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
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 kotlinx.android.synthetic.main.activity_login.*
|
||||||
import top.fumiama.copymanga.json.LoginInfoStructure
|
import kotlinx.coroutines.Dispatchers
|
||||||
import top.fumiama.copymanga.tools.api.CMApi
|
import kotlinx.coroutines.launch
|
||||||
import top.fumiama.copymanga.tools.http.DownloadTools
|
import kotlinx.coroutines.withContext
|
||||||
import top.fumiama.dmzj.copymanga.R
|
import top.fumiama.dmzj.copymanga.R
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
class LoginActivity:Activity() {
|
|
||||||
|
class LoginActivity : AppCompatActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_login)
|
setContentView(R.layout.activity_login)
|
||||||
@@ -22,36 +23,41 @@ class LoginActivity:Activity() {
|
|||||||
alblogin.setText(R.string.logout)
|
alblogin.setText(R.string.logout)
|
||||||
}
|
}
|
||||||
alblogin.setOnClickListener {
|
alblogin.setOnClickListener {
|
||||||
val salt = Random.nextInt(10000)
|
lifecycleScope.launch {
|
||||||
|
val salt = Random.nextInt(10000)
|
||||||
val username = altusrnm.text?.toString() ?: run {
|
val username = altusrnm.text?.toString() ?: run {
|
||||||
Toast.makeText(this, R.string.login_null_username, Toast.LENGTH_SHORT).show()
|
Toast.makeText(
|
||||||
return@setOnClickListener
|
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) {
|
if (isLogout) {
|
||||||
MainActivity.member?.logout()
|
MainActivity.member?.logout()
|
||||||
runOnUiThread {
|
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
|
||||||
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
|
Toast.makeText(
|
||||||
Toast.makeText(this@LoginActivity, R.string.login_restart_to_apply, Toast.LENGTH_SHORT).show()
|
this@LoginActivity,
|
||||||
finish()
|
R.string.login_restart_to_apply,
|
||||||
}
|
Toast.LENGTH_SHORT
|
||||||
return@Thread
|
).show()
|
||||||
|
finish()
|
||||||
|
return@launch
|
||||||
}
|
}
|
||||||
val l = MainActivity.member?.login(username, pwd, salt)
|
val l = MainActivity.member?.login(username, pwd, salt)
|
||||||
Log.d("MyLA", "login return code: ${l?.code}")
|
Log.d("MyLA", "login return code: ${l?.code}")
|
||||||
if (l?.code == 200) {
|
if (l?.code == 200) {
|
||||||
runOnUiThread {
|
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
|
||||||
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
|
finish()
|
||||||
finish()
|
return@launch
|
||||||
}
|
|
||||||
return@Thread
|
|
||||||
}
|
}
|
||||||
runOnUiThread { Toast.makeText(this@LoginActivity, l?.message, Toast.LENGTH_SHORT).show() }
|
Toast.makeText(this@LoginActivity, l?.message, Toast.LENGTH_SHORT).show()
|
||||||
}.start()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ package top.fumiama.copymanga.user
|
|||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import com.google.gson.Gson
|
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.MainActivity
|
||||||
import top.fumiama.copymanga.json.LoginInfoStructure
|
import top.fumiama.copymanga.json.LoginInfoStructure
|
||||||
import top.fumiama.copymanga.tools.api.CMApi
|
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) {
|
class Member(private val pref: SharedPreferences, private val getString: (Int) -> String) {
|
||||||
val hasLogin: Boolean get() = pref.getString("token", "")?.isNotEmpty()?:false
|
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 {
|
try {
|
||||||
CMApi.getLoginConnection(username, pwd, salt)?.apply {
|
CMApi.getLoginConnection(username, pwd, salt)?.apply {
|
||||||
Gson().fromJson(inputStream.reader(), LoginInfoStructure::class.java)?.let { data ->
|
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("username", data.results?.username)
|
||||||
putString("nickname", data.results?.nickname)
|
putString("nickname", data.results?.nickname)
|
||||||
apply()
|
apply()
|
||||||
return refreshAvatar()
|
return@withContext refreshAvatar()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data
|
return@withContext data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val l = LoginInfoStructure()
|
val l = LoginInfoStructure()
|
||||||
l.code = 400
|
l.code = 400
|
||||||
l.message = getString(R.string.login_get_conn_failed)
|
l.message = getString(R.string.login_get_conn_failed)
|
||||||
return l
|
return@withContext l
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
val l = LoginInfoStructure()
|
val l = LoginInfoStructure()
|
||||||
l.code = 400
|
l.code = 400
|
||||||
l.message = e.localizedMessage
|
l.message = e.localizedMessage
|
||||||
return l
|
return@withContext l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fun refreshAvatar() : LoginInfoStructure {
|
fun refreshAvatar() : LoginInfoStructure {
|
||||||
try {
|
try {
|
||||||
DownloadTools.getHttpContent(getString(R.string.memberInfoApiUrl).format(
|
DownloadTools.getHttpContent(getString(R.string.memberInfoApiUrl).format(
|
||||||
|
|||||||
Reference in New Issue
Block a user