Async loading lists page

This commit is contained in:
Clayton Wilson 2020-10-22 18:06:10 -04:00
parent 20408ea42b
commit ec28e8babc
2 changed files with 61 additions and 26 deletions

View File

@ -7,9 +7,11 @@ import android.view.ViewGroup;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import com.amplifyframework.auth.AuthException; import com.amplifyframework.auth.AuthException;
import com.example.listify.AuthManager; import com.example.listify.AuthManager;
@ -34,16 +36,19 @@ import java.util.Properties;
import static com.example.listify.MainActivity.am; import static com.example.listify.MainActivity.am;
public class ListsFragment extends Fragment implements CreateListDialogFragment.OnNewListListener { public class ListsFragment extends Fragment implements CreateListDialogFragment.OnNewListListener, Requestor.Receiver {
ArrayList<List> shoppingLists = new ArrayList<>(); ArrayList<List> shoppingLists = new ArrayList<>();
DisplayShoppingListsAdapter displayShoppingListsAdapter; DisplayShoppingListsAdapter displayShoppingListsAdapter;
Requestor requestor;
ListView shoppingListsView; ListView shoppingListsView;
ProgressBar loadingLists;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_lists, container, false); View root = inflater.inflate(R.layout.fragment_lists, container, false);
shoppingListsView = root.findViewById(R.id.shopping_lists); shoppingListsView = root.findViewById(R.id.shopping_lists);
loadingLists = (ProgressBar) root.findViewById(R.id.progress_loading_lists);
loadingLists.setVisibility(View.VISIBLE);
// TODO: Switch this to async
Properties configs = new Properties(); Properties configs = new Properties();
try { try {
configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json"); configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json");
@ -51,35 +56,18 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
e.printStackTrace(); e.printStackTrace();
} }
Requestor requestor = new Requestor(am, configs.getProperty("apiKey")); requestor = new Requestor(am, configs.getProperty("apiKey"));
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>(); SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver); final Requestor.Receiver<Integer[]> recv = this;
try { Thread t = new Thread(new Runnable() {
Integer[] listIds = listIdsReceiver.await();
for (int i = 0; i < listIds.length; i++) {
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
shoppingLists.add(listReceiver.await());
}
} catch (Exception e) {
e.printStackTrace();
}
// Set adapter and display this users lists
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists);
shoppingListsView.setAdapter(displayShoppingListsAdapter);
shoppingListsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void run() {
Intent listPage = new Intent(getContext(), ListPage.class); requestor.getListOfIds(List.class, recv, null);
// Send the list ID
listPage.putExtra("listID", shoppingLists.get(position).getItemID());
startActivity(listPage);
} }
}); });
t.start();
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab); FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab);
Fragment thisFragment = this; Fragment thisFragment = this;
@ -120,4 +108,41 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override
public void acceptDelivery(Object delivered) {
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
Integer[] listIds = (Integer[]) delivered;
try {
// Integer[] listIds = listIdsReceiver.await();
for (int i = 0; i < listIds.length; i++) {
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
shoppingLists.add(listReceiver.await());
}
} catch (Exception e) {
e.printStackTrace();
}
// Set adapter and display this users lists
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
shoppingListsView.setAdapter(displayShoppingListsAdapter);
shoppingListsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent listPage = new Intent(getContext(), ListPage.class);
// Send the list ID
listPage.putExtra("listID", shoppingLists.get(position).getItemID());
startActivity(listPage);
}
});
loadingLists.setVisibility(View.GONE);
}
});
}
} }

View File

@ -12,6 +12,16 @@
tools:context=".ui.lists.ListsFragment" tools:context=".ui.lists.ListsFragment"
tools:showIn="@layout/fragment_lists"> tools:showIn="@layout/fragment_lists">
<ProgressBar
android:id="@+id/progress_loading_lists"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:visibility="gone"/>
<ListView <ListView
android:id="@+id/shopping_lists" android:id="@+id/shopping_lists"
android:layout_width="fill_parent" android:layout_width="fill_parent"