Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add confirmation on exit #998

Merged
merged 7 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Bundle
import android.util.Log
import android.util.Patterns
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -75,6 +76,8 @@ import com.jerboa.ui.components.settings.account.AccountSettingsViewModel
import com.jerboa.ui.components.settings.account.AccountSettingsViewModelFactory
import com.jerboa.ui.components.settings.lookandfeel.LookAndFeelActivity
import com.jerboa.ui.theme.JerboaTheme
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class JerboaApplication : Application() {
private val database by lazy { AppDB.getDatabase(this) }
Expand All @@ -94,6 +97,8 @@ class MainActivity : AppCompatActivity() {
AppSettingsViewModelFactory((application as JerboaApplication).appSettingsRepository)
}

private var isBackPressedOnce = false

@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -125,6 +130,22 @@ class MainActivity : AppCompatActivity() {
appSettings = appSettings,
) {
val navController = rememberNavController()

onBackPressedDispatcher.addCallback(this) {
val isRoot = navController.previousBackStackEntry == null
if (isRoot && isBackPressedOnce) {
finish()
} else if (isRoot) {
Toast.makeText(ctx, ctx.getText(R.string.back_warning), Toast.LENGTH_SHORT).show()
isBackPressedOnce = true
Executors.newSingleThreadScheduledExecutor().schedule({
isBackPressedOnce = false
}, 2, TimeUnit.SECONDS)
} else {
navController.navigateUp()
}
}

val serverVersionOutdatedViewed = remember { mutableStateOf(false) }

MarkdownHelper.init(
Expand Down
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 @@ -333,4 +333,5 @@
<string name="dialogs_top_nine_month">Top Nine Months</string>
<string name="post_listing_view_source">View Source</string>
<string name="post_listing_view_original">View Original</string>
<string name="back_warning">Repeat action to exit</string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there are other actions besides pressing the back button/gesture equivalent that we would need to be concerned about? If not, "Press back again to exit" sounds more natural to me. Otherwise LGTM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I didn't want to put tap because of the back gesture, but that could work too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya I also like "Press back again to exit"

</resources>