Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e10d5093 authored by Sayantan Roychowdhury's avatar Sayantan Roychowdhury
Browse files

issue_5413_2 [WIP]: Provision for optional noAuthRefresh() function in TimeoutFragment.kt

parent 417be7c5
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -70,11 +70,26 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) {
     */
    abstract fun refreshData(authData: AuthData)

    /*
     * Optional function to execute if above refreshData could not be executed because
     * authData could not be obtained from network
     * i.e mainActivityViewModel.authData.value is null.
     *
     * Uses: Used to show cleanapk data if GPlay is unavailable.
     *
     * If returns true, then we do not attempt to refresh the token.
     * If returns false, then after running the function, we attempt to refresh GPlay token.
     */
    open val noAuthRefresh: (() -> Boolean)? = null

    /*
     * Checks if network connectivity is present.
     * -- If yes, then checks if valid authData is present.
     * ---- If yes, then dismiss timeout dialog (if showing) and call refreshData()
     * ---- If no, then request new token data.
     * ---- If no:
     * ------ Run optional noAuthRefresh() function if not null.
     * ------ If it returns false or is null, then refresh GPlay token.
     * ------ Don't do anything if noRefreshAuth() returns true.
     */
    fun refreshDataOrRefreshToken(mainActivityViewModel: MainActivityViewModel) {
        if (mainActivityViewModel.internetConnection.value == true) {
@@ -82,7 +97,11 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) {
                dismissTimeoutDialog()
                refreshData(authData)
            } ?: run {
                if (mainActivityViewModel.authValidity.value != null) { // checking at least authvalidity is checked for once
                if (
                    noAuthRefresh?.invoke() != true &&
                    mainActivityViewModel.authValidity.value != null
                    // checking at least authValidity is checked for once
                ) {
                    mainActivityViewModel.retryFetchingTokenAfterTimeout()
                }
            }