Permalink
Browse files

fixed duplicate contacts and added "delete" option

  • Loading branch information...
dakhnod committed Jul 8, 2018
1 parent 806862f commit fa6a20d0f7103fae23da9f6101cdf7a47e919c2e
@@ -40,6 +40,7 @@
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
@@ -264,8 +265,26 @@ private void refreshContactList() {
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
contactListView.setAdapter(new ContactListAdapter(ContactListActivity.this, R.layout.contact_item, mainBinder.getContacts()));
List<Contact> contacts = mainBinder.getContacts();
contactListView.setAdapter(new ContactListAdapter(ContactListActivity.this, R.layout.contact_item, contacts));
contactListView.setOnItemClickListener(ContactListActivity.this);
contactListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
PopupMenu menu = new PopupMenu(ContactListActivity.this, view);
menu.getMenu().add("delete");
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
mainBinder.deleteContact(contacts.get(i));
refreshContactList();
return false;
}
});
menu.show();
return true;
}
});
}
});
}
@@ -57,19 +57,19 @@ public void close(){
database.close();
}
public void insertContact(Contact c) {
public void insertContact(Contact c) throws ContactAlreadyAddedException{
ContentValues values = new ContentValues(3);
//values.put(columnID, c.getId());
values.put(columnIdentifier, c.getIdentifier());
values.put(columnIP, c.getAddress());
values.put(columnName, c.getName());
values.put(columnPhoto, c.getPhoto());
values.put(columnIdentifier, c.getIdentifier());
Cursor cur = database.query(tableName, new String[]{columnID}, columnIdentifier + "=\"" + c.getIdentifier() + "\"", null, "", "", "");
int length = cur.getCount();
cur.close();
if(length > 0){
return;
throw new ContactAlreadyAddedException();
}
@@ -113,4 +113,11 @@ public void onCreate(SQLiteDatabase sqLiteDatabase) {
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
class ContactAlreadyAddedException extends Exception{
@Override
public String getMessage() {
return "Contact already added";
}
}
}
@@ -171,8 +171,10 @@ private void handleClient(Socket client) {
"",
identifier
);
sqlHelper.insertContact(c);
contacts.add(c);
try {
sqlHelper.insertContact(c);
contacts.add(c);
}catch (ContactSqlHelper.ContactAlreadyAddedException e){}
JSONObject response = new JSONObject();
response.put("username", userName);
os.write((response.toString() + "\n").getBytes());
@@ -259,13 +261,23 @@ String generateChallenge() {
}
void addContact(Contact c, String challenge) {
sqlHelper.insertContact(c);
contacts.add(c);
log("adding contact " + c.getName() + " " + c.getId());
try {
sqlHelper.insertContact(c);
contacts.add(c);
log("adding contact " + c.getName() + " " + c.getId());
} catch (ContactSqlHelper.ContactAlreadyAddedException e) {
Toast.makeText(MainService.this, "Contact already added", 0).show();
}
new Thread(new ConnectRunnable(c, challenge)).start();
}
void deleteContact(Contact c){
sqlHelper.deleteContact(c);
refreshContacts();
}
void pingContacts(List<Contact> c, ContactPingListener listener) {
new Thread(new PingRunnable(c, listener)).start();
}

0 comments on commit fa6a20d

Please sign in to comment.