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