use of cl.smartcities.isci.transportinspector.adapters.dialogAdapters.BusSelectionAdapter in project androidApp by InspectorIncognito.
the class BusSelectionDialog method onCreateDialog.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = this.getActivity().getLayoutInflater();
final View dialog_view = inflater.inflate(R.layout.bus_selection_dialog, null);
final Typeface iconTypeface = Typeface.createFromAsset(this.getContext().getAssets(), getActivity().getString(R.string.icon_font));
((TextView) dialog_view.findViewById(R.id.suggestion_icon)).setTypeface(iconTypeface);
builder.setView(dialog_view);
builder.setCancelable(false);
dialog = builder.create();
final Bundle bundle = getArguments();
ArrayList<ServerSentBus> buses = null;
if (bundle != null) {
buses = bundle.getParcelableArrayList(BUS);
}
if (buses != null && !buses.isEmpty()) {
dialog_view.findViewById(R.id.list_view).setVisibility(View.VISIBLE);
setBusMap(buses);
ArrayList<String> serviceList = new ArrayList<>();
serviceList.addAll(this.busMap.keySet());
BusSelectionAdapter adapter = new BusSelectionAdapter(this.getContext(), serviceList, busMap, new BusSelectionAdapter.ListViewAdapterListener() {
@Override
public void onPositiveClick(ServerSentBus bus) {
dialog.cancel();
listener.onPositiveClick(bus);
}
@Override
public void onNegativeClick() {
dialog.cancel();
listener.onNegativeClick();
}
});
ListView listView = dialog_view.findViewById(R.id.list_view);
listView.setAdapter(adapter);
if (adapter.getCount() > VISIBLE_BUSES) {
View item = adapter.getView(0, null, listView);
item.measure(0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) ((VISIBLE_BUSES + 0.5) * item.getMeasuredHeight()));
listView.setLayoutParams(params);
}
}
Button otherAcceptButton = dialog_view.findViewById(R.id.send_button);
otherAcceptButton.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.button_color_off));
otherAcceptButton.setTextColor(ContextCompat.getColor(getContext(), R.color.odd_row));
final EditText serviceEditText = dialog_view.findViewById(R.id.service_edit_text);
final EditText licensePlateEditText = dialog_view.findViewById(R.id.license_plate_edit_text);
final TextListener editListener = new TextListener(otherAcceptButton, getContext());
serviceEditText.addTextChangedListener(new EditTextListener(editListener) {
@Override
public void check(String service) {
innerListener.setServiceReady(ServiceValidator.validate(service) && helper.getColorId(Util.formatServiceName(service)) != 0);
}
});
licensePlateEditText.addTextChangedListener(new EditTextListener(editListener) {
@Override
public void check(String licensePlate) {
innerListener.setLicensePlateReady(licensePlate.length() == 0 || LicensePlateValidator.validate(licensePlate));
}
});
otherAcceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (!editListener.isReady()) {
if (!editListener.serviceReady) {
Toast.makeText(getContext(), R.string.bus_selection_warning_service, Toast.LENGTH_SHORT).show();
} else if (!editListener.licensePlateReady) {
Toast.makeText(getContext(), R.string.bus_selection_warning_plate, Toast.LENGTH_SHORT).show();
}
} else {
String service = Util.formatServiceName(serviceEditText.getText().toString());
String licensePlate = licensePlateEditText.getText().toString();
licensePlate = licensePlate.toUpperCase().replaceAll("[ -]", "");
licensePlate = licensePlate.length() == 0 ? Constants.DUMMY_LICENSE_PLATE : licensePlate.toUpperCase();
ServerSentBus.createBus(licensePlate, Util.formatServiceName(service), new ServerSentBus.ConstructorListener() {
@Override
public void busConstructed(final ServerSentBus bus) {
try {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
} catch (NullPointerException ex) {
TranSappApplication.addLog("Bus Selection Dialog " + "InputMethodManager is null");
} finally {
cancelWithDelay(dialog, bus);
}
}
});
}
}
});
/* set cancel button to close dialog */
dialog_view.findViewById(R.id.close_dialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
listener.onNegativeClick();
}
});
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
use of cl.smartcities.isci.transportinspector.adapters.dialogAdapters.BusSelectionAdapter in project androidApp by InspectorIncognito.
the class BusSelectionAdapterTest method setUp.
@Before
public void setUp() throws Exception {
listener = mock(BusSelectionAdapter.ListViewAdapterListener.class);
HashMap<String, ArrayList<Bus>> busHashMap = new HashMap<>();
ArrayList<Bus> array509 = new ArrayList<>();
array509.add(new Bus("509", "GGWP10", ""));
array509.add(new Bus("509", "GGWP11", ""));
busHashMap.put("509", array509);
ArrayList<Bus> array506 = new ArrayList<>();
array506.add(new Bus("506", "GGWP20", ""));
array506.add(new Bus("506", "GGWP21", ""));
busHashMap.put("506", array506);
ArrayList<Bus> array507 = new ArrayList<>();
b507first = new Bus("507", "GGWP00", "");
b507second = new Bus("507", "GGWP01", "");
array507.add(b507first);
array507.add(b507second);
busHashMap.put("507", array507);
ArrayList<Bus> array506v = new ArrayList<>();
b506first = new Bus("506v", "GGWP03", "");
array506v.add(b506first);
busHashMap.put("506v", array506v);
ArrayList<String> serviceList = new ArrayList<>();
serviceList.add("509");
serviceList.add("506");
serviceList.add("507");
serviceList.add("506v");
mAdapter = new BusSelectionAdapter(rule.getActivity(), serviceList, busHashMap, listener);
}
Aggregations