Skip to content

Commit

Permalink
Finally got saving/loading channel layout maps working!
Browse files Browse the repository at this point in the history
I had to use Gson... And here's the juicy bit that makes it prefer integers: google/gson#1290
  • Loading branch information
theonlytechnohead committed Oct 26, 2022
1 parent 16d8d4c commit 5ac2d3e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Expand Up @@ -36,6 +36,7 @@ android {
dependencies {

//implementation 'com.github.alpbak:BoxedVerticalSeekBar:1.1.1'
implementation 'com.google.code.gson:gson:2.10'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'com.illposed.osc:javaosc-core:0.8'
Expand Down
Expand Up @@ -49,7 +49,7 @@ public class ChannelStripRecyclerViewAdapter extends RecyclerView.Adapter<Recycl
int groups = 0;
private final HashMap<Integer, ArrayList<ChannelStrip>> groupedChannels = new HashMap<>();

public ChannelStripRecyclerViewAdapter(ItemMoveCallback.StartDragListener startDragListener, Context context, int numChannels, HashMap<Integer, Integer> channelLayer, ArrayList<Integer> channelColours, float width) {
public ChannelStripRecyclerViewAdapter(ItemMoveCallback.StartDragListener startDragListener, Context context, int numChannels, HashMap<Integer, Integer> channelLayer, HashMap<Integer, Object> layout, ArrayList<Integer> channelColours, float width) {
this.context = context;
this.startDragListener = startDragListener;
colourArray = context.getResources().getIntArray(R.array.mixer_colours);
Expand Down Expand Up @@ -80,6 +80,9 @@ public ChannelStripRecyclerViewAdapter(ItemMoveCallback.StartDragListener startD
channels.add(entry.getValue());
notifyItemInserted(entry.getKey());
}
for (Map.Entry<Integer, Object> entry : layout.entrySet()) {
// Log.i("LAYOUT", entry.getKey() + " " + entry.getValue());
}
}

@NonNull
Expand Down Expand Up @@ -725,7 +728,7 @@ public HashMap<Integer, Object> getLayout() {
if (!channel.group) {
layout.put(i, channel.index);
}
if (channel.group && channel.groupIndex == 0) {
if (channel.group && channel.colour != 0) {
Group group = new Group();
group.index = channel.index;
group.name = channel.name;
Expand All @@ -738,6 +741,7 @@ public HashMap<Integer, Object> getLayout() {
group.channels.put(j, subchannel.index);
}
}
layout.put(group.index, group.toMap());
}
}
for (Map.Entry<Integer, ChannelStrip> entry : hiddenChannels.entrySet()) {
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/net/ddns/anderserver/touchfadersapp/Group.java
@@ -1,8 +1,10 @@
package net.ddns.anderserver.touchfadersapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Group {
Expand All @@ -11,20 +13,36 @@ public class Group {

public String name;
public int colourIndex;
public int groupIndex = -1;

public HashMap<Integer, Integer> channels = new HashMap<>();
public Map<Integer, Integer> channels = new HashMap<>();

@NonNull
@Override
public String toString() {
return "Group {" + name + ", channels: " + channels + "}";
}

@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof Group) {
return ((Group) obj).index == index;
Group other = (Group) obj;
if (index != other.index) return false;
return channels == other.channels;
}
return false;

}

@Override
public int hashCode() {
return Objects.hash(index);
}

Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("index", index);
map.put("name", name);
map.put("channels", channels);
return map;
}
}
Expand Up @@ -103,7 +103,7 @@ protected void onStart() {
super.onStart();
width = 70;
channelLayer = loadMap();
adapter = new ChannelStripRecyclerViewAdapter(this, instanceContext, numChannels, channelLayer, channelColours, width);
adapter = new ChannelStripRecyclerViewAdapter(this, instanceContext, numChannels, channelLayer, loadLayout(), channelColours, width);
adapter.setValuesChangeListener((index, points) -> {
});
adapter.setFaderMuteListener(((view, index, muted) -> {
Expand Down Expand Up @@ -208,6 +208,47 @@ private void saveMap(HashMap<Integer, Integer> inputMap) {
}
}

private void saveLayout(HashMap<Integer, Object> inputLayout) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (preferences != null) {
JSONObject json = new JSONObject();
for (Map.Entry<Integer, Object> inputEntry : inputLayout.entrySet()) {
try {
json.put(inputEntry.getKey().toString(), inputEntry.getValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
String jsonString = json.toString();
preferences.edit()
.remove("channel_layout")
.putString("channel_layout", jsonString)
.apply();
}
}

private HashMap<Integer, Object> loadLayout() {
HashMap<Integer, Object> outputMap = new HashMap<>();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
try {
if (preferences != null) {
String jsonString = preferences.getString("channel_layout", (new JSONObject()).toString());
if (jsonString != null) {
JSONObject json = new JSONObject(jsonString);
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = json.get(key);
outputMap.put(Integer.valueOf(key), value);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return outputMap;
}

private HashMap<Integer, Integer> loadMap() {
HashMap<Integer, Integer> outputMap = new HashMap<>();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Expand Down
Expand Up @@ -25,6 +25,10 @@
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.google.gson.reflect.TypeToken;
import com.illposed.osc.OSCMessage;
import com.illposed.osc.OSCMessageEvent;
import com.illposed.osc.OSCMessageListener;
Expand All @@ -34,6 +38,7 @@
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketTimeoutException;
Expand All @@ -59,6 +64,8 @@ public class MainActivity extends AppCompatActivity implements ItemMoveCallback.

public static final String COLOUR = "colour";

public static final String JSON_CHANNEL_LAYOUT = "channel_layout";

Thread udpListenerThread;
boolean runUDP = true;

Expand Down Expand Up @@ -103,7 +110,7 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
width = Float.parseFloat(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(getResources().getString(R.string.setting_fader_width), "35"));

channelLayer = loadMap();
adapter = new ChannelStripRecyclerViewAdapter(MainActivity.this, instanceContext, numChannels, channelLayer, channelColours, width);
adapter = new ChannelStripRecyclerViewAdapter(MainActivity.this, instanceContext, numChannels, channelLayer, loadLayout(), channelColours, width);
adapter.setValuesChangeListener((index, points) -> SendOSCFaderValue(index + 1, points));
adapter.setFaderMuteListener(((view, index, muted) -> SendOSCChannelMute(index + 1, muted)));
recyclerView = findViewById(R.id.faderRecyclerView);
Expand Down Expand Up @@ -329,7 +336,7 @@ protected void onStart() {
} else {
width = Float.parseFloat(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(getResources().getString(R.string.setting_fader_width), "35"));
channelLayer = loadMap();
adapter = new ChannelStripRecyclerViewAdapter(this, instanceContext, numChannels, channelLayer, channelColours, width);
adapter = new ChannelStripRecyclerViewAdapter(this, instanceContext, numChannels, channelLayer, loadLayout(), channelColours, width);
adapter.setValuesChangeListener((index, points) -> {
});
adapter.setFaderMuteListener(((view, index, muted) -> {
Expand Down Expand Up @@ -535,37 +542,39 @@ private void saveLayout(HashMap<Integer, Object> inputLayout) {
JSONObject json = new JSONObject();
for (Map.Entry<Integer, Object> inputEntry : inputLayout.entrySet()) {
try {
json.put(inputEntry.getKey().toString(), inputEntry.getValue());
if (inputEntry.getValue() instanceof Group) {
Group group = (Group) inputEntry.getValue();
json.put(inputEntry.getKey().toString(), new JSONObject(group.toMap()));
} else {
json.put(inputEntry.getKey().toString(), inputEntry.getValue());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
String jsonString = json.toString();
preferences.edit()
.remove("channel_layout")
.putString("channel_layout", jsonString)
.remove(JSON_CHANNEL_LAYOUT)
.putString(JSON_CHANNEL_LAYOUT, jsonString)
.apply();
}
}

private HashMap<Integer, Object> loadLayout() {
HashMap<Integer, Object> outputMap = new HashMap<>();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
try {
if (preferences != null) {
String jsonString = preferences.getString("channel_layout", (new JSONObject()).toString());
if (jsonString != null) {
JSONObject json = new JSONObject(jsonString);
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = json.get(key);
outputMap.put(Integer.valueOf(key), value);
}
if (preferences != null) {
String jsonString = preferences.getString(JSON_CHANNEL_LAYOUT, (new JSONObject()).toString());
if (jsonString != null) {
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> map = gson.fromJson(jsonString, mapType);
for (String key : map.keySet()) {
Object value = map.get(key);
outputMap.put(Integer.valueOf(key), value);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return outputMap;
}
Expand Down

0 comments on commit 5ac2d3e

Please sign in to comment.