Last active
May 31, 2023 05:56
-
-
Save fuzzyweapon/661a1d770f6ed71bd873f3008ba9feb5 to your computer and use it in GitHub Desktop.
FileIoDemo Partial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 PolyForest | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package fileiodemo | |
import com.acornui.async.* | |
import com.acornui.component.button | |
import com.acornui.component.layout.algorithm.VerticalLayoutContainer | |
import com.acornui.component.layout.algorithm.hGroup | |
import com.acornui.component.stack | |
import com.acornui.component.stage | |
import com.acornui.component.text.TextArea | |
import com.acornui.component.text.text | |
import com.acornui.component.text.textArea | |
import com.acornui.core.di.Owned | |
import com.acornui.core.di.inject | |
import com.acornui.core.immutable.dataBinding | |
import com.acornui.core.input.interaction.click | |
import com.acornui.core.popup.alert | |
import com.acornui.file.FileFilterGroup | |
import com.acornui.file.FileIoManager | |
import com.acornui.file.FileReader | |
import com.acornui.skins.BasicUiSkin | |
class FileIoDemo(owner: Owned) : VerticalLayoutContainer(owner) { | |
private lateinit var editor: TextArea | |
private val dataBinding = dataBinding<List<FileVo>>(emptyList()) | |
private data class FileVo (val name: String, val size: Long, val lastModified: Long, val contents: Deferred<String>) { | |
constructor(reader: FileReader) : this(reader.name, reader.size, reader.lastModified, async { return@async reader.readAsString() }) | |
} | |
private var fileManager: FileIoManager | |
private val saveSupported: Boolean | |
// Set extensions to null for all. | |
private val filterGroups = listOf(listOf(".txt",".kt"), listOf("zip"), listOf("image/*")).map { FileFilterGroup(it) } | |
private var multipleFilesOpen = false | |
init { | |
BasicUiSkin(stage).apply() | |
fileManager = inject(FileIoManager) | |
saveSupported = fileManager.saveSupported | |
+hGroup { | |
+text("File Io Demo") | |
+hGroup { | |
style.gap = 0f | |
dataBinding.bind { | |
clearElements(dispose = true) | |
if (it.isNotEmpty()) apply { | |
for (i in 0..it.lastIndex) { | |
if (i < 1) | |
+text(" - ${it[i].name}") | |
else | |
+text(" | ${it[i].name}") | |
} | |
} | |
} | |
} | |
} | |
+hGroup { | |
+button("Open File") { | |
click().add { | |
openFile() | |
} | |
} | |
+button("Open Files") { | |
click().add { | |
openFiles() | |
} | |
} | |
+button("Save File") { | |
dataBinding.bind { | |
visible = saveSupported && !multipleFilesOpen && it.isNotEmpty() | |
} | |
click().add { | |
saveFile() | |
} | |
} | |
+button("Clear Files") { | |
click().add { | |
clearFiles() | |
} | |
} | |
} | |
+stack { | |
editor = +textArea { | |
dataBinding.bind { fileList -> | |
var fileContents = "" | |
text = when { | |
fileList.size > 1 -> fileList.joinToString("\n\n\n") { | |
launch { fileContents = it.contents.await() } | |
"${it.name}\n$fileContents" } | |
fileList.size == 1 -> { | |
launch { fileContents = fileList[0].contents.await() } | |
fileContents | |
} | |
else -> fileContents | |
} | |
visible = !isDisposed && fileList.isNotEmpty() | |
} | |
} layout { fill() } | |
} layout { fill() } | |
} | |
private fun openFile() { | |
fileManager.pickFileForOpen(filterGroups) { | |
@Suppress("UNCHECKED_CAST") | |
dataBinding.value = filterBadFiles(listOf(FileVo(it))) | |
} | |
} | |
private fun openFiles() { | |
fileManager.pickFilesForOpen(filterGroups) { | |
dataBinding.value = filterBadFiles(it.map { FileVo(it) }) | |
} | |
} | |
private fun saveFile() { | |
fileManager.pickFileForSave(filterGroups, "txt") { | |
async { it.saveToFileAsString(editor.text) } | |
} | |
} | |
private fun clearFiles() { | |
dataBinding.value = emptyList() | |
} | |
private fun filterBadFiles(fileList: List<FileVo>): List<FileVo> { | |
val aList = fileList.filterNot { | |
try { | |
launch { it.contents.await() } | |
false | |
} catch (e: Exception) { | |
alert("Error", e.message ?: "Some unknown error has occurred.") | |
true | |
} | |
} | |
return aList | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment