mardi 25 janvier 2022

Design pattern for checking two modes (online/offline) in multiple places

I'm working on a Xamarin application. Currently the app works in Offline mode. In Offline mode, user download a SQLite database from a server. The app then queries data from this SQLite database to display and performs CRUD actions. Changes will be synced to server when user clicks sync button.

Now, I want to implement an Online mode. In Online mode, whenever the user go to a screen, it will make a WebApi request to the server. Same goes for CRUD actions.

UI will be the same in two modes (Online/Offline). User can choose which mode they want to work on. I want to prevent the use of mode checking conditional statement in all places.

For example, when user goes to screen A conditional statement will looks like

if (mode == "Online") {
  getDataOnline();
} else if (mode == "Offline") {
  getDataOffline();
}

This check will be required in most places.

What would be a design pattern or best way to achieve this?

Aucun commentaire:

Enregistrer un commentaire