Add functionality to the Settings 'Automatically close BiTSy after 3 minutes of inactivity', the user selection is saved into the SharedPreferences and if it happens to be true then the app will close when that time amount of inactivity happens.

This commit is contained in:
Severiano Jaramillo 2018-12-13 14:29:32 -06:00
parent e1c32eb7f8
commit f45d9055c3
4 changed files with 51 additions and 14 deletions

View file

@ -1,7 +1,7 @@
package cy.agorise.bitsybitshareswallet.activities
import android.content.res.Resources
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceManager
import android.view.MenuItem
import androidx.navigation.findNavController
@ -21,6 +21,10 @@ class MainActivity : ConnectedActivity() {
private lateinit var appBarConfiguration : AppBarConfiguration
// Handler and Runnable used to add a timer for user inaction and close the app if enough time has passed
private lateinit var mHandler: Handler
private lateinit var mRunnable: Runnable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Sets the theme to night mode if it has been selected by the user
@ -42,13 +46,33 @@ class MainActivity : ConnectedActivity() {
setupActionBarWithNavController(navController, appBarConfiguration)
navController.addOnDestinationChangedListener { _, destination, _ ->
val dest: String = try {
resources.getResourceName(destination.id)
} catch (e: Resources.NotFoundException) {
Integer.toString(destination.id)
mHandler = Handler()
mRunnable = Runnable {
if (PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, false))
finish()
else
restartHandler()
}
startHandler()
}
override fun onUserInteraction() {
super.onUserInteraction()
restartHandler()
}
private fun restartHandler() {
stopHandler()
startHandler()
}
private fun stopHandler() {
mHandler.removeCallbacks(mRunnable)
}
private fun startHandler() {
mHandler.postDelayed(mRunnable, 3 * 60 * 1000) //for 3 minutes
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View file

@ -25,9 +25,23 @@ class SettingsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initAutoCloseSwitch()
initNightModeSwitch()
}
private fun initAutoCloseSwitch() {
val autoCloseOn = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, false)
switchAutoClose.isChecked = autoCloseOn
switchAutoClose.setOnCheckedChangeListener { buttonView, isChecked ->
PreferenceManager.getDefaultSharedPreferences(buttonView.context).edit()
.putBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, isChecked).apply()
}
}
private fun initNightModeSwitch() {
val nightModeOn = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(Constants.KEY_NIGHT_MODE_ACTIVATED, false)

View file

@ -44,16 +44,15 @@ object Constants {
*/
const val MISSING_PAYMENT_CHECK_PERIOD: Long = 5000
/**
* Time period to wait to send a request to the NetworkService, and retry in case it is still not connected
*/
/** Time period to wait to send a request to the NetworkService, and retry in case it is still not connected */
const val NETWORK_SERVICE_RETRY_PERIOD: Long = 5000
/**
* Key used to store the number of operations that the currently selected account had last time we checked
*/
/** Key used to store the number of operations that the currently selected account had last time we checked */
const val KEY_ACCOUNT_OPERATION_COUNT = "key_account_operation_count"
/** Key used to store the auto close app if no user activity setting into the shared preferences */
const val KEY_AUTO_CLOSE_ACTIVATED = "key_auto_close_activated"
/** Key used to store the night mode setting into the shared preferences */
const val KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated"
}

View file

@ -19,7 +19,7 @@
android:textStyle="bold"/>
<Switch
android:id="@+id/switchCloseTimer"
android:id="@+id/switchAutoClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_different_topic"