Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bookmarks system #92

Merged
merged 9 commits into from
Jun 23, 2023
Merged
12 changes: 9 additions & 3 deletions src/mahomaps/overlays/SearchOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mahomaps.MahoMapsApp;
import mahomaps.map.Geopoint;
import mahomaps.map.MapState;
import mahomaps.screens.BookmarksScreen;
import mahomaps.screens.SearchResultScreen;
import mahomaps.screens.SearchScreen;
import mahomaps.ui.Button;
Expand Down Expand Up @@ -71,11 +72,12 @@ private void SetSelection(Geopoint p) {

content = new FillFlowContainer(new UIElement[] { new SimpleText(data.getNullableString("name")),
new SimpleText(data.getString("description", "")),
new ColumnsContainer(
new UIElement[] { new Button(MahoMapsApp.text[109], 2, this), new Button(MahoMapsApp.text[110], 3, this) }),
new ColumnsContainer(new UIElement[] { new Button(MahoMapsApp.text[109], 2, this),
new Button(MahoMapsApp.text[110], 3, this) }),
new ColumnsContainer(new UIElement[] { new Button(MahoMapsApp.text[104], 4, this),
new Button(MahoMapsApp.text[105], 5, this) }),
new Button(MahoMapsApp.text[1], 6, this) });
new ColumnsContainer(new UIElement[] { new Button("В закладки", 7, this),
new Button(MahoMapsApp.text[1], 6, this) }), });
InvalidateSize();
}

Expand Down Expand Up @@ -132,6 +134,10 @@ public void OnButtonTap(UIElement sender, int uid) {
case 6:
SetNullSelection();
break;
case 7:
BookmarksScreen.BeginAdd(selected,
((JSONObject) selected.object).getObject("properties").getNullableString("name"));
break;
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/mahomaps/overlays/SelectOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import mahomaps.MahoMapsApp;
import mahomaps.map.Geopoint;
import mahomaps.screens.BookmarksScreen;
import mahomaps.screens.SearchLoader;
import mahomaps.ui.Button;
import mahomaps.ui.ColumnsContainer;
Expand All @@ -26,8 +27,9 @@ public SelectOverlay(final Geopoint p) {
selection.color = Geopoint.COLOR_RED;
v.addElement(selection);

content = new FillFlowContainer(new UIElement[] { new SimpleText(p.toString()),
new Button(MahoMapsApp.text[103], 1, this), new ColumnsContainer(new UIElement[] {
content = new FillFlowContainer(new UIElement[] {
new SimpleText(p.toString()), new Button(MahoMapsApp.text[103], 1, this),
new Button("В закладки", 4, this), new ColumnsContainer(new UIElement[] {
new Button(MahoMapsApp.text[104], 2, this), new Button(MahoMapsApp.text[105], 3, this) }),
new Button(MahoMapsApp.text[38], 0, this) });
}
Expand Down Expand Up @@ -63,6 +65,9 @@ public void OnButtonTap(UIElement sender, int uid) {
Close();
RouteBuildOverlay.Get().SetB(selection);
break;
case 4:
BookmarksScreen.BeginAdd(selection, null);
break;
}
}
}
142 changes: 142 additions & 0 deletions src/mahomaps/screens/BookmarksScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package mahomaps.screens;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.rms.RecordStore;

import cc.nnproject.json.JSON;
import cc.nnproject.json.JSONArray;
import cc.nnproject.json.JSONObject;
import mahomaps.MahoMapsApp;
import mahomaps.map.Geopoint;
import mahomaps.map.MapState;
import mahomaps.overlays.RouteBuildOverlay;

public class BookmarksScreen extends List implements CommandListener {

public final static String RMS_NAME = "mm_v1_bookmarks";

private JSONArray list;

private Command from = new Command("Отсюда", Command.ITEM, 0);
private Command to = new Command("Сюда", Command.ITEM, 1);
private Command del = new Command("Удалить", Command.ITEM, 1);

public BookmarksScreen() {
super("Закладки", Choice.IMPLICIT);
list = read();
list.build();
fillList();
addCommand(MahoMapsApp.back);
if (list.size() > 0) {
addCommand(from);
addCommand(to);
addCommand(del);
}
setCommandListener(this);
}

private void fillList() {
for (int i = 0; i < list.size(); i++) {
append(list.getObject(i).getString("name", "Not named"), null);
}
}

private static JSONArray read() {
try {
RecordStore r = RecordStore.openRecordStore(RMS_NAME, true);
byte[] d = null;
if (r.getNumRecords() > 0) {
d = r.getRecord(1);
}
r.closeRecordStore();

if (d == null)
return JSON.getArray("[]");

return JSON.getArray(new String(d, "UTF-8"));
} catch (Throwable e) {
return JSON.getArray("[]");
}
}

public static void BeginAdd(final Geopoint p, String defaultName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет вызова setCurrent, не работает.

final TextBox tb = new TextBox("Название закладки?", defaultName == null ? "" : defaultName, 100, 0);
tb.addCommand(MahoMapsApp.back);
tb.addCommand(MahoMapsApp.ok);
tb.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == MahoMapsApp.back)
MahoMapsApp.BringMap();
else if (c == MahoMapsApp.ok) {
String text = tb.getString();
if (text != null && text.length() > 0) {
Add(p, text);
}
MahoMapsApp.BringMap();
}
}
});
MahoMapsApp.BringSubScreen(tb);
}

