@@ -29,7 +29,8 @@ public class AddContactPageViewModel : ViewModelBase
29
29
public ObservableCollection < PhoneContact > Contacts ;
30
30
private List < PhoneContact > signalContacts ;
31
31
private PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil . GetInstance ( ) ;
32
- private CancellationToken cancellationToken ;
32
+ public MainPageViewModel MainPageVM ;
33
+ public AddContactPage View ;
33
34
34
35
private string _ContactName = "" ;
35
36
public string ContactName
@@ -45,21 +46,91 @@ public string ContactNumber
45
46
set { _ContactNumber = value ; RaisePropertyChanged ( nameof ( ContactNumber ) ) ; }
46
47
}
47
48
49
+ private bool _ContactsVisible = true ;
50
+ public bool ContactsVisible
51
+ {
52
+ get { return _ContactsVisible ; }
53
+ set { _ContactsVisible = value ; RaisePropertyChanged ( nameof ( ContactsVisible ) ) ; }
54
+ }
55
+
56
+ private bool _RefreshingContacts = false ;
57
+ public bool RefreshingContacts
58
+ {
59
+ get { return _RefreshingContacts ; }
60
+ set { _RefreshingContacts = value ; RaisePropertyChanged ( nameof ( RefreshingContacts ) ) ; }
61
+ }
62
+
48
63
public AddContactPageViewModel ( )
49
64
{
50
65
Contacts = new ObservableCollection < PhoneContact > ( ) ;
51
66
signalContacts = new List < PhoneContact > ( ) ;
52
67
}
53
68
69
+ private bool _UIEnabled = true ;
70
+ public bool UIEnabled
71
+ {
72
+ get { return _UIEnabled ; }
73
+ set { _UIEnabled = value ; RaisePropertyChanged ( nameof ( UIEnabled ) ) ; }
74
+ }
75
+
76
+ private bool _AddEnabled = false ;
77
+ public bool AddEnabled
78
+ {
79
+ get { return _AddEnabled ; }
80
+ set { _AddEnabled = value ; RaisePropertyChanged ( nameof ( AddEnabled ) ) ; }
81
+ }
82
+
83
+ private bool validName = false ;
84
+ private bool ValidName
85
+ {
86
+ get { return validName ; }
87
+ set
88
+ {
89
+ validName = value ;
90
+ SetAddEnabled ( ) ;
91
+ }
92
+ }
93
+
94
+ private bool validNumber = false ;
95
+ private bool ValidNumber
96
+ {
97
+ get { return validNumber ; }
98
+ set
99
+ {
100
+ validNumber = value ;
101
+ SetAddEnabled ( ) ;
102
+ }
103
+ }
104
+
54
105
public async Task OnNavigatedTo ( CancellationToken ? cancellationToken = null )
55
106
{
107
+ await RefreshContacts ( cancellationToken ) ;
108
+ }
109
+
110
+ public async Task RefreshContacts ( CancellationToken ? cancellationToken = null )
111
+ {
112
+ RefreshingContacts = true ;
113
+ Contacts . Clear ( ) ;
114
+ signalContacts . Clear ( ) ;
56
115
SignalServiceAccountManager accountManager = new SignalServiceAccountManager ( App . ServiceUrls , App . Store . Username , App . Store . Password , ( int ) App . Store . DeviceId , App . USER_AGENT ) ;
57
116
ContactStore contactStore = await ContactManager . RequestStoreAsync ( ContactStoreAccessType . AllContactsReadOnly ) ;
58
117
List < PhoneContact > intermediateContacts = new List < PhoneContact > ( ) ;
59
118
if ( contactStore != null )
60
119
{
61
120
HashSet < string > seenNumbers = new HashSet < string > ( ) ;
62
121
var contacts = await contactStore . FindContactsAsync ( ) ;
122
+ ContactAnnotationStore contactAnnotationStore = await ContactManager . RequestAnnotationStoreAsync ( ContactAnnotationStoreAccessType . AppAnnotationsReadWrite ) ;
123
+ ContactAnnotationList contactAnnotationList ;
124
+ var contactAnnotationLists = await contactAnnotationStore . FindAnnotationListsAsync ( ) ;
125
+ if ( contactAnnotationLists . Count == 0 )
126
+ {
127
+ contactAnnotationList = await contactAnnotationStore . CreateAnnotationListAsync ( ) ;
128
+ }
129
+ else
130
+ {
131
+ contactAnnotationList = contactAnnotationLists [ 0 ] ;
132
+ }
133
+
63
134
foreach ( var contact in contacts )
64
135
{
65
136
var phones = contact . Phones ;
@@ -82,6 +153,7 @@ public async Task OnNavigatedTo(CancellationToken? cancellationToken = null)
82
153
seenNumbers . Add ( formattedNumber ) ;
83
154
PhoneContact phoneContact = new PhoneContact
84
155
{
156
+ Id = contact . Id ,
85
157
Name = contact . FullName ,
86
158
PhoneNumber = formattedNumber ,
87
159
OnSignal = false
@@ -101,61 +173,41 @@ public async Task OnNavigatedTo(CancellationToken? cancellationToken = null)
101
173
}
102
174
}
103
175
176
+ // check if we've annotated a contact as a Signal contact already, if we have we don't need to ask Signal about them
177
+ for ( int i = 0 ; i < intermediateContacts . Count ; i ++ )
178
+ {
179
+ var annotatedContact = await contactAnnotationList . FindAnnotationsByRemoteIdAsync ( intermediateContacts [ i ] . PhoneNumber ) ;
180
+ if ( annotatedContact . Count > 0 )
181
+ {
182
+ intermediateContacts [ i ] . OnSignal = true ;
183
+ signalContacts . Add ( intermediateContacts [ i ] ) ;
184
+ intermediateContacts . RemoveAt ( i ) ;
185
+ i -- ;
186
+ }
187
+ }
188
+
104
189
var signalContactDetails = accountManager . getContacts ( intermediateContacts . Select ( c => c . PhoneNumber ) . ToList ( ) ) ;
105
190
foreach ( var contact in intermediateContacts )
106
191
{
107
192
var foundContact = signalContactDetails . FirstOrDefault ( c => c . getNumber ( ) == contact . PhoneNumber ) ;
108
193
if ( foundContact != null )
109
194
{
110
195
contact . OnSignal = true ;
196
+ ContactAnnotation contactAnnotation = new ContactAnnotation ( ) ;
197
+ contactAnnotation . ContactId = contact . Id ;
198
+ contactAnnotation . RemoteId = contact . PhoneNumber ;
199
+ contactAnnotation . SupportedOperations = ContactAnnotationOperations . Message | ContactAnnotationOperations . ContactProfile ;
200
+ await contactAnnotationList . TrySaveAnnotationAsync ( contactAnnotation ) ;
111
201
signalContacts . Add ( contact ) ;
112
202
}
113
203
}
114
204
Contacts . AddRange ( signalContacts ) ;
115
205
}
116
206
else
117
207
{
118
- // something something we don't have contact access
119
- }
120
- }
121
-
122
- public MainPageViewModel MainPageVM ;
123
- public AddContactPage View ;
124
-
125
- private bool _UIEnabled = true ;
126
- public bool UIEnabled
127
- {
128
- get { return _UIEnabled ; }
129
- set { _UIEnabled = value ; RaisePropertyChanged ( nameof ( UIEnabled ) ) ; }
130
- }
131
-
132
- private bool _AddEnabled = false ;
133
- public bool AddEnabled
134
- {
135
- get { return _AddEnabled ; }
136
- set { _AddEnabled = value ; RaisePropertyChanged ( nameof ( AddEnabled ) ) ; }
137
- }
138
-
139
- private bool validName = false ;
140
- private bool ValidName
141
- {
142
- get { return validName ; }
143
- set
144
- {
145
- validName = value ;
146
- SetAddEnabled ( ) ;
147
- }
148
- }
149
-
150
- private bool validNumber = false ;
151
- private bool ValidNumber
152
- {
153
- get { return validNumber ; }
154
- set
155
- {
156
- validNumber = value ;
157
- SetAddEnabled ( ) ;
208
+ ContactsVisible = false ;
158
209
}
210
+ RefreshingContacts = false ;
159
211
}
160
212
161
213
private void SetAddEnabled ( )
0 commit comments