Skip to content

Commit

Permalink
File system surfing 2 (#39)
Browse files Browse the repository at this point in the history
* primitive surfing

* navigation to Nihil

* created constant class
  • Loading branch information
SomeAnonimCoder authored Jun 16, 2019
1 parent 4871edc commit b71c9b8
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 50 deletions.
1 change: 1 addition & 0 deletions app/src/main/assets/Nihil/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"text":"Eсли вы здесь, значит, вы добрались до таксона, который ещё не был добавлен в наш справочник", "to":1}, {"text":"Вы можете помочь нам это сделать https://github.com/SomeAnonimCoder/TheBigMayevsky", "to":1}]
72 changes: 72 additions & 0 deletions app/src/main/java/msu/ug/ChoiceFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package msu.ug

import android.content.Context
import android.view.View
import android.widget.ListView
import android.widget.SimpleAdapter
import androidx.fragment.app.ListFragment
import org.json.JSONArray
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

class ChoiceFragment(private val actContext: Context) : ListFragment() {
private val storage = Storage(actContext)
private val toIndices = ArrayList<Int>()
private val taxons = ArrayList<String>()

override fun onStart() {
super.onStart()

val from = arrayOf(Const.MAP_TEXT_KEY)
val to = intArrayOf(android.R.id.text1)
val displayData = ArrayList<Map<String, Any>>()
updateDisplayData(displayData)
val listAdapter = SimpleAdapter(context, displayData, android.R.layout.simple_list_item_1, from, to)
setListAdapter(listAdapter)
storage.addSwitchListener {
updateDisplayData(displayData)
listAdapter.notifyDataSetChanged()
}
}

override fun onListItemClick(l: ListView?, v: View?, position: Int, id: Long) {
super.onListItemClick(l, v, position, id)
if (toIndices[position] == 0) {
storage.appendPath(taxons[position])
storage.currentChoise = 1
} else {
storage.currentChoise = toIndices[position]
}

}

private fun getPlainText(statementFile : String) : String {
val statementStream = actContext.assets.open(statementFile)
val br = BufferedReader(InputStreamReader(statementStream))
var line : String? = br.readLine()
val sb = StringBuilder()

while (line != null) {
sb.append(line)
line = br.readLine()
}

return sb.toString()
}

private fun updateDisplayData(displayData: ArrayList<Map<String, Any>>) {
val file = storage.getPath().joinToString("/") + "/" + storage.currentChoise.toString()
val arr = JSONArray(getPlainText(file))
toIndices.clear()
displayData.clear()
(0 until arr.length()).forEach {i ->
val map = HashMap<String, Any>()
val thesa = arr.getJSONObject(i)
map[Const.MAP_TEXT_KEY] = thesa.getString(Const.JSON_TEXT_KEY)
toIndices.add(thesa.getInt(Const.TO_KEY))
taxons.add(thesa.optString(Const.TAXON_KEY, Const.DEFAULT_TAXON))
displayData.add(map)
}
}
}
46 changes: 0 additions & 46 deletions app/src/main/java/msu/ug/ChoiseFragment.kt

This file was deleted.

21 changes: 21 additions & 0 deletions app/src/main/java/msu/ug/Const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package msu.ug

class Const {
companion object {
const val CUR_CHOICE_KEY = "current_choise"
const val PATH_LEN_KEY = "path_len"

const val DEFAULT_TAXON = "Nihil"

const val MAP_TEXT_KEY = "text"
const val JSON_TEXT_KEY = "text"
const val TO_KEY = "to"
const val TAXON_KEY = "taxon"

const val SP_NAME = "my_shared_preferences"

fun folderKey(folderIndex : Int) : String {
return "folder_$folderIndex"
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/msu/ug/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_frame, ChoiseFragment(this))
transaction.replace(R.id.fragment_frame, ChoiceFragment(this))
transaction.commit()
}
}
37 changes: 34 additions & 3 deletions app/src/main/java/msu/ug/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,43 @@ import android.content.Context
import android.content.SharedPreferences

class Storage (context : Context) {
private val sp : SharedPreferences = context.getSharedPreferences("MY_SP", Context.MODE_PRIVATE)
private val sp : SharedPreferences = context.getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
private val switchListeners : ArrayList<() -> Unit> = ArrayList()

fun addSwitchListener(listener : () -> Unit) {
switchListeners.add(listener)
}

var currentChoise : Int
set(value) {
sp.edit().putInt("current_choise", value).apply()
if (value > 0) {
sp.edit().putInt(Const.CUR_CHOICE_KEY, value).apply()
switchListeners.forEach {
it.invoke()
}
}
}
get() {
return sp.getInt("current_choise", 1)
return sp.getInt(Const.CUR_CHOICE_KEY, 1)
}

fun getPath() : List<String> {
val pathLen = sp.getInt(Const.PATH_LEN_KEY, 0)
val ret = ArrayList<String>()

for (i in 0 until pathLen) {
val str = sp.getString(Const.folderKey(i), "")
if (str != null) {
ret.add(str)
}
}

return ret
}

fun appendPath(folder : String) {
val len = sp.getInt(Const.PATH_LEN_KEY, 0)
sp.edit().putString(Const.folderKey(len), folder).apply()
sp.edit().putInt(Const.PATH_LEN_KEY, len + 1).apply()
}
}

0 comments on commit b71c9b8

Please sign in to comment.