Merge branch 'master' into improvements

This commit is contained in:
2020-11-15 20:14:20 -05:00
committed by GitHub
23 changed files with 442 additions and 61 deletions

View File

@@ -5,25 +5,16 @@ import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.*;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide;
import com.example.listify.data.Chain;
import com.example.listify.data.Item;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.data.ListShare;
import com.example.listify.ui.home.HomeFragment;
import com.bumptech.glide.Glide;
import com.example.listify.data.*;
import org.json.JSONException;
import java.io.IOException;
import java.text.DecimalFormat;
@@ -32,8 +23,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.json.JSONException;
import static com.example.listify.MainActivity.am;
public class ListPage extends AppCompatActivity implements Requestor.Receiver {
@@ -132,9 +121,10 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
public void onClick(DialogInterface dialog, int which) {
EditText sharedEmailText = (EditText) codeView.findViewById(R.id.editTextTextSharedEmail);
String sharedEmail = sharedEmailText.getText().toString();
ListShare listShare = new ListShare(LIST_ID, sharedEmail);
ListShare listShare = new ListShare(LIST_ID, sharedEmail, "Read, Write, Delete, Share");
try {
requestor.postObject(listShare);
requestor.putObject(listShare);
}
catch(Exception e) {
e.printStackTrace();

View File

@@ -16,19 +16,15 @@ import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.amplifyframework.auth.AuthException;
import com.example.listify.data.Item;
import com.example.listify.data.ItemSearch;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.data.SearchHistory;
import com.example.listify.ui.LoginPage;
import com.google.android.material.navigation.NavigationView;
import org.json.JSONException;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import static com.example.listify.SplashActivity.showSplash;
@@ -108,7 +104,14 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
}
Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey"));
SynchronousReceiver<SearchHistory> historyReceiver = new SynchronousReceiver<>();
requestor.getObject("N/A", SearchHistory.class, historyReceiver, historyReceiver);
try {
System.out.println(historyReceiver.await());
} catch (Exception e) {
e.printStackTrace();
}
/*
List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli());
ListEntry entry = new ListEntry(1, 4, Math.abs(new Random().nextInt()), Instant.now().toEpochMilli(),false);
@@ -139,6 +142,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
} catch (Exception receiverError) {
receiverError.printStackTrace();
}
*/
}
//------------------------------------------------------------------------------------------//

View File

@@ -60,6 +60,25 @@ public class Requestor {
launchCall(deleteRequest, null, classType, failureHandler);
}
public void putObject(Object toPost) throws JSONException {
putObject(toPost, (RequestErrorHandler) null);
}
public void putObject(Object toPost, RequestErrorHandler failureHandler) throws JSONException {
putObject(toPost, null, failureHandler);
}
public void putObject(Object toPost, Receiver<Integer> idReceiver) throws JSONException {
putObject(toPost, idReceiver, null);
}
public void putObject(Object toPut, Receiver<Integer> idReceiver, RequestErrorHandler failureHandler) throws JSONException {
String putURL = DEV_BASEURL + "/" + toPut.getClass().getSimpleName();
Request putRequest = buildBaseRequest(putURL, "PUT", new Gson().toJson(toPut));
launchCall(putRequest, idReceiver, Integer.class, failureHandler);
}
public void postObject(Object toPost) throws JSONException {
postObject(toPost, (RequestErrorHandler) null);
}
@@ -85,7 +104,7 @@ public class Requestor {
String responseString = response.body().string();
if (receiver != null) {
if (classType == null) {
Log.e("Requestor Contract Error", "classType while receiver populated");
Log.e("Requestor Contract Error", "no/null classType while receiver populated");
}
try {
receiver.acceptDelivery(new Gson().fromJson(responseString, classType));

View File

@@ -133,9 +133,9 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter {
public void onClick(DialogInterface dialog, int which) {
EditText sharedEmailText = (EditText) codeView.findViewById(R.id.editTextTextSharedEmail);
String sharedEmail = sharedEmailText.getText().toString();
ListShare listShare = new ListShare(curList.getItemID(), sharedEmail);
ListShare listShare = new ListShare(curList.getItemID(), sharedEmail, "Read, Write, Delete, Share");
try {
requestor.postObject(listShare);
requestor.putObject(listShare);
}
catch(Exception e) {
e.printStackTrace();

View File

@@ -1,12 +1,67 @@
package com.example.listify.data;
import com.example.listify.BuildConfig;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ListShare {
Integer listID;
String shareWithEmail;
Integer permissionLevel;
public ListShare(Integer listID, String shareWithEmail) {
private static final Map<Integer, String> keysToPerms;
static {
//All keys should be a prime number > 1
//All keys need to be maintained here and in List module->ListPermissions class on the Lambda side
HashMap<Integer, String> keysToPermsTemp = new HashMap<>();
keysToPermsTemp.put(2, "read");
keysToPermsTemp.put(3, "write");
keysToPermsTemp.put(5, "delete");
keysToPermsTemp.put(7, "share");
keysToPerms = Collections.unmodifiableMap(keysToPermsTemp);
}
public ListShare(Integer listID, String shareWithEmail, Integer permissionLevel) {
this.listID = listID;
this.shareWithEmail = shareWithEmail;
this.permissionLevel = permissionLevel;
}
public ListShare(Integer listID, String shareWithEmail, String permissionsRaw) {
String permissions = permissionsRaw.toLowerCase();
this.listID = listID;
this.shareWithEmail = shareWithEmail;
permissionLevel = 1;
for (Map.Entry<Integer, String> keytoPermEntry: keysToPerms.entrySet()) {
if (permissions.contains(keytoPermEntry.getValue())) {
permissionLevel *= keytoPermEntry.getKey();
}
}
}
@Override
public String toString() {
StringBuilder toReturn = new StringBuilder("ListShare{" +
"listID=" + listID +
", shareWithEmail='" + shareWithEmail + '\'' +
", permissionLevel=" + permissionLevel +
" [Permissions: ");
int permissionLevelCopy = permissionLevel;
for (Integer permissionObject : keysToPerms.keySet()) {
Integer permissionInteger = permissionObject;
if (permissionLevelCopy % permissionInteger == 0) {
permissionLevelCopy /= permissionInteger;
toReturn.append(keysToPerms.get(permissionInteger)).append(",");
}
}
if (BuildConfig.DEBUG && permissionLevelCopy != 1) {
throw new AssertionError("Assertion failed");
}
toReturn.append("]}");
return toReturn.toString();
}
public Integer getListID() {
@@ -24,4 +79,13 @@ public class ListShare {
public void setShareWithEmail(String shareWithEmail) {
this.shareWithEmail = shareWithEmail;
}
public Integer getPermissionLevel() {
return permissionLevel;
}
public void setPermissionLevel(Integer permissionLevel) {
this.permissionLevel = permissionLevel;
}
}

View File

@@ -0,0 +1,30 @@
package com.example.listify.data;
import java.util.ArrayList;
public class SearchHistory {
ArrayList<String> searches;
public SearchHistory(ArrayList<String> searches) {
this.searches = searches;
}
public ArrayList<String> getSearches() {
return searches;
}
public void setSearches(ArrayList<String> searches) {
this.searches = searches;
}
public void addSearch(String newSearch) {
searches.add(newSearch);
}
@Override
public String toString() {
return "SearchHistory{" +
"searches=" + searches +
'}';
}
}