Skip to content

Commit 3137af2

Browse files
committed
55986-elys
1 parent d22a4ac commit 3137af2

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

app/src/main/java/joshuatee/wx/settings/SettingsColorPaletteEditor.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class SettingsColorPaletteEditor : BaseActivity(), OnMenuItemClickListener {
5050

5151
companion object {
5252
const val URL = ""
53+
private const val READ_REQUEST_CODE = 42
5354
}
5455

5556
private lateinit var arguments: Array<String>
@@ -68,6 +69,7 @@ class SettingsColorPaletteEditor : BaseActivity(), OnMenuItemClickListener {
6869
R.menu.settings_color_palette_editor,
6970
true
7071
)
72+
showLoadFromFileMenuItem()
7173
arguments = intent.getStringArrayExtra(URL)!!
7274
type = arguments[0]
7375
typeAsInt = type.toIntOrNull() ?: 94
@@ -196,6 +198,7 @@ class SettingsColorPaletteEditor : BaseActivity(), OnMenuItemClickListener {
196198
palContent.text,
197199
"wX_colormap_" + palTitle.text + ".txt"
198200
)
201+
R.id.action_load -> loadSettings()
199202

200203
else -> return super.onOptionsItemSelected(item)
201204
}
@@ -207,6 +210,20 @@ class SettingsColorPaletteEditor : BaseActivity(), OnMenuItemClickListener {
207210
super.onStop()
208211
}
209212

213+
214+
private fun showLoadFromFileMenuItem() {
215+
toolbarBottom.menu.findItem(R.id.action_load).isVisible = true
216+
}
217+
218+
private fun loadSettings() {
219+
performFileSearch()
220+
}
221+
222+
private fun displaySettings(txt: String) {
223+
//palContent.setText(txt)
224+
palContent.text = txt
225+
}
226+
210227
private fun convertPalette(txt: String): String {
211228
var txtLocal = txt
212229
.replace("color", "Color")
@@ -227,6 +244,55 @@ class SettingsColorPaletteEditor : BaseActivity(), OnMenuItemClickListener {
227244
.replace("RF", GlobalVariables.newline + "#RF")
228245
return txtLocal
229246
}
247+
/**
248+
* Fires an intent to spin up the "file chooser" UI and select an image.
249+
*/
250+
private fun performFileSearch() {
251+
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file browser.
252+
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
253+
// Filter to only show results that can be "opened", such as a
254+
// file (as opposed to a list of contacts or timezones)
255+
intent.addCategory(Intent.CATEGORY_OPENABLE)
256+
// Filter to show only images, using the image MIME data type.
257+
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
258+
// To search for all documents available via installed storage providers,
259+
// it would be "*/*".
260+
intent.type = "*/*"
261+
startActivityForResult(intent, READ_REQUEST_CODE)
262+
}
263+
264+
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
265+
super.onActivityResult(requestCode, resultCode, resultData)
266+
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
267+
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
268+
// response to some other intent, and the code below shouldn't run at all.
269+
270+
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
271+
// The document selected by the user won't be returned in the intent.
272+
// Instead, a URI to that document will be contained in the return intent
273+
// provided to this method as a parameter.
274+
// Pull that URI using resultData.getData().
275+
//val uri: Uri
276+
resultData?.let {
277+
val uri = it.data
278+
displaySettings(readTextFromUri(uri!!))
279+
}
280+
}
281+
}
282+
283+
private fun readTextFromUri(uri: Uri): String {
284+
val content = UtilityIO.readTextFromUri(this, uri)
285+
val uriArr = uri.lastPathSegment!!.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
286+
var fileName = "map"
287+
if (uriArr.isNotEmpty()) {
288+
fileName = uriArr.last()
289+
}
290+
fileName = fileName.replace(".txt", "").replace(".pal", "")
291+
name = fileName + "_" + formattedDate
292+
//palTitle.setText(name)
293+
palTitle.text = name
294+
return convertPalette(content)
295+
}
230296
//elys mod
231297
private fun savepalfile(fileName: String, text: String) {
232298
val dir = GlobalVariables.PalFilesPath

0 commit comments

Comments
 (0)