Skip to content

Commit

Permalink
to start and back buttons working
Browse files Browse the repository at this point in the history
  • Loading branch information
RedSnail committed Jun 17, 2019
1 parent a30b2a4 commit 6ea21f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 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
10 changes: 8 additions & 2 deletions app/src/main/java/msu/ug/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ class MainActivity : AppCompatActivity() {
transaction.replace(R.id.fragment_frame, ChoiceFragment(this))
transaction.commit()

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

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()
}
}
}
19 changes: 12 additions & 7 deletions app/src/main/java/msu/ug/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Storage (context : Context) {
fun addSwitchListener(listener : () -> Unit) {
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()
}
}
Expand All @@ -30,12 +32,10 @@ class Storage (context : Context) {
return sp.getInt(Const.PATH_LEN_KEY, 0)
}

var currentChoise : Int
var currentChoice : Int
set(value) {
if (value > 0) {
sp.edit().putInt(Const.stepKey(stepsNum + 1), value).apply()
stepsNum += 1
}
sp.edit().putInt(Const.stepKey(stepsNum + 1), value).apply()
stepsNum += 1
}
get() {
return sp.getInt(Const.stepKey(stepsNum), 1)
Expand All @@ -61,8 +61,8 @@ class Storage (context : Context) {
}

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

Expand All @@ -73,4 +73,9 @@ class Storage (context : Context) {
false
}
}

fun toStart() {
pathLen = 0
stepsNum = 0
}
}
20 changes: 15 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@
android:layout_weight="0.9">

</FrameLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back"
android:layout_gravity="center"
android:id="@+id/back_button"/>
<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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<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 6ea21f0

Please sign in to comment.