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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/***************************************************************************************************
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 Qt.DotNet;
using System.Collections;
using System.ComponentModel;
using UserViewLib;
namespace UserViewQml
{
public class UserListModel : ListModel, IUserList, INotifyPropertyChanged
{
public enum UserItemRoles
{
FullName = Roles.UserRole,
FirstName, LastName, Email, Thumbnail, Picture, BirthDate, Age
}
private readonly object CriticalSection = new();
public event PropertyChangedEventHandler PropertyChanged;
private UserList Users { get; set; } = new();
public int Count { get; private set; } = 0;
public void Add(User user, int index = -1)
{
if (user == null)
return;
if (index < 0 || index > Count)
index = Count;
BeginInsertRows(ModelIndex.Empty, index, index);
Users.Add(user, index);
Count = Users.Count;
EndInsertRows();
PropertyChanged?.Invoke(this, new(nameof(Count)));
}
public void AddRange(IList<User> users, int index = -1)
{
if (users is null or { Count: 0 })
return;
if (index < 0 || index > Count)
index = Count;
BeginInsertRows(ModelIndex.Empty, index, index + users.Count - 1);
foreach (var user in users)
Users.Add(user, index++);
Count = Users.Count;
EndInsertRows();
PropertyChanged?.Invoke(this, new(nameof(Count)));
}
public void RemoveAt(int index)
{
if (index < 0 || index >= Count)
return;
BeginRemoveRows(ModelIndex.Empty, index, index);
Users.RemoveAt(index);
Count = Users.Count;
EndRemoveRows();
PropertyChanged?.Invoke(this, new(nameof(Count)));
}
public User RemoveRandom(Random random)
{
User oldUser = null;
if (!Users.Any())
return null;
int index = random.Next(Users.Count);
if (index < 0 || index >= Count)
return null;
oldUser = Users.ElementAt(index);
BeginRemoveRows(ModelIndex.Empty, index, index);
Users.RemoveAt(index);
Count = Users.Count;
EndRemoveRows();
PropertyChanged?.Invoke(this, new(nameof(Count)));
return oldUser;
}
public int BinarySearch(User user, IComparer<User> comparer)
{
return Users.BinarySearch(user, comparer);
}
public IEnumerator<User> GetEnumerator()
{
return Users.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Users.GetEnumerator();
}
public Dictionary<int, string> RoleMap { get; } = new()
{
{ (int)UserItemRoles.FullName, "fullName" },
{ (int)UserItemRoles.FirstName, "firstName" },
{ (int)UserItemRoles.LastName, "lastName" },
{ (int)UserItemRoles.Email, "email" },
{ (int)UserItemRoles.Thumbnail, "thumbnail" },
{ (int)UserItemRoles.Picture, "picture" },
{ (int)UserItemRoles.BirthDate, "birthDate" },
{ (int)UserItemRoles.Age, "age" }
};
public override Dictionary<int, string> RoleNames()
{
return RoleMap;
}
public override object Data(ModelIndex index, int role)
{
var users = Users.ToList();
if (index.Row >= users.Count)
return null;
var user = users.ElementAt(index.Row);
return role switch
{
(int)Roles.DisplayRole or
(int)UserItemRoles.FullName => user.Name.Full,
(int)UserItemRoles.FirstName => user.Name.First,
(int)UserItemRoles.LastName => user.Name.Last,
(int)UserItemRoles.Email => user.Email,
(int)UserItemRoles.Thumbnail => user.Picture.Thumbnail,
(int)UserItemRoles.Picture => user.Picture.Large,
(int)UserItemRoles.BirthDate => user.Birth.Date,
(int)UserItemRoles.Age => user.Age,
_ => null
};
}
public override int RowCount(ModelIndex parent)
{
return Count;
}
}
}
|