Commit 5aff7fd9 authored by Christophe Henry's avatar Christophe Henry

Save UI states across activity restarts

parent 3a4f0937
......@@ -68,7 +68,7 @@ dependencies {
implementation "com.android.support:support-compat:$android_support_version"
// AndroidX layout
implementation "androidx.appcompat:appcompat:1.0.0-beta01"
implementation "androidx.appcompat:appcompat:1.1.0-alpha02"
implementation "androidx.core:core-ktx:1.1.0-alpha03"
implementation "com.google.android.material:material:1.0.0-beta01"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
......
......@@ -5,6 +5,7 @@
package="fr.chenry.android.freshrss">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<dist:module dist:instant="true" />
......@@ -28,7 +29,8 @@
<activity android:name=".activities.TestActivity"></activity>
<activity
android:name=".activities.LoginActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:persistableMode="persistAcrossReboots">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
......
......@@ -6,16 +6,16 @@ import android.os.Handler
import android.os.IBinder
import androidx.core.app.NotificationManagerCompat
import androidx.core.os.postDelayed
import androidx.lifecycle.Observer
import com.facebook.stetho.Stetho
import fr.chenry.android.freshrss.RefresherService.RefresherBinder
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.database.FreshRSSDabatabase
import fr.chenry.android.freshrss.utils.Try
import fr.chenry.android.freshrss.utils.whenNotNull
import nl.komponents.kovenant.android.startKovenant
import nl.komponents.kovenant.android.stopKovenant
import nl.komponents.kovenant.deferred
import java.util.*
import java.util.Properties
class FreshRSSApplication: Application() {
private val refreshDelay: Long get() = 30
......@@ -57,12 +57,15 @@ class FreshRSSApplication: Application() {
val notificationManager: NotificationManagerCompat
get() = NotificationManagerCompat.from(FreshRSSApplication.application)
val stateSharedPreferences: SharedPreferences get() =
context.getSharedPreferences("STATE", Context.MODE_PRIVATE)
fun getStringR(id: Int) = application.resources.getString(id)
}
inner class RefresherServiceConnection: ServiceConnection {
private val handler = Handler()
private val token = object{}
private val token = object {}
override fun onServiceDisconnected(name: ComponentName?) {
handler.removeCallbacksAndMessages(token)
......
package fr.chenry.android.freshrss.activities
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import fr.chenry.android.freshrss.*
import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.utils.*
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.resolve
......@@ -15,6 +18,7 @@ import nl.komponents.kovenant.ui.successUi
class MainActivity: AppCompatActivity() {
private val SUBSCRIPTION_SECTION = "SUBSCRIPTION_SECTION"
private val deferred = deferred<Unit, Exception>()
private val navigation: NavController by lazy {
Navigation.findNavController(this, R.id.main_activity_host_fragment)
......@@ -29,16 +33,51 @@ class MainActivity: AppCompatActivity() {
}
}
override fun onStart() {
super.onStart()
deferred.promise failUi {this.e(it)}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
restoreState()
setContentView(R.layout.activity_main)
setupActionBarWithNavController(navigation, appBarConfiguration)
}
override fun onStart() {
super.onStart()
deferred.promise failUi {this.e(it)}
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)
}
override fun onSupportNavigateUp() = navigation.navigateUp() || super.onSupportNavigateUp()
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()
}
}
......@@ -4,7 +4,6 @@ import android.content.Intent
import android.os.Bundle
import android.view.*
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.ShareActionProvider
......@@ -12,9 +11,9 @@ import androidx.core.view.MenuItemCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import fr.chenry.android.freshrss.R
import fr.chenry.android.freshrss.store.database.FreshRSSDabatabase
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.database.models.*
import fr.chenry.android.freshrss.store.database.models.Article
import fr.chenry.android.freshrss.store.database.models.ReadStatus
import fr.chenry.android.freshrss.store.database.models.ReadStatus.UNREAD
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.SubscriptionArticlesVM
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.SubscriptionArticlesVMF
......@@ -50,14 +49,12 @@ class SubscriptionArticlesDetailFragment: Fragment() {
return inflater.inflate(R.layout.fragment_subscription_article_detail, container, false)
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.article_actionbar, menu)
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.article_actionbar, menu)
if(menu != null) {
setUpReadStatusButton(menu)
setupOpenInBrowser(menu)
setupShareAction(menu)
}
setUpReadStatusButton(menu)
setupOpenInBrowser(menu)
setupShareAction(menu)
super.onCreateOptionsMenu(menu, inflater)
}
......
......@@ -34,10 +34,12 @@ class MainSubscriptionFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main_subscription, container, false)
activity?.supportFragmentManager
?.beginTransaction()
?.add(R.id.subcription_fragment_container, Fragment.instantiate(activity, fragment.qualifiedName))
?.commit()
activity.whenNotNull {
it.supportFragmentManager
.beginTransaction()
.add(R.id.subcription_fragment_container, Fragment.instantiate(it, fragment.qualifiedName!!))
.commit()
}
return view
}
......@@ -58,17 +60,19 @@ class MainSubscriptionFragment: Fragment() {
else -> SubscriptionSection.ALL
}
activity?.supportFragmentManager
?.beginTransaction()
?.replace(R.id.subcription_fragment_container, Fragment.instantiate(activity, fragment.qualifiedName))
?.commit()
activity.whenNotNull {ac ->
ac.supportFragmentManager
.beginTransaction()
.replace(R.id.subcription_fragment_container, Fragment.instantiate(ac, fragment.qualifiedName!!))
.commit()
}
true
}
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.main_actionbar, menu)
menu?.findItem(R.id.action_refresh)?.setOnMenuItemClickListener {
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.main_actionbar, menu)
menu.findItem(R.id.action_refresh)?.setOnMenuItemClickListener {
FreshRSSApplication.application.refresherService.whenNotNull {it.refresh()}.let {true}
}
......
package fr.chenry.android.freshrss.components.subscriptions
enum class SubscriptionSection(val key: String) {
FAVORITES("favorites"), ALL("all"), UNREAD("unread");
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
companion object {
fun fromKey(key: String) = when(key) {
FAVORITES.key -> FAVORITES
ALL.key -> ALL
UNREAD.key -> UNREAD
else -> null
}
}
@Parcelize
enum class SubscriptionSection: Parcelable {
FAVORITES, ALL, UNREAD;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment