Skip to content

Commit

Permalink
New setting to configure the default country phone prefix, used to se…
Browse files Browse the repository at this point in the history
…arch (and find) contacts missing it
  • Loading branch information
nicorac committed Jan 20, 2024
1 parent b86ed89 commit c43828d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/app/pages/settings/settings.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ <h4>General</h4>
<ion-button slot="end" (click)="editDatetimeFormat()">Edit</ion-button>
</ion-item>

<ion-item>
<ion-label class="phone-prefix-label">
<h2>Default country prefix</h2>
<p class="ion-text-wrap">The country prefix (i.e. +1 for USA) to be used with numbers missing it</p>
</ion-label>
<ion-input
[ngModel]="settings.defaultCountryPrefix"
(ngModelChange)="setDefaultCountryPrefix($event)"
class="phone-prefix ion-text-right"
type="tel"
maxlength="7"
/>
</ion-item>

<ion-item-divider>
<h4>Recordings</h4>
</ion-item-divider>
Expand Down
7 changes: 7 additions & 0 deletions src/app/pages/settings/settings.page.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.phone-prefix-label {
max-width: none;
}

.phone-prefix {
min-width: 4em;
}
15 changes: 15 additions & 0 deletions src/app/pages/settings/settings.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,19 @@ export class SettingsPage {
this.editor.present();
}

/**
* Set the default country prefix to settings
*/
setDefaultCountryPrefix(prefix: string) {
prefix = prefix.trim();
if (prefix) {
if (!prefix.startsWith('+')) {
prefix = '+' + prefix;
}
if (/^\+\d*$/g.test(prefix)) {
this.settings.defaultCountryPrefix = prefix;
}
}
}

}
34 changes: 26 additions & 8 deletions src/app/services/contacts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { Injectable } from '@angular/core';
import { ContactPayload, Contacts, PhoneType } from '@capacitor-community/contacts';
import { PermissionState } from '@capacitor/core';
import { MessageBoxService } from './message-box.service';
import { SettingsService } from './settings.service';

@Injectable()
export class ContactsService {

constructor(
private mbs: MessageBoxService
private mbs: MessageBoxService,
private settings: SettingsService,
) {}

/**
Expand All @@ -20,7 +22,7 @@ export class ContactsService {
}

/**
* Cleanup the given phone number by keeping only "+" and digits
* Cleanup the given phone number by keeping only "+" and digits.
*/
cleanupPhoneNumber(phoneNumber: string): string {
return phoneNumber.replace(/[^\d\+]/g, '');
Expand All @@ -42,13 +44,29 @@ export class ContactsService {
}
});

// find and return the first match
return res.contacts.find(item =>
item.phones?.filter(i => i.number)
.map(i => this.cleanupPhoneNumber(i.number!))
.includes(phoneNumber)
);
// find and return the first contact match
for (const contact of res.contacts) {

// get defined and cleaned contact numbers
const numbers = contact.phones?.filter(n => n).map(p => this.cleanupPhoneNumber(p!.number!));
if (numbers?.length) {

// search first the plain number...
for (const n of numbers) {
if (n === phoneNumber) return contact;
}

// ...then tries adding the default country prefix
// (special numbers could not have intl prefix)
if (this.settings.defaultCountryPrefix) {
for (const n of numbers) {
if (n[0] !== '+' && (this.settings.defaultCountryPrefix + n === phoneNumber)) return contact;
}
}
}
}

return undefined;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/app/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export class SettingsService {
@JsonProperty()
public seekTime: number = 10;

/**
* Default country phone prefix
*/
@JsonProperty()
public defaultCountryPrefix: string = '';

/**
* Custom filename format
*/
Expand Down

0 comments on commit c43828d

Please sign in to comment.