Skip to content

Commit

Permalink
Merge pull request #41 from SomeAnonimCoder/back_button
Browse files Browse the repository at this point in the history
Back button
  • Loading branch information
RedSnail authored Jun 17, 2019
2 parents f323ef9 + 6ea21f0 commit 8238840
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 31 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/msu/ug/ChoiceFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class ChoiceFragment(private val actContext: Context) : ListFragment() {
super.onListItemClick(l, v, position, id)
if (toIndices[position] == 0) {
storage.appendPath(taxons[position])
storage.currentChoise = 1
storage.currentChoice = 1
} else {
storage.currentChoise = toIndices[position]
storage.currentChoice = toIndices[position]
}

}
Expand All @@ -60,7 +60,7 @@ class ChoiceFragment(private val actContext: Context) : ListFragment() {
if (path == "/") {
path = ""
}
val file = path + storage.currentChoise.toString()
val file = path + storage.currentChoice.toString()

val arr = JSONArray(getPlainText(file))
toIndices.clear()
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/msu/ug/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package msu.ug

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

const val DEFAULT_TAXON = "Nihil"
Expand All @@ -17,5 +17,9 @@ class Const {
fun folderKey(folderIndex : Int) : String {
return "folder_$folderIndex"
}

fun stepKey(stepIndex : Int) : String {
return "step_$stepIndex"
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/msu/ug/IntroActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class IntroActivity : AppCompatActivity() {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setContentView(R.layout.activity_intro)
val context:Context = this

val classButton = findViewById<Button>(R.id.button_classifier)
classButton.setOnClickListener {
val intent = Intent(context, MainActivity::class.java)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
val aboutButton = findViewById<Button>(R.id.button_about)
aboutButton.setOnClickListener {
val intent = Intent(context, AboutActivity::class.java)
val intent = Intent(this, AboutActivity::class.java)
startActivity(intent)
}

Expand Down
21 changes: 19 additions & 2 deletions app/src/main/java/msu/ug/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package msu.ug
import android.content.pm.ActivityInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.view.View
import android.widget.Button
import android.widget.Toast


class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Expand All @@ -16,5 +17,21 @@ class MainActivity : AppCompatActivity() {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_frame, ChoiceFragment(this))
transaction.commit()

val storage = Storage(this)

val backButton = findViewById<Button>(R.id.back_button)

backButton.setOnClickListener {
if (!storage.goBack()) {
Toast.makeText(this, "Вы в начале", Toast.LENGTH_SHORT).show()
}
}

val toStartButton = findViewById<Button>(R.id.to_start_button)

toStartButton.setOnClickListener {
storage.toStart()
}
}
}
61 changes: 48 additions & 13 deletions app/src/main/java/msu/ug/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,40 @@ import android.content.SharedPreferences

class Storage (context : Context) {
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)
sp.registerOnSharedPreferenceChangeListener { _, s ->
if (s == Const.STEPS_NUM_KEY) {
//important: UI updates only after stepsNum changes.
// That means, to get the proper UI you need first update pathLen and then update stepsNum
listener.invoke()
}
}
}

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

private var pathLen : Int
set(value) {
sp.edit().putInt(Const.PATH_LEN_KEY, value).apply()
}
get() {
return sp.getInt(Const.PATH_LEN_KEY, 0)
}

var currentChoice : Int
set(value) {
sp.edit().putInt(Const.stepKey(stepsNum + 1), value).apply()
stepsNum += 1
}
get() {
return sp.getInt(Const.stepKey(stepsNum), 1)
}

fun getPath() : List<String> {
Expand All @@ -39,8 +56,26 @@ class Storage (context : Context) {
}

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()
sp.edit().putString(Const.folderKey(pathLen), folder).apply()
pathLen += 1
}

fun goBack() : Boolean {
return if (stepsNum > 0) {
if (currentChoice == 1) {
pathLen -= 1
}

stepsNum -= 1

true
} else {
false
}
}

fun toStart() {
pathLen = 0
stepsNum = 0
}
}
35 changes: 26 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_frame" android:background="#4CAF50">
android:layout_height="0dp"
android:id="@+id/fragment_frame"
android:background="#4CAF50"
android:layout_weight="0.9">

</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/back"
android:id="@+id/back_button"
android:layout_weight="1"/>

<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/to_start"
android:id="@+id/to_start_button"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<string name="name">The Big\nMayevsky</string>
<string name="classifier_button">Определитель</string>
<string name="about_button">О программе</string>
<string name="back">Назад</string>
<string name="to_start">К началу</string>
<string name="copyrights">There will be copyright</string>

</resources>

0 comments on commit 8238840

Please sign in to comment.