A task sequence is the concept of having a task flow that the application can run sequentially. One common scenario to use this is running several tasks while showing the splash screen.
In this article, I’m going to show you how to do that in Xamarin Forms/MAUI. This series will be divided into the following parts:
- Part 1: How to build a task sequence
- Part 2: Start in a specific task, skip tasks, conditional tasks, etc
- Part 3: Build a task sequence using Prism
Advantage of using a task sequence
The main advantages of building a sequence are:
- Centralized and pre-defined flow of what should happen in an ordered step by step sequence.
- Skip tasks based on specific conditions (Ex, if there is no new version available you can skip that step or the Onboarding page will run the first time the user opens the app).
- Start running the sequence from a specific task (Ex. If the user closes the application running a task, you can save the state and open the application in that specific state).
Use case
Show the following task sequence when application starts.
Let’s do it step by step
1. Define the flow
Let’s define the task flow:
- Download initial data in the background Data that needs to be downloaded before entering application. (For this example, we will just do a delay simulating data download)
- Update version check Optional task that should only run when there is a newer version available in the store.
- Onboarding screen Optional task that should only run the first time application is launched.
- Request App Permissions Optional task that should only run if user has not allowed/denied the required permissions yet.
- Authentication Optional task and should only run if the user is not authenticated or session expired.
- Advertising Runs an advertisement video before entering application (The user can skip this step manually).
- Home
2. Create a common interface
Let’s create an interface that will represent all the tasks in our flow:
3. Create the Splash Screen
For this use case, we will run the task sequence from a splash screen. Instead of using the default native splash screen, we are going to create custom splash screen.
4. Create the Tasks
Not all task in the sequence are pages, some will run without showing a page. For example, SimulateDownloadDataStartupTask and UpdateVersionStartupTask.
Now let’s create the pages:
Create a base page to implement the IStartupTask interface:
When a page that inherits from StartupPage finish it purpose will call the CompleteAsync method, that will let know our sequence that task has completed.
5. Let’s build the sequencer
Define the interface that represents a the task sequencer.
The task sequencer purpose is to start running the sequence and coordinate each task run. We can create the task sequencer by using the builder pattern.
6. Let’s start the sequence
On the SplashPage, create the StartupTaskBuilder with the tasks that will be part of the sequence. The builder will return the sequencer that will be used to start the task sequence.
Result
Stay tuned for the next part of this series where you’ll see how to start running the sequence from a specific task, skip tasks, conditionate task run, etc.
Check the full source code here.
Happy Task Sequence!
The post Building a Task Sequence in Xamarin Forms/ MAUI (Part 1) appeared first on Xamboy.