aboutsummaryrefslogtreecommitdiffstats
path: root/examples/UserView/UserViewQml/UserViewApp.cs
blob: f53a1dd48578b61b945323730427e713030971a5 (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
/***************************************************************************************************
 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;
using System.ComponentModel;
using UserViewLib;

using Qt.DotNet;
using Qt.DotNet.Utils;
using Qt.MetaObject;
using Qt.Quick;

namespace UserViewQml
{
    public class UserEventArgs : EventArgs
    {
        public DateTime Timestamp { get; } = DateTime.Now;
        public User User { get; set; }
    }

    public class UserViewApp
    {
        public UserListModel Users { get; private set; } = new();

        public UserViewApp()
        {
            Program.Users = Users;
        }

        public event EventHandler<UserEventArgs> UserAdded;
        public event EventHandler<UserEventArgs> UserRemoved;

        public void Add()
        {
            if (RandomUserService.Fetch(1).FirstOrDefault() is not { } newUser)
                return;
            var index = Users.BinarySearch(newUser, UserComparer.ByLastName);
            if (index < 0) {
                Users.Add(newUser, ~index);
                UserAdded?.Invoke(this, new UserEventArgs { User = newUser });
            }
        }

        public void Remove()
        {
            if (Users.RemoveRandom(random) is not { } oldUser)
                return;
            UserRemoved?.Invoke(this, new UserEventArgs { User = oldUser });
        }

        private readonly Random random = new();
    }
}