Skip to content

Commit

Permalink
i think
Browse files Browse the repository at this point in the history
i __think__
sync works
  • Loading branch information
Shaji Khan committed Nov 24, 2024
1 parent 3c9fb1a commit 9f6505f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
109 changes: 109 additions & 0 deletions app/src/main/java/com/shajikhan/ladspa/amprack/FirestoreDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
import com.google.firebase.firestore.WriteBatch;
import com.google.firestore.v1.WriteResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -137,6 +140,112 @@ public void onSuccess(Void unused) {
}
}

@ServerTimestamp
public void savePresets(JSONObject jsonObject, boolean shared, AlertDialog dialog, MyPresets myPresets) {
FirebaseAuth auth = FirebaseAuth.getInstance() ;
WriteBatch batch = db.batch();
CollectionReference collectionReference = db.collection("presets");
if (auth == null) {
Log.e(TAG, "savePreset: uid is null", null);
return ;
} else if (auth.getUid() == null) {
Log.e(TAG, "loadUserPresets: uid is null", null);
return ;
}

Iterator<String> keys = jsonObject.keys();

while(keys.hasNext()) {
String key = keys.next();
JSONObject jo ;
Map<String, Object> data = new HashMap<>();
try {
jo = jsonObject.getJSONObject(key);
data.put("name", jo.get("name"));
data.put("desc", jo.get("desc"));
data.put("likes", 0);

data.put("public", shared);
data.put("controls", MainActivity.JSONtoMap(jo.getJSONObject("controls")));
} catch (JSONException e) {
throw new RuntimeException(e);
}

data.put("uid", auth.getUid());
data.put("timestamp", FieldValue.serverTimestamp());

OnSuccessListener<DocumentReference> successListener = new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
// Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
dialog.dismiss();
Toast.makeText(context,
"Patch saved successfully",
Toast.LENGTH_LONG)
.show();
data.put("path", documentReference.getPath());
data.put("uid", auth.getUid());
((MainActivity) context).lastPresetLoadedPath = documentReference.getPath().split("presets")[1];
((MainActivity) context).lastPresetLoadedUID = auth.getUid();
myPresets.myPresetsAdapter.addPreset(data);
}
};

OnFailureListener failureListener = new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
Toast.makeText(context,
"Could not save patch: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
};

DocumentReference documentReference = collectionReference.document() ;
batch.set(documentReference, data);

// db.collection("presets")
// .add(data)
// .addOnSuccessListener(successListener)
// .addOnFailureListener(failureListener);
}

Task<Void> task = batch.commit();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
dialog.dismiss();
Toast.makeText(context,
"Patch saved successfully",
Toast.LENGTH_LONG)
.show();

Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
JSONObject jo;
try {
jo = jsonObject.getJSONObject(key);
myPresets.myPresetsAdapter.addPreset(MainActivity.JSONtoMap(jo));
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
Toast.makeText(context,
"Could not save imported presets to cloud: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}

public void loadUserPresets (MyPresetsAdapter presetsAdapter, boolean shared, boolean quick) {
if (presetsAdapter.progressBar != null) {
presetsAdapter.progressBar.setVisibility(View.VISIBLE);
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/shajikhan/ladspa/amprack/Rack.java
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,8 @@ void syncDialog () {

Button btn = linearLayout.findViewById(R.id.sync);
EditText editText = linearLayout.findViewById(R.id.ip);
AlertDialog dialog = builder.create();

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -1328,6 +1330,7 @@ public void onClick(View v) {
Log.d(TAG, "[preset]: " + jo.toString());
}

mainActivity.presets.fragmentStateAdapter.myPresets.myPresetsAdapter.db.savePresets(j, false, dialog, mainActivity.presets.fragmentStateAdapter.myPresets);
Log.d(TAG, "[sync]: end presets processing");
} catch (ExecutionException e) {
throw new RuntimeException(e);
Expand All @@ -1338,7 +1341,8 @@ public void onClick(View v) {
}
}
});
builder.show();

dialog.show();
}

public String syncData(String host, int port, String toSend){
Expand Down

0 comments on commit 9f6505f

Please sign in to comment.