Skip to content

Commit

Permalink
Opening a contact
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Apr 8, 2021
1 parent 4ce2491 commit 2cefd5e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
16 changes: 2 additions & 14 deletions src/main/java/org/luwrain/app/contacts/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,11 @@ public App()
return mainLayout.getAreaLayout();
}

void ensureEverythingSaved()
{
/*
if (!base.hasCurrentContact())
return;
base.saveForm(valuesArea);
base.saveNotes(notesArea);
*/
}

/*
@Override public void closeApp()
{
ensureEverythingSaved();
luwrain.closeApp();
mainLayout.ensureEverythingSaved();
super.closeApp();
}
*/

@Override public MonoApp.Result onMonoAppSecondInstance(Application app)
{
Expand Down
65 changes: 29 additions & 36 deletions src/main/java/org/luwrain/app/contacts/MainLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ final class MainLayout extends LayoutBase implements ListArea.ClickHandler
private final EditArea notesArea;

private ContactsFolder openedFolder = null;
private Contact openedContact = null;
private final List items = new ArrayList();
private Contact currentContact = null;

MainLayout(App app)
{
Expand Down Expand Up @@ -119,33 +119,33 @@ void fillValuesArea(FormArea area)
{
NullCheck.notNull(area, "area");
area.clear();
if (currentContact == null)
if (openedContact == null)
return;
area.addEdit("name", "Имя:", currentContact.getTitle(), null, true);
area.addEdit("name", "Имя:", openedContact.getTitle(), null, true);
int counter = 1;
for(ContactValue v: currentContact.getValues())
for(ContactValue v: openedContact.getValues())
if (v.getType() == ContactValue.Type.MAIL)
area.addEdit("mail" + (counter++), "Электронная почта:", v.getValue(), v, true);
for(ContactValue v: currentContact.getValues())
for(ContactValue v: openedContact.getValues())
if (v.getType() == ContactValue.Type.PHONE)
area.addEdit("mobile" + (counter++), "Мобильный телефон:", v.getValue(), v, true);
for(ContactValue v: currentContact.getValues())
for(ContactValue v: openedContact.getValues())
if (v.getType() == ContactValue.Type.ADDRESS)
area.addEdit("address" + (counter++), "Адрес:", v.getValue(), v, true);
for(ContactValue v: currentContact.getValues())
for(ContactValue v: openedContact.getValues())
if (v.getType() == ContactValue.Type.BIRTHDAY)
area.addEdit("birthday" + (counter++), "Дата рождения:", v.getValue(), v, true);
for(ContactValue v: currentContact.getValues())
for(ContactValue v: openedContact.getValues())
if (v.getType() == ContactValue.Type.SKYPE)
area.addEdit("skype" + (counter++), "Skype:", v.getValue(), v, true);
}

//Returns true on success saving, shows all corresponding error message;
boolean saveForm(FormArea area)
{
if (currentContact == null)
if (openedContact == null)
return false;
final LinkedList<ContactValue> values = new LinkedList<ContactValue>();
final List<ContactValue> values = new ArrayList<ContactValue>();
for(int i = 0;i < area.getItemCount();++i)
{
final Object obj = area.getItemObj(i);
Expand All @@ -156,14 +156,14 @@ boolean saveForm(FormArea area)
if (!value.getValue().trim().isEmpty())
values.add(value);
}
currentContact.setValues(values.toArray(new ContactValue[values.size()]));
openedContact.setValues(values.toArray(new ContactValue[values.size()]));
return true;
}

//Returns true if new value is really added, shows all corresponding error messages;
boolean insertValue()
{
if (currentContact == null)
if (openedContact == null)
return false;
final String mailTitle = "Электронная почта";
final String mobileTitle = "Мобильный телефон";
Expand Down Expand Up @@ -193,26 +193,26 @@ boolean insertValue()
if (res == skypeTitle)
type = ContactValue.Type.SKYPE; else
return false;//Should never happen
final ContactValue[] oldValues = currentContact.getValues();
final ContactValue[] oldValues = openedContact.getValues();
final ContactValue[] newValues = new ContactValue[oldValues.length + 1];
for(int i = 0;i < oldValues.length;++i)
newValues[i] = oldValues[i];
newValues[newValues.length - 1] = new ContactValue(type, "", false);
currentContact.setValues(newValues);
openedContact.setValues(newValues);
return true;
}

boolean fillNotesArea(EditArea area)
{
String value;
value = currentContact.getNotes();
value = openedContact.getNotes();
area.setLines(value.split("\n", -1));
return true;
}

boolean saveNotes(EditArea area)
{
if (currentContact == null)
if (openedContact == null)
return true;
final StringBuilder b = new StringBuilder();
final int count = area.getLineCount();
Expand All @@ -222,7 +222,7 @@ boolean saveNotes(EditArea area)
for(int i = 1;i < count;++i)
b.append("\n" + area.getLine(i));
}
currentContact.setNotes(b.toString());
openedContact.setNotes(b.toString());
return true;
}

Expand Down Expand Up @@ -260,7 +260,7 @@ boolean deleteContact(Contact contact)
if (popup.wasCancelled() || !popup.result())
return false;
app.getStoring().getContacts().delete(contact);
currentContact = null;//FIXME:maybe only if currentContact == contact
openedContact = null;//FIXME:maybe only if currentContact == contact
return true;
}

Expand Down Expand Up @@ -311,18 +311,13 @@ boolean deleteFromTree(ListArea foldersArea, FormArea valuesArea, EditArea notes

@Override public boolean onListClick(ListArea area, int index, Object obj)
{
/*
NullCheck.notNull(app, "app");
NullCheck.notNull(valuesArea, "valuesArea");
NullCheck.notNull(notesArea, "notesArea");
if (obj == null || !(obj instanceof Contact))
return;
app.ensureEverythingSaved();
base.setCurrentContact((Contact)obj);
base.fillValuesArea(valuesArea);
base.fillNotesArea(notesArea);
luwrain.setActiveArea(valuesArea);
*/
return false;
ensureEverythingSaved();
this.openedContact = (Contact)obj;
fillValuesArea(valuesArea);
fillNotesArea(notesArea);
setActiveArea(valuesArea);
return true;
}

Expand Down Expand Up @@ -408,13 +403,11 @@ private boolean insertContact(ContactsFolder insertInto)
}
}

void setCurrentContact(Contact contact)
void ensureEverythingSaved()
{
NullCheck.notNull(contact, "contact");
currentContact = contact;
if (openedContact == null)
return;
saveForm(valuesArea);
saveNotes(notesArea);
}


}


0 comments on commit 2cefd5e

Please sign in to comment.