Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Xamarin.Forms Projects

You're reading from   Xamarin.Forms Projects Build multiplatform mobile apps and a game from scratch using C# and Visual Studio 2019

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781839210051
Length 504 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
 Hindrikes Hindrikes
Author Profile Icon Hindrikes
Hindrikes
 Karlsson Karlsson
Author Profile Icon Karlsson
Karlsson
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Xamarin 2. Building Our First Xamarin.Forms App FREE CHAPTER 3. Building a News App Using Xamarin.Forms Shell 4. A Matchmaking App with a Rich UX Using Animations 5. Building a Photo Gallery App Using CollectionView and CarouselView 6. Building a Location Tracking App Using GPS and Maps 7. Building a Weather App for Multiple Form Factors 8. Setting Up a Backend for a Chat App Using Azure Services 9. Building a Real-Time Chat Application 10. Creating an Augmented Reality Game 11. Hot Dog or Not Hot Dog Using Machine Learning 12. Other Books You May Enjoy

Creating a repository and its interface

Now that we have the TodoItem class, let's define an interface that describes a repository to store our to-do list items:

  1. In the .NET Standard library project, create a folder called Repositories.
  2. Create an interface called ITodoItemRepository.cs in the Repositories folder and write the following code:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DoToo.Models;


namespace DoToo.Repositories
{
public interface ITodoItemRepository
{
event EventHandler<TodoItem> OnItemAdded;
event EventHandler<TodoItem> OnItemUpdated;

Task<List<TodoItem>> GetItems();
Task AddItem(TodoItem item);
Task UpdateItem(TodoItem item);
Task AddOrUpdate(TodoItem item);
}
}
The eagle-eyed among you might notice that we are not defining a Delete method in this interface. This is definitely something that should be there in a real-world app. While the app that we are creating in this chapter does not support deleting items, we are quite sure that you could add this yourself if you want to!

This interface defines everything we need for our app. It is there to create logical insulation between your implementation of a repository and the user of that repository. If any other parts of your application want an instance of TodoItemRepository, we can pass it an object that implements ITodoItemRepository, regardless of how it's implemented.

Having that said, let's implement ITodoItemRepository:

  1. Create a class called TodoItemRepository.cs.
  2. Enter the following code:
using DoToo.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace DoToo.Repositories
{
public class TodoItemRepository : ITodoItemRepository
{
public event EventHandler<TodoItem> OnItemAdded;
public event EventHandler<TodoItem> OnItemUpdated;

public async Task<List<TodoItem>> GetItems()
{
return null; // Just to make it build
}

public async Task AddItem(TodoItem item)
{
}

public async Task UpdateItem(TodoItem item)
{
}

public async Task AddOrUpdate(TodoItem item)
{
if (item.Id == 0)
{
await AddItem(item);
}
else
{
await UpdateItem(item);
}
}
}
}

This code is the bare-bones implementation of the interface, except for the AddOrUpdate(...) method. This handles a small piece of logic that states that if the ID of an item is 0, it's a new item. Any item with an ID value greater than 0 is stored in the database. This is because the database assigns a value larger than 0 when we create rows in a table.

There are also two events defined in the preceding code. They will be used to notify subscribers of a list that items have been updated or added.

You have been reading a chapter from
Xamarin.Forms Projects - Second Edition
Published in: Jun 2020
Publisher: Packt
ISBN-13: 9781839210051
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images