That fuck shit the fascists are using
1package org.tm.archive.contacts;
2
3import android.content.Context;
4
5import androidx.annotation.NonNull;
6import androidx.annotation.Nullable;
7
8import org.tm.archive.contacts.paged.ContactSearchConfiguration;
9import org.tm.archive.contacts.paged.ContactSearchKey;
10import org.tm.archive.recipients.Recipient;
11import org.tm.archive.recipients.RecipientId;
12
13/**
14 * Model for a contact and the various ways it could be represented. Used in situations where we
15 * don't want to create Recipients for the wrapped data (like a custom-entered phone number for
16 * someone you don't yet have a conversation with).
17 */
18public final class SelectedContact {
19 private final RecipientId recipientId;
20 private final String number;
21 private final String username;
22
23 public static @NonNull SelectedContact forPhone(@Nullable RecipientId recipientId, @NonNull String number) {
24 return new SelectedContact(recipientId, number, null);
25 }
26
27 public static @NonNull SelectedContact forUsername(@Nullable RecipientId recipientId, @NonNull String username) {
28 return new SelectedContact(recipientId, null, username);
29 }
30
31 public static @NonNull SelectedContact forRecipientId(@NonNull RecipientId recipientId) {
32 return new SelectedContact(recipientId, null, null);
33 }
34
35 private SelectedContact(@Nullable RecipientId recipientId, @Nullable String number, @Nullable String username) {
36 this.recipientId = recipientId;
37 this.number = number;
38 this.username = username;
39 }
40
41 public @NonNull RecipientId getOrCreateRecipientId(@NonNull Context context) {
42 if (recipientId != null) {
43 return recipientId;
44 } else if (number != null) {
45 return Recipient.external(context, number).getId();
46 } else {
47 throw new AssertionError();
48 }
49 }
50
51 public @Nullable RecipientId getRecipientId() {
52 return recipientId;
53 }
54
55 public @Nullable String getNumber() {
56 return number;
57 }
58
59 public boolean hasUsername() {
60 return username != null;
61 }
62
63 public @NonNull ContactSearchKey toContactSearchKey() {
64 if (recipientId != null) {
65 return new ContactSearchKey.RecipientSearchKey(recipientId, false);
66 } else if (number != null) {
67 return new ContactSearchKey.UnknownRecipientKey(ContactSearchConfiguration.SectionKey.PHONE_NUMBER, number);
68 } else if (username != null) {
69 return new ContactSearchKey.UnknownRecipientKey(ContactSearchConfiguration.SectionKey.USERNAME, username);
70 } else {
71 throw new IllegalStateException("Nothing to map!");
72 }
73 }
74
75 /**
76 * Returns true when non-null recipient ids match, and false if not.
77 * <p>
78 * If one or more recipient id is not set, then it returns true iff any other non-null property
79 * matches one on the other contact.
80 */
81 public boolean matches(@Nullable SelectedContact other) {
82 if (other == null) return false;
83
84 if (recipientId != null && other.recipientId != null) {
85 return recipientId.equals(other.recipientId);
86 }
87
88 return number != null && number .equals(other.number) ||
89 username != null && username.equals(other.username);
90 }
91}