Skip to content

Commit

Permalink
code impr
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Mar 30, 2023
1 parent 4da8348 commit f0cb7b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/storyteller_f/fei/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MainActivity : ComponentActivity() {
} else {
removeUri(path)
}
cacheInvalid()
cacheInvalid()//when delete
fei?.feiService?.server?.channel?.trySend(SseEvent(data = "refresh"))
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ class MainActivity : ComponentActivity() {
@OptIn(ObsoleteCoroutinesApi::class)
private suspend fun saveUri(uri: Uri) {
savedUriFile.appendText(uri.toString())
cacheInvalid()
cacheInvalid()//when save
fei?.feiService?.server?.channel?.send(SseEvent("refresh"))
}

Expand Down Expand Up @@ -426,7 +426,7 @@ class MainActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
lifecycleScope.launch {
cacheInvalid()
cacheInvalid()//when resume
}
}

Expand Down
29 changes: 15 additions & 14 deletions app/src/main/java/com/storyteller_f/fei/Shares.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ fun Context.removeUri(path: SharedFileInfo) {
val file = savedUriFile
try {
val readText = file.readText()
readText.trim().split("\n").filter {
val valid = readText.trim().split("\n").filter {
it.isNotEmpty() && it != path.uri
}.joinToString("\n").let {
}
valid.joinToString("\n").let {
file.writeText(it)
}

//todo contentResolver.outgoingPersistedUriPermissions
if (contentResolver.outgoingPersistedUriPermissions.any {
it.uri.toString() == path.uri
})
contentResolver.releasePersistableUriPermission(
path.uri.toUri(),
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
contentResolver.outgoingPersistedUriPermissions.forEach {
if (!valid.contains(it.uri.toString())) {
contentResolver.releasePersistableUriPermission(
path.uri.toUri(),
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
}
} catch (e: Exception) {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_LONG).show()
}
Expand Down Expand Up @@ -95,13 +95,14 @@ suspend fun Context.cacheInvalid() = withContext(Dispatchers.IO) {
}

private fun Context.cacheInvalidInternal(): Boolean {
val readText = savedUriFile.readText()
val savedList = readText.split("\n").filter {
val buffer = savedUriFile
val content = buffer.readText()
val savedList = content.split("\n").filter {
it.isNotEmpty()
}.mapNotNull {
sharedFileInfo(it)
}
savedUriFile.writeText(savedList.joinToString("\n") {
buffer.writeText(savedList.joinToString("\n") {
it.uri
})
val savedFiles = File(filesDir, "saved").listFiles()?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class FeiServer(private val feiService: FeiService) {
feiService.scope.launch {
saveFile(File(info.name).extension, uri)
feiService.removeUri(info)
feiService.cacheInvalid()
feiService.cacheInvalid()//when save to local
channel?.send(SseEvent("refresh"))
}
}
Expand Down

0 comments on commit f0cb7b4

Please sign in to comment.