Strypper / mauisland

MAUIsland 🏝️ is the number 1 controls gallery for .NET MAUI
MIT License
198 stars 14 forks source link

Support local database #168

Closed Strypper closed 9 months ago

Strypper commented 9 months ago

Description

Starting from 2024, MAUIsland will incorporate local database support to optimize API usage, particularly for reducing calls to external APIs such as the GitHub API. As part of this enhancement, we will introduce a local database functionality into each installation of the application.

Upon the initial launch of the app, each time the Community controls are accessed, it will retrieve all the necessary data and store it in the local database under the table named 'CardInfo.' After successfully saving all card information locally, the application will then update the 'ApplicationLastUpdate' field within the 'CardsSync' table. This field is responsible for tracking the timestamp of the last card data update, considering that GitHub cards exhibit dynamic data changes over time.

To ensure efficient data retrieval and prevent unnecessary API calls, the application will compare the current time with the 'ApplicationLastUpdate' timestamp. If more than one hour has elapsed since the last update, the app will initiate a data refresh by retrieving the updated information from the GitHub API. This proactive approach not only significantly reduces the frequency of API calls but also safeguards against potential rate limiter issues that could arise from excessive or unnecessary external API requests.

Public API Changes

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        int count = 0;
        private readonly ICustomersService _customersService;
        private readonly ILocalControlService _localControlService;
        public MainPage(ICustomersService customersService,
                        ILocalControlService localControlService)
        {
            InitializeComponent();
            _customersService = customersService;
            _localControlService = localControlService;
            RefreshAsync();
        }

        async Task RefreshAsync()
        {
            var controls = await _localControlService.GetAllAsync();
            TestList.ItemsSource = controls.Select(x => x.ControlName).ToList();
        }

        private async void OnCounterClicked(object sender, EventArgs e)
        {
            count++;

            if (count == 1)
                CounterBtn.Text = $"Clicked {count} time";
            else
                CounterBtn.Text = $"Clicked {count} times";

            SemanticScreenReader.Announce(CounterBtn.Text);
            await _localControlService.AddAsync(new CardInfo { ControlName = $"Test {count}" });
            await RefreshAsync();
        }

        private async void CustomerBtn_Clicked(object sender, EventArgs e)
        {

            string answer = await DisplayPromptAsync("Hello", "What's your name?", placeholder: "Type your name");
            if (answer != null)
            {
                await _customersService.AddAsync(new CustomerInfo { Name = answer });
                await DisplayAlert("Welcome", $"Hello, {answer}", "Cancel");
            }
        }

        private async void ShowAllCustomers_Clicked(object sender, EventArgs e)
        {
            var customers = await _customersService.GetAllAsync();
            string answer = await DisplayActionSheet("See all customers", "Cancel", null, customers.Select(x => x.Name).ToArray());
            if (answer != "Cancel")
            {
                await DisplayAlert("Answer", $"{answer} is great!", "OK");
            }
        }
    }

}

Intended Use-Case

Save lazy data to local database, reduce server calling overhead