public static void Add(Geopoint p, String name) {
JSONArray arr = read();
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("lat", p.lat);
obj.put("lon", p.lon);
arr.add(obj);
Save(arr);
}

private static void Save(JSONArray arr) {
try {
byte[] d = arr.toString().getBytes("UTF-8");
RecordStore r = RecordStore.openRecordStore(RMS_NAME, true);
if (r.getNumRecords() == 0)
r.addRecord(new byte[1], 0, 1);
r.setRecord(1, d, 0, d.length);
r.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
}
}

public void commandAction(Command c, Displayable d) {
if (c == MahoMapsApp.back) {
MahoMapsApp.BringMap();
return;
}

if (list.size() == 0)
return;
int n = getSelectedIndex();
if (n == -1)
return;

if (c == del) {
list.remove(n);
delete(n);
Save(list);
return;
}
JSONObject obj = list.getObject(n);
Geopoint p = new Geopoint(obj.getDouble("lat"), obj.getDouble("lon"));
if (c == SELECT_COMMAND) {
MahoMapsApp.GetCanvas().state = MapState.FocusAt(p);
MahoMapsApp.BringMap();
} else if (c == from) {
RouteBuildOverlay.Get().SetA(p);
MahoMapsApp.BringMap();
} else if (c == to) {
RouteBuildOverlay.Get().SetB(p);
MahoMapsApp.BringMap();
}
}

}
2 changes: 1 addition & 1 deletion src/mahomaps/screens/MapCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected void keyPressed(int k) {
BeginTextSearch();
break handling;
case KEY_NUM9:
// bookmarks
MahoMapsApp.BringSubScreen(new BookmarksScreen());
break handling;
case KEY_STAR:
state = state.ZoomIn();
Expand Down
18 changes: 10 additions & 8 deletions src/mahomaps/screens/MenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class MenuScreen extends List implements CommandListener {

public MenuScreen(TilesProvider tiles) {
super("MahoMaps v1", Choice.IMPLICIT,
new String[] { MahoMapsApp.text[9], MahoMapsApp.text[10], MahoMapsApp.text[11], MahoMapsApp.text[69],
MahoMapsApp.text[12], MahoMapsApp.text[13], MahoMapsApp.text[0] },
new String[] { "Закладки", MahoMapsApp.text[9], MahoMapsApp.text[10], MahoMapsApp.text[11],
MahoMapsApp.text[69], MahoMapsApp.text[12], MahoMapsApp.text[13], MahoMapsApp.text[0] },
null);
this.tiles = tiles;
addCommand(MahoMapsApp.back);
Expand All @@ -30,18 +30,20 @@ public void commandAction(Command c, Displayable d) {
} else if (c == SELECT_COMMAND) {
int sel = getSelectedIndex();
if (sel == 0) {
MahoMapsApp.BringSubScreen(new APIReconnectForm());
MahoMapsApp.BringSubScreen(new BookmarksScreen());
} else if (sel == 1) {
MahoMapsApp.BringSubScreen(new KeyboardHelpScreen());
MahoMapsApp.BringSubScreen(new APIReconnectForm());
} else if (sel == 2) {
MahoMapsApp.BringSubScreen(new SettingsScreen());
MahoMapsApp.BringSubScreen(new KeyboardHelpScreen());
} else if (sel == 3) {
MahoMapsApp.BringSubScreen(new CacheManager(tiles));
MahoMapsApp.BringSubScreen(new SettingsScreen());
} else if (sel == 4) {
MahoMapsApp.BringSubScreen(new AboutScreen());
MahoMapsApp.BringSubScreen(new CacheManager(tiles));
} else if (sel == 5) {
MahoMapsApp.BringSubScreen(new OtherAppsScreen());
MahoMapsApp.BringSubScreen(new AboutScreen());
} else if (sel == 6) {
MahoMapsApp.BringSubScreen(new OtherAppsScreen());
} else if (sel == 7) {
MahoMapsApp.Exit();
}
}
Expand Down