Exporting from MAUI

Mobile app users typically do not have access to a command-line interface to export data from their devices. Instead, you can provide a user-friendly way to export facts from your MAUI application and share them with others.

Use the ExportFactsToJson and ExportFactsToFactual methods in a MAUI application to export facts. In order for the user to get their facts off the device, they will need some mechanism to share them. Since they are just text files, you can call Share.Default.RequestAsync within the application to share the exported facts.

Export from a view model

Supposing that you have configured dependency injection in your MAUI application, you can inject the JinagaClient instance into a view model. Then, you can use the ExportFactsToJson and ExportFactsToFactual methods to export facts to a temporary file and share it using the Share API.

public partial class AccountViewModel : ObservableObject
{
    private readonly JinagaClient jinagaClient;

    public ICommand ExportFactsCommand { get; }

    public AccountViewModel(JinagaClient jinagaClient)
    {
        this.jinagaClient = jinagaClient;

        ExportFactsCommand = new AsyncRelayCommand(HandleExportFacts);
    }

    private async Task HandleExportFacts()
    {
        var tempFile = Path.GetTempFileName();
        try
        {
            // Export facts to the temporary file
            using (var fileStream = File.OpenWrite(tempFile))
            {
                await jinagaClient.ExportFactsToFactual(fileStream);
            }

            // Share the file
            await Share.Default.RequestAsync(new ShareFileRequest
            {
                Title = "Exported Facts (Factual)",
                File = new ShareFile(tempFile)
            });

            // RequestAsync does not wait for the user to complete the share operation.
            // We therefore cannot delete the temporary file here.
        }
        catch (Exception ex)
        {
            // Clean up the temporary file if something went wrong
            if (File.Exists(tempFile))
                File.Delete(tempFile);
            throw;
        }
    }
}

Continue With

Jinaga is a product of Jinaga LLC.

Michael L Perry, President