Skip to content

Commit

Permalink
shared prefs listener
Browse files Browse the repository at this point in the history
context init in onCreate
  • Loading branch information
RedSnail committed Jun 17, 2019
1 parent 476d523 commit a30b2a4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions app/src/main/java/msu/ug/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ 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() {
val storage = Storage(this)

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

fun onBackClick(view : View) {
if (!storage.goBack()) {
Toast.makeText(this, "Вы в начале", Toast.LENGTH_SHORT).show()
val backButton = findViewById<Button>(R.id.back_button)

val storage = Storage(this)

backButton.setOnClickListener {
if (!storage.goBack()) {
Toast.makeText(this, "Вы в начале", Toast.LENGTH_SHORT).show()
}
}
}
}
17 changes: 8 additions & 9 deletions app/src/main/java/msu/ug/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ 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) {
listener.invoke()
}
}
}

private var stepsNum : Int
Expand All @@ -30,11 +33,8 @@ class Storage (context : Context) {
var currentChoise : Int
set(value) {
if (value > 0) {
sp.edit().putInt(Const.stepKey(stepsNum + 1), value).apply()
stepsNum += 1
sp.edit().putInt(Const.stepKey(stepsNum), value).apply()
switchListeners.forEach {
it.invoke()
}
}
}
get() {
Expand All @@ -56,9 +56,8 @@ 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 {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
android:layout_height="wrap_content"
android:text="@string/back"
android:layout_gravity="center"
android:onClick="onBackClick"/>
android:id="@+id/back_button"/>
</LinearLayout>

0 comments on commit a30b2a4

Please sign in to comment.