mirror of
https://github.com/fumiama/simple-dict-android.git
synced 2026-06-06 17:20:26 +08:00
v1.9.1
1. 修复编辑闪退 2. 刷新重置行数
This commit is contained in:
@@ -12,8 +12,8 @@ android {
|
||||
applicationId "top.fumiama.simpledict"
|
||||
minSdkVersion 26
|
||||
targetSdkVersion 30
|
||||
versionCode 11
|
||||
versionName '1.9'
|
||||
versionCode 12
|
||||
versionName '1.9.1'
|
||||
resConfigs "zh", "zh-rCN"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
@@ -20,34 +20,38 @@ class Client(private val ip: String, private val port: Int) {
|
||||
/**
|
||||
* 初始化普通交互连接
|
||||
*/
|
||||
fun initConnect(depth: Int = 0){
|
||||
fun initConnect(depth: Int = 0): Boolean{
|
||||
if(depth > 3) Log.d("MyC", "connect server failed after $depth tries")
|
||||
else try {
|
||||
sc = Socket(ip, port) //通过socket连接服务器
|
||||
din = sc?.getInputStream() //获取输入流并转换为StreamReader,约定编码格式
|
||||
dout = sc?.getOutputStream() //获取输出流
|
||||
sc?.soTimeout = 2333 //设置连接超时限制
|
||||
if (isConnect) Log.d("MyC", "connect server successful")
|
||||
else {
|
||||
return if (isConnect) {
|
||||
Log.d("MyC", "connect server successful")
|
||||
true
|
||||
} else {
|
||||
Log.d("MyC", "connect server failed, now retry...")
|
||||
initConnect(depth + 1)
|
||||
}
|
||||
} catch (e: IOException) { //获取输入输出流是可能报IOException的,所以必须try-catch
|
||||
e.printStackTrace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据至服务器
|
||||
* @param message 要发送至服务器的字符串
|
||||
*/
|
||||
fun sendMessage(message: CharSequence?) {
|
||||
fun sendMessage(message: CharSequence?): Boolean {
|
||||
try {
|
||||
if (isConnect) {
|
||||
if (message != null) { //判断输出流或者消息是否为空,为空的话会产生nullpoint错误
|
||||
dout?.write(message.toString().toByteArray())
|
||||
dout?.flush()
|
||||
Log.d("MyC", "Send msg: $message")
|
||||
return true
|
||||
} else Log.d("MyC", "The message to be sent is empty")
|
||||
Log.d("MyC", "send message succeed")
|
||||
} else Log.d("MyC", "send message failed: no connect")
|
||||
@@ -55,6 +59,7 @@ class Client(private val ip: String, private val port: Int) {
|
||||
Log.d("MyC", "send message failed: crash")
|
||||
e.printStackTrace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun receiveRawMessage(totalSize: Int = -1, bufferSize: Int = 4096) : ByteArray {
|
||||
@@ -82,17 +87,16 @@ class Client(private val ip: String, private val port: Int) {
|
||||
/**
|
||||
* 关闭连接
|
||||
*/
|
||||
fun closeConnect() {
|
||||
try {
|
||||
fun closeConnect() = try {
|
||||
din?.close()
|
||||
dout?.close()
|
||||
sc?.close()
|
||||
sc = null
|
||||
din = null
|
||||
dout = null
|
||||
true
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
false
|
||||
}
|
||||
Log.d("MyC", "关闭连接")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class MainActivity : AppCompatActivity() {
|
||||
private var dict: SimpleDict? = null
|
||||
private var hasLiked = false
|
||||
private var cm: ClipboardManager? = null
|
||||
private var ad: ListViewHolder.RecyclerViewAdapter? = null
|
||||
private var ad: LikeViewHolder.RecyclerViewAdapter? = null
|
||||
private var lastLikeLine: View? = null
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@@ -200,7 +200,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}*/
|
||||
|
||||
private fun updateSize() {
|
||||
private fun updateSize() = runOnUiThread {
|
||||
lastLikeLine?.fftc?.text = dict?.size?.toString()?:"0"
|
||||
}
|
||||
|
||||
@@ -210,6 +210,7 @@ class MainActivity : AppCompatActivity() {
|
||||
runOnUiThread {
|
||||
Toast.makeText(this@MainActivity, "刷新成功", Toast.LENGTH_SHORT).show()
|
||||
ffsw.isRefreshing = false
|
||||
ad?.capacity = 5
|
||||
ad?.refresh()
|
||||
doWhenFinish?.apply { this() }
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
private var dict = HashMap<String, String?>()
|
||||
val size get() = dict.size
|
||||
val keys get() = dict.keys
|
||||
//val values get() = dict.values
|
||||
//val size get() = dict.size
|
||||
var latestKeys = arrayOf<String>()
|
||||
private val raw: ByteArray
|
||||
get() {
|
||||
@@ -17,25 +15,26 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
var firstRecv: ByteArray
|
||||
do {
|
||||
re = byteArrayOf()
|
||||
initDict()
|
||||
sendMessageWithDelay("cat", 2333)
|
||||
try {
|
||||
firstRecv = client.receiveRawMessage()
|
||||
val firstStr = firstRecv.decodeToString()
|
||||
var length = ""
|
||||
for ((i, c) in firstStr.withIndex()) {
|
||||
if(c.isDigit()) length += c
|
||||
else {
|
||||
if(i + 1 < firstRecv.size) re = firstRecv.copyOfRange(i, firstRecv.size)
|
||||
break
|
||||
if(initDict()) {
|
||||
sendMessageWithDelay("cat", 2333)
|
||||
try {
|
||||
firstRecv = client.receiveRawMessage()
|
||||
val firstStr = firstRecv.decodeToString()
|
||||
var length = ""
|
||||
for ((i, c) in firstStr.withIndex()) {
|
||||
if(c.isDigit()) length += c
|
||||
else {
|
||||
if(i + 1 < firstRecv.size) re = firstRecv.copyOfRange(i, firstRecv.size)
|
||||
break
|
||||
}
|
||||
}
|
||||
re += client.receiveRawMessage(length.toInt() - re.size)
|
||||
break
|
||||
} catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
}
|
||||
re += client.receiveRawMessage(length.toInt() - re.size)
|
||||
break
|
||||
} catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
closeDict()
|
||||
}
|
||||
closeDict()
|
||||
} while (times-- > 0)
|
||||
return re
|
||||
}
|
||||
@@ -45,16 +44,22 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
sleep(delay)
|
||||
}.start()
|
||||
|
||||
private fun initDict() {
|
||||
client.initConnect()
|
||||
client.sendMessage(pwd)
|
||||
client.receiveRawMessage()
|
||||
sleep(233)
|
||||
private fun initDict(): Boolean {
|
||||
if(client.initConnect()){
|
||||
if(client.sendMessage(pwd)) {
|
||||
client.receiveRawMessage()
|
||||
sleep(233)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun closeDict() {
|
||||
client.sendMessage("quit")
|
||||
client.closeConnect()
|
||||
private fun closeDict(): Boolean {
|
||||
if(client.sendMessage("quit")) {
|
||||
if (client.closeConnect()) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun analyzeDictBlk(dictBlock: ByteArray) {
|
||||
@@ -70,7 +75,6 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
Log.d("MySD", "Fetch $key=$data")
|
||||
}
|
||||
|
||||
//fun filterKeys(predicate: (String) -> Boolean) = dict.filterKeys(predicate)
|
||||
fun filterValues(predicate: (String?) -> Boolean) = dict.filterValues(predicate)
|
||||
|
||||
fun fetchDict(doOnLoadSuccess: ()->Unit = {
|
||||
@@ -86,19 +90,21 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
}
|
||||
|
||||
operator fun minusAssign(key: String) {
|
||||
initDict()
|
||||
sendMessageWithDelay("del")
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(key)
|
||||
client.receiveMessage()
|
||||
closeDict()
|
||||
dict.remove(key)
|
||||
val end = latestKeys.size-1
|
||||
if(end > 0) latestKeys = latestKeys.let { oldArr ->
|
||||
var index = -1
|
||||
Array(end) {
|
||||
if(oldArr[it] == key) index = it
|
||||
return@Array if(index < 0 || (index > 0 && it < index)) oldArr[it] else oldArr[it+1]
|
||||
if(initDict()) {
|
||||
sendMessageWithDelay("del")
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(key)
|
||||
client.receiveMessage()
|
||||
if(closeDict()) {
|
||||
dict.remove(key)
|
||||
val end = latestKeys.size-1
|
||||
if(end > 0) latestKeys = latestKeys.let { oldArr ->
|
||||
var index = -1
|
||||
Array(end) {
|
||||
if(oldArr[it] == key) index = it
|
||||
return@Array if(index < 0 || (index > 0 && it < index)) oldArr[it] else oldArr[it+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,15 +113,15 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must
|
||||
|
||||
operator fun set(key: String, value: String): String? {
|
||||
val p = dict[key]
|
||||
initDict()
|
||||
sendMessageWithDelay("set")
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(key)
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(value)
|
||||
client.receiveMessage()
|
||||
closeDict()
|
||||
dict[key] = value
|
||||
if(initDict()) {
|
||||
sendMessageWithDelay("set")
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(key)
|
||||
client.receiveMessage()
|
||||
sendMessageWithDelay(value)
|
||||
client.receiveMessage()
|
||||
if(closeDict()) dict[key] = value
|
||||
}
|
||||
return p
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user