diff --git a/app/src/main/assets/Nihil/1 b/app/src/main/assets/Nihil/1 new file mode 100644 index 0000000..de62ea1 --- /dev/null +++ b/app/src/main/assets/Nihil/1 @@ -0,0 +1 @@ +[{"text":"Eсли вы здесь, значит, вы добрались до таксона, который ещё не был добавлен в наш справочник", "to":1}, {"text":"Вы можете помочь нам это сделать https://github.com/SomeAnonimCoder/TheBigMayevsky", "to":1}] diff --git a/app/src/main/java/msu/ug/ChoiceFragment.kt b/app/src/main/java/msu/ug/ChoiceFragment.kt new file mode 100644 index 0000000..93cdd93 --- /dev/null +++ b/app/src/main/java/msu/ug/ChoiceFragment.kt @@ -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() + private val taxons = ArrayList() + + override fun onStart() { + super.onStart() + + val from = arrayOf(Const.MAP_TEXT_KEY) + val to = intArrayOf(android.R.id.text1) + val displayData = ArrayList>() + 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>) { + 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() + 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) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/msu/ug/ChoiseFragment.kt b/app/src/main/java/msu/ug/ChoiseFragment.kt deleted file mode 100644 index 21d9baf..0000000 --- a/app/src/main/java/msu/ug/ChoiseFragment.kt +++ /dev/null @@ -1,46 +0,0 @@ -package msu.ug - -import android.content.Context -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 ChoiseFragment(private val actContext: Context) : ListFragment() { - private val storage = Storage(actContext) - - override fun onStart() { - super.onStart() - - val arr = JSONArray(getPlainText(storage.currentChoise.toString())) - - val displayData = ArrayList>(2) - - (0 until arr.length()).forEach {i -> - val map = HashMap() - map["text"] = arr.getJSONObject(i).getString("text") - displayData.add(map) - } - - val from = arrayOf("text") - val to = intArrayOf(android.R.id.text1) - val listAdapter = SimpleAdapter(context, displayData, android.R.layout.simple_list_item_1, from, to) - setListAdapter(listAdapter) - } - - 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() - } -} \ No newline at end of file diff --git a/app/src/main/java/msu/ug/Const.kt b/app/src/main/java/msu/ug/Const.kt new file mode 100644 index 0000000..dd565ba --- /dev/null +++ b/app/src/main/java/msu/ug/Const.kt @@ -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" + } + } +} \ No newline at end of file diff --git a/app/src/main/java/msu/ug/MainActivity.kt b/app/src/main/java/msu/ug/MainActivity.kt index 412a494..30f7460 100644 --- a/app/src/main/java/msu/ug/MainActivity.kt +++ b/app/src/main/java/msu/ug/MainActivity.kt @@ -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() } } diff --git a/app/src/main/java/msu/ug/Storage.kt b/app/src/main/java/msu/ug/Storage.kt index f5889b7..ac3c429 100644 --- a/app/src/main/java/msu/ug/Storage.kt +++ b/app/src/main/java/msu/ug/Storage.kt @@ -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 { + val pathLen = sp.getInt(Const.PATH_LEN_KEY, 0) + val ret = ArrayList() + + 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() + } } \ No newline at end of file