Arxcis / prog2007-gruppearbeid

Prosjekt inkluderer obligatorisk gruppearbeid til faget PROG-2007 - Mobile Programming.
The Unlicense
0 stars 0 forks source link

Re-try failed network calls when Wifi connection is re-established #7

Open Arxcis opened 3 years ago

Arxcis commented 3 years ago

Why?

Based on feedback on a review comment here - https://github.com/Arxcis/prog2007-gruppearbeid/pull/6#pullrequestreview-810290721

Potential solution from https://stackoverflow.com/a/5890553

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
        if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
            //do stuff
        } else {
            // wifi connection was lost
        }
    }
}
Bibli2311 commented 3 years ago

Well in the code WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION is used which is deprecated. (https://developer.android.com/reference/android/net/wifi/WifiManager#SUPPLICANT_CONNECTION_CHANGE_ACTION). The link also says "This is no longer supported" so it looks like it is outdated.

Arxcis commented 2 years ago

Ok. Good catch @Bibli2311 . Another option is to display a refresh-button to the user, so user can refresh whenever user feels like

Bibli2311 commented 2 years ago

Yeah, that sounds like a good idea. I can start implementing it today.

Bibli2311 commented 2 years ago

Bug with refresh button from testInternetConnection branch

Background info: I have made a fragment which contains the refresh button. The refresh button has a click-listener which looks like this:

mainXML.btnRefresh.setOnClickListener{
            when(currentActivity.activity)
            {
                "Films" ->(activity as FilmsActivity).fetchFilms()
                "People" -> (activity as PeopleActivity).fetchPeople()
                null -> Log.d("buttonRefresh", "currentActivity is null")
            }
}

.fetchFilms() and .fetchPeople() refetches data from the API and updates the UI. To determine if .fetchFilms() or .fetchPeople() should be called I am using the "currentActivity" class. This class updates a string whenever an Activity is used. This class updates its string this way in the Activities:

class Activity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {...}
    override fun onResume() {
        super.onResume()
        currentActivity.activity = title.toString()
        configureBottomNavigation(this, FilmsNavigation, R.id.FilmsMenuItem)
    }

    override fun onPause() {
        super.onPause()
        currentActivity.activity = null
    }

    override fun onDestroy() {
        super.onDestroy()
        currentActivity.activity = null
    }
}

Action performed to find bug

The actions are listed with log messages which are called from the setter in the currentActivity class and in the ButtonRefresh.kt file which controls the refresh button.

  1. Click on Films Activity on bottom navigation bar (with WIFI enabled)
    Log (currentActivity): currentActivity: currentActivity is Films
  2. Disables WIFI
  3. Click on Films Activity on bottom navigation bar (again)
    Log: currentActivity (currentActivity): currentActivity is Films     //currentActivity is set to Films again since onResume() is called again
  4. Click the refresh button
    Log BUG (ButtonRefresh): buttonRefresh: currentActivity is null

    No data is updated even after I have enabled the WIFI again.