package com.rantrum.katana;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.balysv.materialripple.MaterialRippleLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.rantrum.katana.adapter.AdvancedItemListAdapter;
import com.rantrum.katana.app.App;
import com.rantrum.katana.constants.Constants;
import com.rantrum.katana.model.Item;
import com.rantrum.katana.util.Api;
import com.rantrum.katana.util.CustomRequest;
public class RecentlyDeletedFragment extends Fragment implements Constants, SwipeRefreshLayout.OnRefreshListener {
private static final String STATE_LIST = "State Adapter Data";
private RecyclerView mRecyclerView;
private NestedScrollView mNestedView;
private SwipeRefreshLayout mItemsContainer;
private BottomSheetBehavior mBehavior;
private BottomSheetDialog mBottomSheetDialog;
private View mBottomSheet;
private TextView mMessage;
private ImageView mSplash;
private ArrayList
private AdvancedItemListAdapter itemsAdapter;
private int itemId = 0;
private int arrayLength = 0;
private Boolean loadingMore = false;
private Boolean viewMore = false;
private Boolean restore = false;
public RecentlyDeletedFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
itemsList = savedInstanceState.getParcelableArrayList(STATE_LIST);
itemsAdapter = new AdvancedItemListAdapter(getActivity(), itemsList);
restore = savedInstanceState.getBoolean("restore");
itemId = savedInstanceState.getInt("itemId");
} else {
itemsList = new ArrayList
itemsAdapter = new AdvancedItemListAdapter(getActivity(), itemsList);
restore = false;
itemId = 0;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recently_deleted, container, false);
mItemsContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.container_items);
mItemsContainer.setOnRefreshListener(this);
//
mMessage = (TextView) rootView.findViewById(R.id.message);
mSplash = (ImageView) rootView.findViewById(R.id.splash);
// Prepare bottom sheet
mBottomSheet = rootView.findViewById(R.id.bottom_sheet);
mBehavior = BottomSheetBehavior.from(mBottomSheet);
//
mNestedView = (NestedScrollView) rootView.findViewById(R.id.nested_view);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
itemsAdapter.setOnMoreButtonClickListener(new AdvancedItemListAdapter.OnItemMenuButtonClickListener() {
@Override
public void onItemClick(View v, Item obj, int actionId, int position) {
switch (actionId){
case ITEM_ACTION_REPOST: {
break;
}
case ITEM_ACTIONS_MENU: {
showItemActionDialog(position);
break;
}
}
}
});
final GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(itemsAdapter);
mRecyclerView.setNestedScrollingEnabled(false);
mNestedView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY < oldScrollY) { // up
}
if (scrollY > oldScrollY) { // down
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
if (!loadingMore && (viewMore) && !(mItemsContainer.isRefreshing())) {
mItemsContainer.setRefreshing(true);
loadingMore = true;
getItems();
}
}
}
});
if (itemsAdapter.getItemCount() == 0) {
showMessage(getText(R.string.label_empty_list).toString());
} else {
hideMessage();
}
if (!restore) {
showMessage(getText(R.string.msg_loading_2).toString());
getItems();
}
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("restore", true);
outState.putInt("itemId", itemId);
outState.putParcelableArrayList(STATE_LIST, itemsList);
}
@Override
public void onRefresh() {
if (App.getInstance().isConnected()) {
itemId = 0;
getItems();
} else {
mItemsContainer.setRefreshing(false);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
public void getItems() {
mItemsContainer.setRefreshing(true);
CustomRequest jsonReq = new CustomRequest(Request.Method.POST, METHOD_ITEMS_GET_RECENTLY_DELETED, null,
new Response.Listener
@Override
public void onResponse(JSONObject response) {
if (!isAdded() || getActivity() == null) {
Log.e("ERROR", "RecentlyDeletedFragment Not Added to Activity");
return;
}
if (!loadingMore) {
itemsList.clear();
}
try {
arrayLength = 0;
if (!response.getBoolean("error")) {
itemId = response.getInt("itemId");
if (response.has("items")) {
JSONArray itemsArray = response.getJSONArray("items");
arrayLength = itemsArray.length();
if (arrayLength > 0) {
for (int i = 0; i < itemsArray.length(); i++) {
JSONObject itemObj = (JSONObject) itemsArray.get(i);
Item item = new Item(itemObj);
item.setAd(0);
itemsList.add(item);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
loadingComplete();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (!isAdded() || getActivity() == null) {
Log.e("ERROR", "RecentlyDeletedFragment Not Added to Activity");
return;
}
loadingComplete();
}
}) {
@Override
protected Map
Map
params.put("accountId", Long.toString(App.getInstance().getId()));
params.put("accessToken", App.getInstance().getAccessToken());
params.put("itemId", Integer.toString(itemId));
params.put("language", "en");
return params;
}
};
App.getInstance().addToRequestQueue(jsonReq);
}
public void loadingComplete() {
if (arrayLength == LIST_ITEMS) {
viewMore = true;
} else {
viewMore = false;
}
itemsAdapter.notifyDataSetChanged();
if (itemsAdapter.getItemCount() == 0) {
if (RecentlyDeletedFragment.this.isVisible()) {
showMessage(getText(R.string.label_empty_list).toString());
}
} else {
hideMessage();
}
loadingMore = false;
mItemsContainer.setRefreshing(false);
}
// Item action
private void showItemActionDialog(final int position) {
if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
final View view = getLayoutInflater().inflate(R.layout.item_restore_actions_sheet_list, null);
MaterialRippleLayout mRestoreButton = (MaterialRippleLayout) view.findViewById(R.id.restore_button);
MaterialRippleLayout mDeleteButton = (MaterialRippleLayout) view.findViewById(R.id.delete_button);
MaterialRippleLayout mPinButton = (MaterialRippleLayout) view.findViewById(R.id.pin_button);
final Item item = itemsList.get(position);
mPinButton.setVisibility(View.GONE);
mRestoreButton.setVisibility(View.VISIBLE);
mDeleteButton.setVisibility(View.VISIBLE);
mRestoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBottomSheetDialog.dismiss();
restore(position);
}
});
mDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBottomSheetDialog.dismiss();
delete(position);
}
});
mBottomSheetDialog = new BottomSheetDialog(getActivity());
mBottomSheetDialog.setContentView(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomSheetDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
mBottomSheetDialog.show();
doKeepDialog(mBottomSheetDialog);
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
// Prevent dialog dismiss when orientation changes
private static void doKeepDialog(Dialog dialog){
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(lp);
}
public void delete(final int position) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle(getText(R.string.label_delete_permanently_post));
alertDialog.setMessage(getText(R.string.label_delete_permanently_post_desc));
alertDialog.setCancelable(true);
alertDialog.setNegativeButton(getText(R.string.action_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton(getText(R.string.action_yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final Item item = itemsList.get(position);
itemsList.remove(position);
itemsAdapter.notifyDataSetChanged();
if (itemsAdapter.getItemCount() == 0) {
showMessage(getText(R.string.label_empty_list).toString());
}
if (App.getInstance().isConnected()) {
Api api = new Api(getActivity());
api.postDelete(item.getId(), 0);
} else {
Toast.makeText(getActivity(), getText(R.string.msg_network_error), Toast.LENGTH_SHORT).show();
}
}
});
alertDialog.show();
}
public void restore(final int position) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle(getText(R.string.label_restore_post));
alertDialog.setMessage(getText(R.string.label_restore_post_desc));
alertDialog.setCancelable(true);
alertDialog.setNegativeButton(getText(R.string.action_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton(getText(R.string.action_yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final Item item = itemsList.get(position);
itemsList.remove(position);
itemsAdapter.notifyDataSetChanged();
if (itemsAdapter.getItemCount() == 0) {
showMessage(getText(R.string.label_empty_list).toString());
}
if (App.getInstance().isConnected()) {
Api api = new Api(getActivity());
api.itemRestore(item.getId());
} else {
Toast.makeText(getActivity(), getText(R.string.msg_network_error), Toast.LENGTH_SHORT).show();
}
}
});
alertDialog.show();
}
//
public void showMessage(String message) {
mMessage.setText(message);
mMessage.setVisibility(View.VISIBLE);
mSplash.setVisibility(View.VISIBLE);
}
public void hideMessage() {
mMessage.setVisibility(View.GONE);
mSplash.setVisibility(View.GONE);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}