mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-04 10:09:42 +00:00
refactor QR code dialog
This commit is contained in:
1 parent
8d9f7dee90
commit
3b3af908a5
4 files changed
+128
-69
No files matched your search
+1
-1
@@ -8,7 +8,7 @@ import com.sap.sse.common.Util;
|
||||
* This class is used by our backend, in GWT-client code and by the Android app. Therefore we cannot use classes like
|
||||
* {@link URLEncoder} to help us with the encoding.
|
||||
*/
|
||||
public class QRCodeUtils {
|
||||
public class DeviceConfigurationQRCodeUtils {
|
||||
|
||||
private static final String fragmentKey = "identifier";
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.sap.sailing.gwt.ui.adminconsole;
|
||||
|
||||
import com.google.gwt.event.dom.client.KeyUpEvent;
|
||||
import com.google.gwt.event.dom.client.KeyUpHandler;
|
||||
import com.google.gwt.event.logical.shared.ValueChangeEvent;
|
||||
import com.google.gwt.event.logical.shared.ValueChangeHandler;
|
||||
import com.google.gwt.user.client.Window;
|
||||
import com.google.gwt.user.client.ui.Grid;
|
||||
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
|
||||
import com.google.gwt.user.client.ui.IsWidget;
|
||||
import com.google.gwt.user.client.ui.Label;
|
||||
import com.google.gwt.user.client.ui.TextBox;
|
||||
import com.google.gwt.user.client.ui.VerticalPanel;
|
||||
import com.google.gwt.user.client.ui.Widget;
|
||||
import com.sap.sailing.gwt.ui.client.StringMessages;
|
||||
import com.sap.sailing.gwt.ui.client.shared.components.QRCodeComposite;
|
||||
|
||||
public abstract class BaseQRIdentifierWidget implements IsWidget {
|
||||
|
||||
protected final TextBox serverBox;
|
||||
protected final Grid inputGrid;
|
||||
private final QRCodeComposite qrCodeComposite;
|
||||
private final Widget baseWidget;
|
||||
private final Label error;
|
||||
|
||||
public BaseQRIdentifierWidget(int qrCodeSize, StringMessages stringMessages) {
|
||||
this(qrCodeSize, stringMessages, QRCodeComposite.ERROR_CORRECTION_LEVEL_H);
|
||||
}
|
||||
|
||||
public BaseQRIdentifierWidget(int qrCodeSize, StringMessages stringMessages, int errorCorrectionLevel) {
|
||||
serverBox = new TextBox();
|
||||
serverBox.setVisibleLength(40);
|
||||
serverBox.setValue(Window.Location.getProtocol() + "//" + Window.Location.getHost());
|
||||
serverBox.addKeyUpHandler(new KeyUpHandler() {
|
||||
@Override
|
||||
public void onKeyUp(KeyUpEvent event) {
|
||||
generateQRCode();
|
||||
}
|
||||
});
|
||||
serverBox.addValueChangeHandler(new ValueChangeHandler<String>() {
|
||||
@Override
|
||||
public void onValueChange(ValueChangeEvent<String> event) {
|
||||
generateQRCode();
|
||||
}
|
||||
});
|
||||
|
||||
inputGrid = new Grid(1, 2);
|
||||
inputGrid.setWidget(0, 0, new Label("Server URL:"));
|
||||
inputGrid.setWidget(0, 1, serverBox);
|
||||
|
||||
qrCodeComposite = new QRCodeComposite(qrCodeSize, errorCorrectionLevel);
|
||||
|
||||
error = new Label();
|
||||
error.setStyleName("errorLabel");
|
||||
|
||||
VerticalPanel panel = new VerticalPanel();
|
||||
panel.add(inputGrid);
|
||||
panel.add(qrCodeComposite);
|
||||
panel.setCellHorizontalAlignment(qrCodeComposite, HasHorizontalAlignment.ALIGN_CENTER);
|
||||
panel.add(error);
|
||||
|
||||
baseWidget = panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Widget asWidget() {
|
||||
return baseWidget;
|
||||
}
|
||||
|
||||
protected abstract String generateEncodedQRCodeContent();
|
||||
|
||||
public void generateQRCode() {
|
||||
String encoded = generateEncodedQRCodeContent();
|
||||
if (encoded != null) {
|
||||
error.setText("");
|
||||
qrCodeComposite.generateQRCode(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
protected String getServerUrlWithoutFinalSlash() {
|
||||
String serverUrl = serverBox.getValue();
|
||||
if (serverUrl.endsWith("/")) {
|
||||
return serverUrl.substring(0, serverUrl.length() - 1);
|
||||
}
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
protected void setError(String text) {
|
||||
error.setText(text);
|
||||
}
|
||||
}
|
||||
+34
-66
@@ -2,72 +2,52 @@ package com.sap.sailing.gwt.ui.adminconsole;
|
||||
|
||||
import com.google.gwt.event.dom.client.ClickEvent;
|
||||
import com.google.gwt.event.dom.client.ClickHandler;
|
||||
import com.google.gwt.event.dom.client.KeyUpEvent;
|
||||
import com.google.gwt.event.dom.client.KeyUpHandler;
|
||||
import com.google.gwt.event.logical.shared.ValueChangeEvent;
|
||||
import com.google.gwt.event.logical.shared.ValueChangeHandler;
|
||||
import com.google.gwt.user.client.Window;
|
||||
import com.google.gwt.user.client.ui.Button;
|
||||
import com.google.gwt.user.client.ui.DialogBox;
|
||||
import com.google.gwt.user.client.ui.Grid;
|
||||
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
|
||||
import com.google.gwt.user.client.ui.HorizontalPanel;
|
||||
import com.google.gwt.user.client.ui.Label;
|
||||
import com.google.gwt.user.client.ui.TextBox;
|
||||
import com.google.gwt.user.client.ui.VerticalPanel;
|
||||
import com.sap.sailing.domain.common.impl.QRCodeUtils;
|
||||
import com.sap.sailing.domain.common.impl.DeviceConfigurationQRCodeUtils;
|
||||
import com.sap.sailing.gwt.ui.client.StringMessages;
|
||||
import com.sap.sailing.gwt.ui.client.shared.components.QRCodeComposite;
|
||||
|
||||
public class DeviceConfigurationQRIdentifierDialog extends DialogBox {
|
||||
|
||||
public static final String rcAppApkPath = "/apps/com.sap.sailing.racecommittee.app.apk";
|
||||
|
||||
private static final int qrCodeSize = 320;
|
||||
|
||||
private final TextBox identifierBox;
|
||||
private final TextBox serverBox;
|
||||
private final QRCodeComposite qrCodeComposite;
|
||||
private static final int qrCodeSize = 320;
|
||||
|
||||
protected static String getApkPath(String serverUrl) {
|
||||
if (serverUrl.endsWith("/")) {
|
||||
return serverUrl.substring(0, serverUrl.length() - 1) + rcAppApkPath;
|
||||
private class DeviceConfigurationQRIdentifierWidget extends BaseQRIdentifierWidget {
|
||||
private final TextBox identifierBox;
|
||||
public DeviceConfigurationQRIdentifierWidget(String identifier, StringMessages stringMessages) {
|
||||
super(qrCodeSize, stringMessages);
|
||||
|
||||
identifierBox = new TextBox();
|
||||
identifierBox.setValue(identifier);
|
||||
identifierBox.setReadOnly(true);
|
||||
identifierBox.setVisibleLength(40);
|
||||
|
||||
|
||||
inputGrid.resize(2, 2);
|
||||
inputGrid.setWidget(1, 0, new Label("Identifier:"));
|
||||
inputGrid.setWidget(1, 1, identifierBox);
|
||||
}
|
||||
return serverUrl + rcAppApkPath;
|
||||
|
||||
@Override
|
||||
protected String generateEncodedQRCodeContent() {
|
||||
if (identifierBox.getValue().contains("#")) {
|
||||
Window.alert("I'm not capable of generating a code for this identifier.");
|
||||
} else if (!identifierBox.getValue().isEmpty() && !serverBox.getValue().isEmpty()) {
|
||||
String apkUrl = getServerUrlWithoutFinalSlash() + rcAppApkPath;
|
||||
return DeviceConfigurationQRCodeUtils.composeQRContent(identifierBox.getValue(), apkUrl);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final DeviceConfigurationQRIdentifierWidget widget;
|
||||
|
||||
public DeviceConfigurationQRIdentifierDialog(String identifier, StringMessages stringMessages) {
|
||||
setText("Synchronize device with server");
|
||||
|
||||
identifierBox = new TextBox();
|
||||
identifierBox.setValue(identifier);
|
||||
identifierBox.setReadOnly(true);
|
||||
identifierBox.setVisibleLength(40);
|
||||
|
||||
serverBox = new TextBox();
|
||||
serverBox.setVisibleLength(40);
|
||||
serverBox.setValue(Window.Location.getProtocol() + "//" + Window.Location.getHost());
|
||||
serverBox.addKeyUpHandler(new KeyUpHandler() {
|
||||
@Override
|
||||
public void onKeyUp(KeyUpEvent event) {
|
||||
generateQRCode();
|
||||
}
|
||||
});
|
||||
serverBox.addValueChangeHandler(new ValueChangeHandler<String>() {
|
||||
@Override
|
||||
public void onValueChange(ValueChangeEvent<String> event) {
|
||||
generateQRCode();
|
||||
}
|
||||
});
|
||||
|
||||
Grid inputGrid = new Grid(2, 2);
|
||||
inputGrid.setWidget(0, 0, new Label("Identifier:"));
|
||||
inputGrid.setWidget(0, 1, identifierBox);
|
||||
inputGrid.setWidget(1, 0, new Label("Server URL:"));
|
||||
inputGrid.setWidget(1, 1, serverBox);
|
||||
|
||||
qrCodeComposite = new QRCodeComposite(qrCodeSize);
|
||||
|
||||
widget = new DeviceConfigurationQRIdentifierWidget(identifier, stringMessages);
|
||||
Button exitButton = new Button(stringMessages.close());
|
||||
exitButton.addClickHandler(new ClickHandler() {
|
||||
@Override
|
||||
@@ -80,9 +60,7 @@ public class DeviceConfigurationQRIdentifierDialog extends DialogBox {
|
||||
actionPanel.add(exitButton);
|
||||
|
||||
VerticalPanel panel = new VerticalPanel();
|
||||
panel.add(inputGrid);
|
||||
panel.add(qrCodeComposite);
|
||||
panel.setCellHorizontalAlignment(qrCodeComposite, HasHorizontalAlignment.ALIGN_CENTER);
|
||||
panel.add(widget);
|
||||
panel.add(actionPanel);
|
||||
setWidget(panel);
|
||||
}
|
||||
@@ -90,16 +68,6 @@ public class DeviceConfigurationQRIdentifierDialog extends DialogBox {
|
||||
@Override
|
||||
protected void onAttach() {
|
||||
super.onAttach();
|
||||
generateQRCode();
|
||||
}
|
||||
|
||||
protected void generateQRCode() {
|
||||
if (identifierBox.getValue().contains("#")) {
|
||||
Window.alert("I'm not capable of generating a code for this identifier.");
|
||||
} else if (!identifierBox.getValue().isEmpty() && !serverBox.getValue().isEmpty()) {
|
||||
String encoded = QRCodeUtils.composeQRContent(identifierBox.getValue(), getApkPath(serverBox.getValue()));
|
||||
qrCodeComposite.generateQRCode(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
widget.generateQRCode();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -18,7 +18,7 @@ import android.widget.Toast;
|
||||
|
||||
import com.sap.sailing.android.shared.ui.fragments.preference.BasePreferenceFragment;
|
||||
import com.sap.sailing.android.shared.ui.views.EditSetPreference;
|
||||
import com.sap.sailing.domain.common.impl.QRCodeUtils;
|
||||
import com.sap.sailing.domain.common.impl.DeviceConfigurationQRCodeUtils;
|
||||
import com.sap.sailing.racecommittee.app.AppPreferences;
|
||||
import com.sap.sailing.racecommittee.app.R;
|
||||
import com.sap.sailing.racecommittee.app.utils.autoupdate.AutoUpdater;
|
||||
@@ -160,7 +160,7 @@ public class GeneralPreferenceFragment extends BasePreferenceFragment {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
String content = data.getStringExtra("SCAN_RESULT");
|
||||
try {
|
||||
Util.Pair<String, String> connectionConfiguration = QRCodeUtils.splitQRContent(content);
|
||||
Util.Pair<String, String> connectionConfiguration = DeviceConfigurationQRCodeUtils.splitQRContent(content);
|
||||
|
||||
String identifier = connectionConfiguration.getA();
|
||||
URL apkUrl = tryConvertToURL(connectionConfiguration.getB());
|
||||
|
||||
Reference in new issue
Block a user