Drag to refresh lists activity

This commit is contained in:
Clayton Wilson 2020-11-15 00:21:38 -05:00
parent 0aa80b514f
commit 3de53be847
4 changed files with 49 additions and 18 deletions

View File

@ -62,8 +62,12 @@ public class CreateListAddDialogFragment extends DialogFragment {
btnMinus.setOnClickListener(new View.OnClickListener() { btnMinus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (etQuantity.getText().toString().equals("")) {
etQuantity.setText("1");
}
int curQauntity = Integer.parseInt(etQuantity.getText().toString()); int curQauntity = Integer.parseInt(etQuantity.getText().toString());
if (curQauntity > 0) { if (curQauntity > 1) {
curQauntity--; curQauntity--;
etQuantity.setText(String.format("%d", curQauntity)); etQuantity.setText(String.format("%d", curQauntity));
} }
@ -74,6 +78,10 @@ public class CreateListAddDialogFragment extends DialogFragment {
btnPlus.setOnClickListener(new View.OnClickListener() { btnPlus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (etQuantity.getText().toString().equals("")) {
etQuantity.setText("1");
}
int curQauntity = Integer.parseInt(etQuantity.getText().toString()); int curQauntity = Integer.parseInt(etQuantity.getText().toString());
curQauntity++; curQauntity++;
etQuantity.setText(String.format("%d", curQauntity)); etQuantity.setText(String.format("%d", curQauntity));

View File

@ -6,11 +6,14 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
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.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.example.listify.AuthManager; import com.example.listify.AuthManager;
import com.example.listify.CreateListDialogFragment; import com.example.listify.CreateListDialogFragment;
import com.example.listify.LoadingCircleDialog; import com.example.listify.LoadingCircleDialog;
@ -37,6 +40,7 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
ListView shoppingListsView; ListView shoppingListsView;
ProgressBar loadingLists; ProgressBar loadingLists;
TextView emptyMessage; TextView emptyMessage;
SwipeRefreshLayout refreshLists;
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_home, container, false); View root = inflater.inflate(R.layout.fragment_home, container, false);
@ -44,6 +48,7 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
loadingLists = (ProgressBar) root.findViewById(R.id.progress_loading_lists); loadingLists = (ProgressBar) root.findViewById(R.id.progress_loading_lists);
loadingLists.setVisibility(View.VISIBLE); loadingLists.setVisibility(View.VISIBLE);
emptyMessage = (TextView) root.findViewById(R.id.textViewEmpty); emptyMessage = (TextView) root.findViewById(R.id.textViewEmpty);
refreshLists = (SwipeRefreshLayout) root.findViewById(R.id.refresh_lists);
Properties configs = new Properties(); Properties configs = new Properties();
try { try {
@ -55,8 +60,8 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
requestor = new Requestor(am, configs.getProperty("apiKey")); requestor = new Requestor(am, configs.getProperty("apiKey"));
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>(); SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
final Requestor.Receiver<Integer[]> recv = this; // final Requestor.Receiver<Integer[]> recv = this;
requestor.getListOfIds(List.class, recv, null); requestor.getListOfIds(List.class, this, null);
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab); FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab);
@ -70,6 +75,23 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
} }
}); });
refreshLists.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Properties configs = new Properties();
try {
configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
requestor = new Requestor(am, configs.getProperty("apiKey"));
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
requestor.getListOfIds(List.class, HomeFragment.this, null);
}
});
return root; return root;
} }
@ -130,6 +152,9 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
@Override @Override
public void acceptDelivery(Object delivered) { public void acceptDelivery(Object delivered) {
// Remove old lists on refresh
shoppingLists.clear();
Integer[] listIds = (Integer[]) delivered; Integer[] listIds = (Integer[]) delivered;
// Create threads and add them to a list // Create threads and add them to a list
Thread[] threads = new Thread[listIds.length]; Thread[] threads = new Thread[listIds.length];
@ -169,16 +194,6 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
@Override @Override
public void run() { public void run() {
shoppingListsView.setAdapter(shoppingListsSwipeableAdapter); shoppingListsView.setAdapter(shoppingListsSwipeableAdapter);
// 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); loadingLists.setVisibility(View.GONE);
if(listIds.length == 0) { if(listIds.length == 0) {
@ -187,5 +202,6 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
} }
}); });
refreshLists.setRefreshing(false);
} }
} }

View File

@ -35,6 +35,7 @@
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="50dp" android:layout_height="50dp"
android:text="@string/_1" android:text="@string/_1"
android:digits="0123456789"
android:inputType="number"/> android:inputType="number"/>
<Button <Button

View File

@ -20,6 +20,10 @@
android:indeterminate="true" android:indeterminate="true"
android:visibility="gone"/> android:visibility="gone"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_lists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView <ListView
android:id="@+id/shopping_lists" android:id="@+id/shopping_lists"
android:layout_width="fill_parent" android:layout_width="fill_parent"
@ -27,6 +31,8 @@
android:divider="@color/list_divider" android:divider="@color/list_divider"
android:dividerHeight="1dp"/> android:dividerHeight="1dp"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/new_list_fab" android:id="@+id/new_list_fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"