Add delete contact funcionalityg

master
esensar 2017-01-07 17:56:45 +01:00
parent 5e4e1692e6
commit a0cea8b8da
6 changed files with 44 additions and 8 deletions

View File

@ -60,7 +60,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -12,6 +12,7 @@ public class NetworkingConstants {
public static final String API_LOGOUT_ENDPOINT = "odjava"; public static final String API_LOGOUT_ENDPOINT = "odjava";
public static final String API_REGISTRATION_ENDPOINT = "dodaj"; public static final String API_REGISTRATION_ENDPOINT = "dodaj";
public static final String API_CONTACTS_ENDPOINT = "contacts"; public static final String API_CONTACTS_ENDPOINT = "contacts";
public static final String API_CONTACTS_DELETE_ENDPOINT = "contacts/{id}";
public static final String API_PROFILE_ENDPOINT = "profile"; public static final String API_PROFILE_ENDPOINT = "profile";
public static final String API_PASSWORD_UPDATE_ENDPOINT = API_PROFILE_ENDPOINT + "/password"; public static final String API_PASSWORD_UPDATE_ENDPOINT = API_PROFILE_ENDPOINT + "/password";
public static final String API_SEARCH_ENDPOINT = "search"; public static final String API_SEARCH_ENDPOINT = "search";

View File

@ -9,6 +9,8 @@ import retrofit2.http.Body;
import retrofit2.http.DELETE; import retrofit2.http.DELETE;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.POST; import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Url;
import rx.Observable; import rx.Observable;
/** /**
@ -26,7 +28,7 @@ public interface ContactsAPI {
@POST(NetworkingConstants.API_CONTACTS_ENDPOINT) @POST(NetworkingConstants.API_CONTACTS_ENDPOINT)
Observable<Object> saveContact(@Body ContactRequest contactRequest); Observable<Object> saveContact(@Body ContactRequest contactRequest);
@DELETE(NetworkingConstants.API_CONTACTS_ENDPOINT) @DELETE(NetworkingConstants.API_CONTACTS_DELETE_ENDPOINT)
Observable<Object> deleteContact(@Body ContactRequest contactRequest); Observable<Object> deleteContact(@Path("id") String id);
} }

View File

@ -44,7 +44,7 @@ public class ContactsAPIService {
} }
public Observable<Object> deleteContact(ContactRequest request) { public Observable<Object> deleteContact(ContactRequest request) {
return RestClient.getInstance().getContactsAPI().deleteContact(request) return RestClient.getInstance().getContactsAPI().deleteContact(request.getUsername())
.doOnError(handleApiErrors); .doOnError(handleApiErrors);
} }

View File

@ -8,6 +8,7 @@ import android.util.Pair;
import android.view.View; import android.view.View;
import com.smarthomies.realtimetalk.RTTActivity; import com.smarthomies.realtimetalk.RTTActivity;
import com.smarthomies.realtimetalk.database.UserDAO;
import com.smarthomies.realtimetalk.managers.AccountManager; import com.smarthomies.realtimetalk.managers.AccountManager;
import com.smarthomies.realtimetalk.managers.AuthenticationManager; import com.smarthomies.realtimetalk.managers.AuthenticationManager;
import com.smarthomies.realtimetalk.managers.ContactsManager; import com.smarthomies.realtimetalk.managers.ContactsManager;
@ -23,6 +24,7 @@ import com.smarthomies.realtimetalk.views.activities.SearchActivity;
import java.util.List; import java.util.List;
import io.realm.Realm;
import rx.Observable; import rx.Observable;
import rx.Subscriber; import rx.Subscriber;
import rx.Subscription; import rx.Subscription;
@ -140,6 +142,15 @@ public class MainViewModel extends BaseObservable {
} }
public void onLogoutDone() { public void onLogoutDone() {
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
new UserDAO().deleteAll(realm);
} finally {
if(realm != null) {
realm.close();
}
}
NavigationSubject.getInstance().onNext(new Pair<Class<? extends RTTActivity>, Bundle>(LoginActivity.class, null)); NavigationSubject.getInstance().onNext(new Pair<Class<? extends RTTActivity>, Bundle>(LoginActivity.class, null));
} }

View File

@ -11,6 +11,7 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import com.smarthomies.realtimetalk.R; import com.smarthomies.realtimetalk.R;
import com.smarthomies.realtimetalk.database.UserDAO;
import com.smarthomies.realtimetalk.managers.ContactsManager; import com.smarthomies.realtimetalk.managers.ContactsManager;
import com.smarthomies.realtimetalk.models.db.User; import com.smarthomies.realtimetalk.models.db.User;
import com.smarthomies.realtimetalk.views.activities.CallActivity; import com.smarthomies.realtimetalk.views.activities.CallActivity;
@ -18,6 +19,8 @@ import com.squareup.picasso.Picasso;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import io.realm.Realm;
import rx.Observable;
import rx.Subscriber; import rx.Subscriber;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
@ -37,7 +40,7 @@ public class UserViewModel extends BaseObservable {
} }
public UserViewModel(User model) { public UserViewModel(User model) {
this.model = model; setModel(model);
} }
public void setModel(User model) { public void setModel(User model) {
@ -45,6 +48,21 @@ public class UserViewModel extends BaseObservable {
if (model == null) { if (model == null) {
this.model = new User(); this.model = new User();
} }
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
if (new UserDAO().findUserById(realm, this.model.getId()) != null) {
state.set(true);
} else {
state.set(false);
}
} finally {
if(realm != null) {
realm.close();
}
}
notifyChange(); notifyChange();
} }
@ -71,8 +89,11 @@ public class UserViewModel extends BaseObservable {
return new View.OnClickListener() { return new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final Subscription subscription = new ContactsManager() Observable<Object> objectObservable = state.get()
.saveContact(model) ? new ContactsManager().deleteContact(model)
: new ContactsManager().saveContact(model);
final Subscription subscription = objectObservable
.delaySubscription(3000, TimeUnit.MILLISECONDS) .delaySubscription(3000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -93,7 +114,8 @@ public class UserViewModel extends BaseObservable {
}); });
Log.d(TAG, "onClick: "); Log.d(TAG, "onClick: ");
state.set(!state.get()); state.set(!state.get());
Snackbar.make(v, model.getFirstName() + " " + model.getLastName() + " added to conctacts.", Snackbar.LENGTH_LONG) String action = state.get() ? "added" : "removed";
Snackbar.make(v, model.getFirstName() + " " + model.getLastName() + " " + action + " to contacts.", Snackbar.LENGTH_LONG)
.setDuration(3000) .setDuration(3000)
.setAction("Undo", new View.OnClickListener() { .setAction("Undo", new View.OnClickListener() {
@Override @Override