Online Routing UI - Implement Test button

This commit is contained in:
nazar-kutz 2020-12-30 10:09:17 +02:00
parent 1649c066ff
commit fb5da3f51b
4 changed files with 120 additions and 11 deletions

View file

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:minHeight="@dimen/setting_list_item_group_height"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding"
android:paddingStart="@dimen/content_padding">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/icon"
android:layout_width="@dimen/standard_icon_size"
android:layout_height="@dimen/standard_icon_size"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding"
tools:src="@drawable/list_destination"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginLeft="@dimen/content_padding"
android:layout_marginStart="@dimen/content_padding"
android:orientation="vertical">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_list_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Some title" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_desc_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Some description" />
</LinearLayout>
</LinearLayout>

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="message_error_recheck_parameters">Error, recheck parameters</string>
<string name="routing_engine_vehicle_type_car">Car</string>
<string name="routing_engine_vehicle_type_bike">Bike</string>
<string name="routing_engine_vehicle_type_foot">Foot</string>

View file

@ -39,7 +39,6 @@ public class OnlineRoutingCard extends BaseCard {
private TextView tvHelperText;
private View bottomDivider;
private View button;
private View resultContainer;
private OnTextChangedListener onTextChangedListener;
public OnlineRoutingCard(@NonNull MapActivity mapActivity, boolean nightMode) {
@ -65,7 +64,6 @@ public class OnlineRoutingCard extends BaseCard {
tvHelperText = view.findViewById(R.id.helper_text);
bottomDivider = view.findViewById(R.id.bottom_divider);
button = view.findViewById(R.id.button);
resultContainer = view.findViewById(R.id.result_container);
editText.addTextChangedListener(new TextWatcher() {
@Override
@ -138,15 +136,18 @@ public class OnlineRoutingCard extends BaseCard {
editText.setTag(null);
}
public String getEditedText() {
return editText.getText().toString();
}
public void showDivider() {
showElements(bottomDivider);
}
public void setButton(OnClickListener listener) {
public void setButton(String title, OnClickListener listener) {
showElements(button);
button.setOnClickListener(listener);
UiUtilities.setupDialogButton(nightMode, button,
DialogButtonType.PRIMARY, R.string.test_route_calculation);
UiUtilities.setupDialogButton(nightMode, button, DialogButtonType.PRIMARY, title);
}
public void show() {

View file

@ -16,6 +16,8 @@ import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidNetworkUtils.OnRequestResultListener;
import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject;
import net.osmand.data.LatLon;
@ -31,6 +33,9 @@ import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.util.Algorithms;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
@ -55,6 +60,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
private OnlineRoutingCard vehicleCard;
private OnlineRoutingCard apiKeyCard;
private OnlineRoutingCard exampleCard;
private View testResultsContainer;
private boolean isEditingMode;
private ApplicationMode appMode;
@ -125,10 +131,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
return null;
}
boolean nightMode = isNightMode();
View view = UiUtilities.getInflater(getContext(), nightMode)
.inflate(R.layout.online_routing_engine_fragment, container, false);
inflater = UiUtilities.getInflater(getContext(), nightMode);
View view = inflater.inflate(
R.layout.online_routing_engine_fragment, container, false);
if (Build.VERSION.SDK_INT >= 21) {
AndroidUtils.addStatusBarPadding21v(getContext(), view);
}
@ -256,14 +261,19 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
}
});
exampleCard.setFieldBoxHelperText(getString(R.string.online_routing_example_hint));
exampleCard.setButton(new View.OnClickListener() {
exampleCard.setButton(getString(R.string.test_route_calculation), new View.OnClickListener() {
@Override
public void onClick(View v) {
// make request to the server
testEngineWork();
}
});
segmentsContainer.addView(exampleCard.getView());
testResultsContainer = inflater.inflate(
R.layout.bottom_sheet_item_with_descr_64dp, segmentsContainer, false);
testResultsContainer.setVisibility(View.GONE);
segmentsContainer.addView(testResultsContainer);
View bottomSpaceView = new View(app);
int space = (int) getResources().getDimension(R.dimen.empty_state_text_button_padding_top);
bottomSpaceView.setLayoutParams(
@ -383,6 +393,47 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
}
}
private void testEngineWork() {
final ServerType server = engine.serverType;
final ExampleLocation location = selectedLocation;
AndroidNetworkUtils.sendRequestAsync(app, exampleCard.getEditedText(), null,
null, false, false, new OnRequestResultListener() {
@Override
public void onResult(String response) {
boolean resultOk = false;
if (response != null) {
try {
JSONObject obj = new JSONObject(response);
if (server == ServerType.GRAPHHOPER) {
resultOk = obj.has("paths");
} else if (server == ServerType.OSRM) {
resultOk = obj.has("routes");
}
} catch (JSONException e) {
}
}
showTestResults(resultOk, location);
}
});
}
private void showTestResults(boolean resultOk, ExampleLocation location) {
testResultsContainer.setVisibility(View.VISIBLE);
ImageView ivImage = testResultsContainer.findViewById(R.id.icon);
TextView tvTitle = testResultsContainer.findViewById(R.id.title);
TextView tvDescription = testResultsContainer.findViewById(R.id.description);
if (resultOk) {
ivImage.setImageDrawable(getContentIcon(R.drawable.ic_action_gdirections_dark));
tvTitle.setText(getString(R.string.shared_string_ok));
} else {
ivImage.setImageDrawable(getContentIcon(R.drawable.ic_action_alert));
tvTitle.setText(getString(R.string.message_error_recheck_parameters));
}
tvDescription.setText(location.getName());
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);