MainActivity.kt 2.27 KB
Newer Older
1 2 3 4
package fr.chenry.android.freshrss.activities

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
5 6 7 8
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
Christophe Henry's avatar
Christophe Henry committed
9 10
import fr.chenry.android.freshrss.FreshRSSApplication
import fr.chenry.android.freshrss.R
11 12
import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection
import fr.chenry.android.freshrss.store.Store
Christophe Henry's avatar
Christophe Henry committed
13
import fr.chenry.android.freshrss.utils.whenNotNull
14

Christophe Henry's avatar
Christophe Henry committed
15
const val SUBSCRIPTION_SECTION = "SUBSCRIPTION_SECTION"
16

17
class MainActivity: AppCompatActivity() {
18 19 20
    private val navigation: NavController by lazy {
        Navigation.findNavController(this, R.id.main_activity_host_fragment)
    }
21

Christophe Henry's avatar
Christophe Henry committed
22
    private val appBarConfiguration by lazy {AppBarConfiguration(navigation.graph)}
23

24 25
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
26
        restoreState()
27
        setContentView(R.layout.activity_main)
28
        setupActionBarWithNavController(navigation, appBarConfiguration)
Christophe Henry's avatar
Christophe Henry committed
29
        FreshRSSApplication.application.refresherService.value.whenNotNull {it.refresh()}
30 31
    }

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    override fun onResume() {
        restoreState()
        super.onResume()
    }

    override fun onPause() {
        saveState()
        super.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        saveState()
        outState.putAll(navigation.saveState())
        super.onSaveInstanceState(outState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        navigation.restoreState(savedInstanceState)
        super.onRestoreInstanceState(savedInstanceState)
51 52
    }

53
    override fun onSupportNavigateUp() = navigation.navigateUp() || super.onSupportNavigateUp()
54 55 56 57 58 59 60 61 62 63 64 65 66

    private fun restoreState() {
        Store.subscriptionsSection.value = SubscriptionSection.valueOf(
            FreshRSSApplication
                .stateSharedPreferences
                .getString(SUBSCRIPTION_SECTION, SubscriptionSection.ALL.name)!!
        )
    }

    private fun saveState() {
        FreshRSSApplication.stateSharedPreferences.edit()
            .putString(SUBSCRIPTION_SECTION, Store.subscriptionsSection.value!!.name).apply()
    }
67
}