Skip to content

Commit

Permalink
only replace the login fragment if we're not currently on the login f…
Browse files Browse the repository at this point in the history
…ragment.

Also, if we get error 400 on refreshing access token, redirect to login screen.
  • Loading branch information
meiron03 committed Oct 1, 2023
1 parent 7745d6f commit bc5775f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import kotlinx.android.synthetic.main.include_main.*
import retrofit.RestAdapter
import retrofit.android.AndroidLog
import retrofit.converter.GsonConverter
import java.util.Calendar

class MainActivity : AppCompatActivity() {
private var tabShowed = false
Expand Down Expand Up @@ -80,6 +81,17 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.setHomeButtonEnabled(false)
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)


// for some testing
val editor = mSharedPrefs.edit()
editor.putString(this.getString(R.string.access_token), "hehe")
editor.putString(this.getString(R.string.refresh_token), "hehe")
val currentTime = Calendar.getInstance().timeInMillis - 100
editor.putLong(this.getString(R.string.token_expires_at), currentTime)
editor.apply()


val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val mainPagerAdapter = MainPagerAdapter(fragmentManager, lifecycle)
Expand Down Expand Up @@ -147,14 +159,20 @@ class MainActivity : AppCompatActivity() {
}

fun startLoginFragment() {

val currentFragment = fragmentManager.findFragmentById(R.id.content_frame)
val fragment: Fragment = LoginFragment()
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
fragmentManager.beginTransaction()

// change the fragment only if we're not already on the login fragment
if (currentFragment == null || currentFragment::class != fragment::class) {
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
main_view_pager.visibility = View.GONE
expandable_bottom_bar.visibility = View.GONE
main_view_pager.visibility = View.GONE
expandable_bottom_bar.visibility = View.GONE
}
}

fun showErrorToast(errorMessage: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {
}

override fun failure(error: RetrofitError) {

Log.e("Accounts", "Error refreshing access token $error")

if (error.response != null && error.response.status == 400) {
mActivity.startLoginFragment()
}

function.invoke()
// mActivity.startLoginFragment()
}
})
}
Expand Down

0 comments on commit bc5775f

Please sign in to comment.