aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/PaginatedResource.cs
blob: 52302eca5612f24ac11587b21cbe6bdbe2638535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.ComponentModel;

namespace ColorPalette
{
    public sealed partial class PaginatedResource : AbstractResource, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string Path { get; set; }

        private int _Page = 1;
        public int Page
        {
            get => _Page;
            set
            {
                if (_Page == value || value < 1)
                    return;
                _Page = value;
                PropertyChanged?.Invoke(this, new(nameof(Page)));
                RefreshCurrentPage();
            }
        }

        public int Pages { get; private set; } = 0;

        public void RefreshCurrentPage()
        {
            if (ResourceApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                var response = Task.Run(() => ResourceApi.RefreshAsync(Path, Page)).Result;
                Pages = response.TotalPages;
                _Page = response.CurrentPage;
                RefreshData(response.Data);
                PropertyChanged?.Invoke(this, new(nameof(Pages)));
                PropertyChanged?.Invoke(this, new(nameof(Page)));
                PropertyChanged?.Invoke(this, new(nameof(Data)));
            } catch (Exception e) {
                HandleRequestException(e, apiException =>
                {
                    if (Page != 1) {
                        // A failed refresh. If we weren't on page 1, try that.
                        // Last resource on currentPage might have been deleted, causing a failure
                        Page = 1;
                    } else {
                        // Refresh failed and we're already on page 1 => clear data
                        ErrorMsg($"Refresh failed: {apiException.Message}");
                        Pages = 0;
                        PropertyChanged?.Invoke(this, new(nameof(Pages)));
                        if (Resources.Any()) {
                            Resources.Clear();
                            PropertyChanged?.Invoke(this, new(nameof(Data)));
                        }
                    }
                });
            }
        }

        public void Add(ResourceData resource)
        {
            if (ResourceApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                _ = Task.Run(() => ResourceApi
                    .AddAsync(Path, resource))
                    .Result;
                RefreshCurrentPage();
            } catch (Exception e) {
                HandleRequestException(e, apiException =>
                {
                    ErrorMsg($"Add failed: {apiException.Message}");
                });
            }
        }

        public void Update(int resourceId, ResourceData resource)
        {
            if (ResourceApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                _ = Task.Run(() => ResourceApi
                    .UpdateAsync(Path, resourceId, resource))
                    .Result;
                RefreshCurrentPage();
            } catch (Exception e) {
                HandleRequestException(e, apiException =>
                {
                    ErrorMsg($"Update failed: {apiException.Message}");
                });
            }
        }

        public void Remove(int resourceId)
        {
            if (ResourceApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                _ = Task.Run(() => ResourceApi
                    .RemoveAsync(Path, resourceId))
                    .Result;
                RefreshCurrentPage();
            } catch (Exception e) {
                HandleRequestException(e, apiException =>
                {
                    ErrorMsg($"Remove failed: {apiException.Message}");
                });
            }
        }
    }
}