Skip to content

Commit

Permalink
Merge pull request #74 from huttneab/partner_id
Browse files Browse the repository at this point in the history
add partner Id endpoint and use it
  • Loading branch information
huttneab authored Aug 23, 2016
2 parents 704c524 + 2dcf398 commit eee2042
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.rockthevote.grommet.data.prefs.EventRegTotal;
import com.rockthevote.grommet.data.prefs.EventZip;
import com.rockthevote.grommet.data.prefs.PartnerId;
import com.rockthevote.grommet.data.prefs.PartnerName;
import com.squareup.moshi.Moshi;

import java.io.File;
Expand Down Expand Up @@ -86,6 +87,13 @@ Preference<String> providePartnerId(RxSharedPreferences prefs, Application app)
return prefs.getString(app.getResources().getString(R.string.pref_key_partner_id));
}

@Provides
@Singleton
@PartnerName
Preference<String> providePartnerName(RxSharedPreferences prefs, Application app) {
return prefs.getString(app.getResources().getString(R.string.pref_key_partner_name));
}

@Provides
@Singleton
@CanvasserName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@AutoValue
public abstract class PartnerNameResponse {
@Json(name = "is_valid")
abstract boolean isValid();
public abstract boolean isValid();

@Json(name = "partner_name")
abstract String partnerName();
public abstract String partnerName();

public static JsonAdapter<PartnerNameResponse> jsonAdapter(Moshi moshi) {
return new AutoValue_PartnerNameResponse.MoshiJsonAdapter(moshi);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.rockthevote.grommet.data.prefs;

import java.lang.annotation.Retention;

import javax.inject.Qualifier;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
public @interface PartnerName {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rockthevote.grommet.ui.registration;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
Expand All @@ -22,6 +24,7 @@
import com.rockthevote.grommet.data.db.model.RockyRequest;
import com.rockthevote.grommet.data.db.model.VoterId;
import com.rockthevote.grommet.data.prefs.CurrentRockyRequestId;
import com.rockthevote.grommet.data.prefs.PartnerName;
import com.rockthevote.grommet.ui.misc.BetterSpinner;
import com.rockthevote.grommet.ui.misc.EnumAdapter;
import com.rockthevote.grommet.ui.misc.ObservableValidator;
Expand All @@ -39,7 +42,6 @@
import rx.Observable;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
import rx.subscriptions.CompositeSubscription;

import static com.rockthevote.grommet.data.db.Db.DEBOUNCE;
Expand Down Expand Up @@ -91,6 +93,8 @@ public class AdditionalInfoFragment extends BaseRegistrationFragment {

@Inject @CurrentRockyRequestId Preference<Long> rockyRequestRowId;

@Inject @PartnerName Preference<String> partnerNamePref;

@Inject BriteDatabase db;

private ObservableValidator validator;
Expand Down Expand Up @@ -148,6 +152,12 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
phoneTypeSpinner.getEditText().setText(phoneTypeEnumAdapter.getItem(0).toString());
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
phoneOptIn.setText(getString(R.string.label_receive_text, partnerNamePref.get()));
}

@Override
public void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.rockthevote.grommet.R;
import com.rockthevote.grommet.ui.misc.BetterViewAnimator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class EditableActionView extends FrameLayout {
@BindView(R.id.eav_edit_button) ImageButton editButton;
@BindView(R.id.eav_cancel_button) Button cancelButton;
@BindView(R.id.eav_save_button) Button saveButton;
@BindView(R.id.eav_better_view_animator) BetterViewAnimator viewAnimator;

private EditableActionViewListener listener;

Expand All @@ -41,7 +38,7 @@ protected void onFinishInflate() {
super.onFinishInflate();
if (!isInEditMode()) {
ButterKnife.bind(this);
enableEditMode(false);
showEditButton();
}
}

Expand Down Expand Up @@ -70,12 +67,16 @@ public void onClickSave(View v) {
}
}

public void enableEditMode(boolean enable) {
public void showSaveCancel() {
viewAnimator.setDisplayedChildId(R.id.eav_edit_mode_layout);
}

editButton.setVisibility(enable ? GONE : VISIBLE);
saveButton.setVisibility(enable ? VISIBLE : GONE);
cancelButton.setVisibility(enable ? VISIBLE : GONE);
public void showSpinner() {
viewAnimator.setDisplayedChildId(R.id.eav_progress_bar);
}

public void showEditButton() {
viewAnimator.setDisplayedChildId(R.id.eav_edit_button);
}

public interface EditableActionViewListener {
Expand Down
110 changes: 80 additions & 30 deletions app/src/main/java/com/rockthevote/grommet/ui/views/EventDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,68 @@

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.f2prateek.rx.preferences.Preference;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.rockthevote.grommet.R;
import com.rockthevote.grommet.data.Injector;
import com.rockthevote.grommet.data.api.RockyService;
import com.rockthevote.grommet.data.api.model.PartnerNameResponse;
import com.rockthevote.grommet.data.prefs.CanvasserName;
import com.rockthevote.grommet.data.prefs.EventName;
import com.rockthevote.grommet.data.prefs.EventRegTotal;
import com.rockthevote.grommet.data.prefs.EventZip;
import com.rockthevote.grommet.data.prefs.PartnerId;
import com.rockthevote.grommet.data.prefs.PartnerName;
import com.rockthevote.grommet.ui.misc.BetterViewAnimator;
import com.rockthevote.grommet.util.Strings;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import rx.Observable;
import rx.subjects.PublishSubject;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class EventDetails extends FrameLayout {
private static final String SAVE = "save";

static final ButterKnife.Setter<View, Boolean> ENABLED =
(view, value, index) -> view.setEnabled(value);

@BindViews({R.id.ede_til_canvasser_name, R.id.ede_til_event_name,
R.id.ede_til_event_zip, R.id.ede_til_partner_id})
List<TextInputLayout> editableViews;

@BindView(R.id.ed_animator) BetterViewAnimator viewAnimator;

@BindView(R.id.ed_canvasser_name) TextView edCanvasserName;

@BindView(R.id.ed_event_name) TextView edEventName;

@BindView(R.id.ed_event_zip) TextView edEventZip;

@BindView(R.id.ed_partner_id) TextView edPartnerId;
@BindView(R.id.ed_partner_name) TextView edPartnerName;

@BindView(R.id.ede_canvasser_name) TextView edeCanvasserName;
@BindView(R.id.ede_canvasser_name) EditText edeCanvasserName;

@BindView(R.id.ede_event_name) TextView edeEventName;
@BindView(R.id.ede_event_name) EditText edeEventName;

@BindView(R.id.ede_event_zip) TextView edeEventZip;
@BindView(R.id.ede_event_zip) EditText edeEventZip;

@BindView(R.id.ede_partner_id) TextView edePartnerId;
@BindView(R.id.ede_til_partner_id) TextInputLayout edePartnerIdTIL;

@BindView(R.id.ed_animator) BetterViewAnimator viewAnimator;
@BindView(R.id.ede_partner_id) EditText edePartnerId;

@Inject @EventRegTotal Preference<Integer> eventRegTotalPref;

Expand All @@ -59,7 +76,9 @@ public class EventDetails extends FrameLayout {

@Inject @PartnerId Preference<String> partnerIdPref;

private PublishSubject<String> publishSubject = PublishSubject.create();
@Inject @PartnerName Preference<String> partnerNamePref;

@Inject RockyService rockyService;

private CompositeSubscription subscriptions = new CompositeSubscription();

Expand Down Expand Up @@ -104,18 +123,8 @@ protected void onAttachedToWindow() {
subscriptions.add(eventZipPref.asObservable()
.subscribe(name -> edEventZip.setText(name)));

subscriptions.add(partnerIdPref.asObservable()
.subscribe(name -> edPartnerId.setText(name)));


//reset event registration total when event name changes
subscriptions.add(Observable.combineLatest(
RxTextView.textChanges(edeEventName), publishSubject,
(eventName, ignore) -> "")
.subscribe(s -> {
eventRegTotalPref.set(0);
})
);
subscriptions.add(partnerNamePref.asObservable()
.subscribe(name -> edPartnerName.setText(name)));
}
}

Expand All @@ -134,19 +143,58 @@ public void onCancel() {

@Override
public void onSave() {
canvasserNamePref.set(edeCanvasserName.getText().toString());
eventNamePref.set(edeEventName.getText().toString());
eventZipPref.set(edeEventZip.getText().toString());
partnerIdPref.set(edePartnerId.getText().toString());

publishSubject.onNext(SAVE);
enableEditMode(false);
// allow the user to not set a partner ID
if (Strings.isBlank(edePartnerId.getText().toString())) {
setPartnerName("");
} else if (edePartnerId.getText().toString().equals(partnerIdPref.get())) {
setPartnerName(partnerNamePref.get());
} else {
rockyService.getPartnerName(edePartnerId.getText().toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(() -> {
editableActionView.showSpinner();
ButterKnife.apply(editableViews, ENABLED, false);
})
.doOnCompleted(() -> ButterKnife.apply(editableViews, ENABLED, true))
.subscribe(result -> {
if (!result.isError() && result.response().isSuccessful()) {
PartnerNameResponse partnerNameResponse = result.response().body();
if (partnerNameResponse.isValid()) {
setPartnerName(partnerNameResponse.partnerName());
} else {
edePartnerIdTIL.setError(
getContext().getString(R.string.error_partner_id));
editableActionView.showSaveCancel();
}
} else {
edePartnerIdTIL.setError(
getContext().getString(R.string.error_partner_id));
editableActionView.showSaveCancel();
}
});
}
}
};

editableActionView.setListener(listener);
}

private void setPartnerName(String name) {
canvasserNamePref.set(edeCanvasserName.getText().toString());
eventNamePref.set(edeEventName.getText().toString());
eventZipPref.set(edeEventZip.getText().toString());
partnerIdPref.set(edePartnerId.getText().toString());
partnerNamePref.set(name);

// reset event registration total when event name changes
if (!edeEventName.getText().toString().equals(eventNamePref.get())) {
eventRegTotalPref.set(0);
}
enableEditMode(false);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Expand All @@ -156,15 +204,17 @@ protected void onDetachedFromWindow() {
}

public void enableEditMode(boolean enable) {
editableActionView.enableEditMode(enable);

viewAnimator.setDisplayedChildId(enable ? R.id.editable_details : R.id.static_details);

if (enable) {
editableActionView.showSaveCancel();
edeCanvasserName.setText(canvasserNamePref.get());
edeEventName.setText(eventNamePref.get());
edeEventZip.setText(eventZipPref.get());
edePartnerId.setText(partnerIdPref.get());
} else {
editableActionView.showEditButton();
// close keyboard if it's showing
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Activity.INPUT_METHOD_SERVICE);
Expand Down
Loading

0 comments on commit eee2042

Please sign in to comment.