Skip to content

Commit

Permalink
Merge pull request #2283 from shubhamkmr04/shubham/ContactStore
Browse files Browse the repository at this point in the history
ContactStore
  • Loading branch information
kaloudis authored Aug 28, 2024
2 parents 77874b9 + 9bad513 commit 4bc1e3b
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 207 deletions.
1 change: 1 addition & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export default class App extends React.PureComponent {
InventoryStore={Stores.inventoryStore}
ModalStore={Stores.modalStore}
NotesStore={Stores.notesStore}
ContactStore={Stores.contactStore}
SyncStore={Stores.syncStore}
LSPStore={Stores.lspStore}
LightningAddressStore={Stores.lightningAddressStore}
Expand Down
151 changes: 151 additions & 0 deletions stores/ContactStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { action, observable } from 'mobx';
import { v4 as uuidv4 } from 'uuid';
import Contact from '../models/Contact';
import EncryptedStorage from 'react-native-encrypted-storage';

export default class ContactStore {
@observable public loading: boolean = true;
@observable public contacts: any = [];
@observable public prefillContact: any = {};

@action
public loadContacts = async () => {
try {
this.loading = true;
console.log('LOADING CONTACTS.....');
const contactsString = await EncryptedStorage.getItem(
'zeus-contacts'
);
if (contactsString) {
const allContacts: Contact[] = JSON.parse(contactsString);
this.contacts = allContacts;
this.loading = false;
} else {
this.loading = false;
}
} catch (error) {
console.log('Error loading contacts:', error);
this.loading = false;
}
};

@action
public saveContact = async (
contactDetails: any,
isEdit: boolean,
isNostrContact: boolean,
navigation: any
) => {
try {
const contactsString = await EncryptedStorage.getItem(
'zeus-contacts'
);
const existingContacts: Contact[] = contactsString
? JSON.parse(contactsString)
: [];

if (isEdit && this.prefillContact && !isNostrContact) {
const updatedContacts = existingContacts.map((contact) =>
contact.contactId === this.prefillContact.contactId
? { ...contact, ...contactDetails }
: contact
);

// Sort the updated contacts alphabetically
updatedContacts.sort((a, b) => a.name.localeCompare(b.name));

// Save the updated contacts to encrypted storage
await EncryptedStorage.setItem(
'zeus-contacts',
JSON.stringify(updatedContacts)
);

console.log('Contact updated successfully!', updatedContacts);

this.loadContacts();
navigation.popTo('Contacts');
} else {
// Creating a new contact
const contactId = uuidv4();

const newContact: Contact = { contactId, ...contactDetails };

const updatedContacts = [...existingContacts, newContact].sort(
(a, b) => a.name.localeCompare(b.name)
);

// Save the updated contacts to encrypted storage
await EncryptedStorage.setItem(
'zeus-contacts',
JSON.stringify(updatedContacts)
);

console.log('Contact saved successfully!');

this.loadContacts();
navigation.popTo('Contacts');
}
// Clear the prefillContact after saving
this.clearPrefillContact();
} catch (error) {
console.log('Error saving contacts:', error);
}
};

@action
public deleteContact = async (navigation: any) => {
if (this.prefillContact) {
try {
const contactsString = await EncryptedStorage.getItem(
'zeus-contacts'
);
const existingContacts: Contact[] = contactsString
? JSON.parse(contactsString)
: [];

const updatedContacts = existingContacts.filter(
(contact) =>
contact.contactId !== this.prefillContact.contactId
);

await EncryptedStorage.setItem(
'zeus-contacts',
JSON.stringify(updatedContacts)
);

console.log('Contact deleted successfully!');

this.loadContacts();
navigation.popTo('Contacts');
} catch (error) {
console.log('Error deleting contact:', error);
}
}
};

@action
public setPrefillContact = (prefillContact: Contact | null) => {
if (prefillContact) {
this.prefillContact = {
lnAddress: prefillContact.lnAddress,
bolt12Address: prefillContact.bolt12Address,
onchainAddress: prefillContact.onchainAddress,
nip05: prefillContact.nip05,
nostrNpub: prefillContact.nostrNpub,
pubkey: prefillContact.pubkey,
name: prefillContact.name,
description: prefillContact.description,
photo: prefillContact.photo,
isFavourite: prefillContact.isFavourite,
contactId: prefillContact?.contactId
};
} else {
this.prefillContact = null;
}
};

@action
public clearPrefillContact = () => {
this.prefillContact = null;
};
}
3 changes: 3 additions & 0 deletions stores/Stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ActivityStore from './ActivityStore';
import PosStore from './PosStore';
import ModalStore from './ModalStore';
import NotesStore from './NotesStore';
import ContactStore from './ContactStore';
import SyncStore from './SyncStore';
import LSPStore from './LSPStore';
import LightningAddressStore from './LightningAddressStore';
Expand All @@ -42,6 +43,7 @@ class Stores {
public posStore: PosStore;
public modalStore: ModalStore;
public notesStore: NotesStore;
public contactStore: ContactStore;
public syncStore: SyncStore;
public lspStore: LSPStore;
public lightningAddressStore: LightningAddressStore;
Expand Down Expand Up @@ -101,6 +103,7 @@ class Stores {
this.utxosStore = new UTXOsStore(this.settingsStore);
this.messageSignStore = new MessageSignStore();
this.notesStore = new NotesStore();
this.contactStore = new ContactStore();
this.syncStore = new SyncStore(this.settingsStore);
this.activityStore = new ActivityStore(
this.settingsStore,
Expand Down
22 changes: 16 additions & 6 deletions views/ContactDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TouchableOpacity,
ScrollView
} from 'react-native';
import { inject, observer } from 'mobx-react';
import EncryptedStorage from 'react-native-encrypted-storage';
import { Route } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
Expand All @@ -17,6 +18,8 @@ import LoadingIndicator from '../components/LoadingIndicator';
import Header from '../components/Header';
import { Row } from '../components/layout/Row';

import ContactStore from '../stores/ContactStore';

import LightningBolt from '../assets/images/SVG/Lightning Bolt.svg';
import BitcoinIcon from '../assets/images/SVG/BitcoinIcon.svg';
import KeySecurity from '../assets/images/SVG/Key Security.svg';
Expand All @@ -41,13 +44,17 @@ interface ContactDetailsProps {
nostrContact: any;
}
>;
ContactStore: ContactStore;
}

interface ContactDetailsState {
contact: Contact | any;
isLoading: boolean;
isNostrContact: boolean;
}

@inject('ContactStore')
@observer
export default class ContactDetails extends React.Component<
ContactDetailsProps,
ContactDetailsState
Expand Down Expand Up @@ -136,6 +143,7 @@ export default class ContactDetails extends React.Component<
};

saveUpdatedContact = async (updatedContact: Contact) => {
const { ContactStore } = this.props;
try {
const contactsString = await EncryptedStorage.getItem(
'zeus-contacts'
Expand All @@ -160,6 +168,7 @@ export default class ContactDetails extends React.Component<
);

console.log('Contact updated successfully!');
ContactStore?.loadContacts();
}
}
} catch (error) {
Expand Down Expand Up @@ -212,7 +221,8 @@ export default class ContactDetails extends React.Component<

render() {
const { isLoading, isNostrContact } = this.state;
const { navigation } = this.props;
const { navigation, ContactStore } = this.props;
const { setPrefillContact } = ContactStore;

const contact = new Contact(this.state.contact);
const nostrContact = this.props.route.params?.nostrContact;
Expand All @@ -229,12 +239,12 @@ export default class ContactDetails extends React.Component<

const EditContactButton = () => (
<TouchableOpacity
onPress={() =>
onPress={() => {
setPrefillContact(contact);
navigation.navigate('AddContact', {
prefillContact: contact,
isEdit: true
})
}
});
}}
>
<EditContact
fill={themeColor('text')}
Expand Down Expand Up @@ -672,8 +682,8 @@ export default class ContactDetails extends React.Component<
<Button
onPress={() => {
navigation.goBack();
setPrefillContact(nostrContact);
navigation.navigate('AddContact', {
prefillContact: nostrContact,
isEdit: true,
isNostrContact
});
Expand Down
8 changes: 8 additions & 0 deletions views/NostrContacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Animated,
Easing
} from 'react-native';
import { inject, observer } from 'mobx-react';
import { CheckBox, Icon } from 'react-native-elements';
import EncryptedStorage from 'react-native-encrypted-storage';
import { relayInit, nip05, nip19 } from 'nostr-tools';
Expand All @@ -28,12 +29,14 @@ import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';

import { DEFAULT_NOSTR_RELAYS } from '../stores/SettingsStore';
import ContactStore from '../stores/ContactStore';

import SelectOff from '../assets/images/SVG/Select Off.svg';
import SelectOn from '../assets/images/SVG/Select On.svg';

interface NostrContactsProps {
navigation: StackNavigationProp<any, any>;
ContactStore: ContactStore;
}

interface NostrContactsState {
Expand All @@ -49,6 +52,8 @@ interface NostrContactsState {
error: string;
}

@inject('ContactStore')
@observer
export default class NostrContacts extends React.Component<
NostrContactsProps,
NostrContactsState
Expand Down Expand Up @@ -367,6 +372,7 @@ export default class NostrContacts extends React.Component<
};

importContacts = async () => {
const { ContactStore } = this.props;
this.setState({
loading: true
});
Expand Down Expand Up @@ -420,6 +426,8 @@ export default class NostrContacts extends React.Component<
);

console.log('Contacts imported successfully!');

ContactStore?.loadContacts();
this.setState({
loading: false
});
Expand Down
Loading

0 comments on commit 4bc1e3b

Please sign in to comment.