我试图让一个选中的按钮,用户点击它的位置会将hasSeen布尔值设置为true。问题是当用户刷新时,它将从服务器获取新数据并覆盖该值。我试图缓存它,但似乎我卡住了。
public class CaseFragment extends BaseFragment implements View.OnClickListener {
private CaseAdapter caseAdapter;
private RecyclerView caseRecycler;
private ArrayList<Rfq> caseList,ca;
private ArrayList<DealerCase> caseDetails;
private DealerCase carDetails;
private RequestQueue rq;
private TextView textUpdatedTime,test2;
private SwipeRefreshLayout swipeRefreshLayout;
public CaseFragment(){
caseList = new ArrayList<>();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_cases, null);
caseRecycler = (RecyclerView)view.findViewById(R.id.recycler_view_case);
textUpdatedTime = (TextView)view.findViewById(R.id.textView8);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
caseRecycler.setLayoutManager(linearLayoutManager);
caseAdapter = new CaseAdapter();
caseAdapter.setData(caseList);
caseRecycler.setAdapter(caseAdapter);
caseRecycler.setFocusable(false);
rq = Volley.newRequestQueue(getContext());
lastUpdated();
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
// swipe refresh layout
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//loadCase();
requestNewCase();
lastUpdated();
swipeRefreshLayout.setRefreshing(false);
}
});
caseAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
String uid = caseList.get(position).getUid();
caseList.get(position).setIsSeen(true);
requestNewCase();
caseAdapter.notifyDataSetChanged();
getCaseDetails(uid,KeyConfig.NEW);
}
});
return view;
}
private void loadFromCache() {
String uri = String.format(UriManager.getDealerCase(),
GlobalConstant.NEW);
ApiManager.getInstance().authUrlRequest(getActivity(), KeyConfig.CACHE_NEW, true, true, StringRequest.Method.GET, uri, new OnResponseListener() {
@Override
public void onResponse(String response, boolean fromCache) {
if(response != null) {
caseList.clear();
String newList = CacheManager.getInstance().getCache().get(KeyConfig.CACHE_NEW);
Gson gson = new Gson();
DealerCase caseModel = gson.fromJson(newList, DealerCase.class);
if(caseModel.getData().getRfqs() != null){
int b = caseModel.getData().getRfqs().size();
//Toast.makeText(getContext(), String.valueOf(ca.size())+" - " + String.valueOf(caseModel.getData().getRfqs().size()),Toast.LENGTH_LONG).show();
for(int i=0;i<b;i++){
if(caseModel.getData().getRfqs().get(i).getIsSeen() == null){
caseModel.getData().getRfqs().get(i).setIsSeen(false);
}
if(caseModel.getData().getRfqs().get(i).getIsSeen()){
caseModel.getData().getRfqs().get(i).setIsSeen(false);
}
caseList.add(i,caseModel.getData().getRfqs().get(i));
}
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
private void lastUpdated() {
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
System.out.print(ts);
long unixSeconds = Long.valueOf(ts);
Date date = new Date(unixSeconds*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("E hh:mma"); // the format of your date
//get gmt
Calendar mCalendar = new GregorianCalendar();
TimeZone mTimeZone = mCalendar.getTimeZone();
String formattedDate = sdf.format(date);
String updated = String.format(getString(R.string.last_updated), formattedDate);
textUpdatedTime.setText(updated);
textUpdatedTime.setVisibility(View.VISIBLE);
textUpdatedTime.setTextColor(Color.RED);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ca = new ArrayList<>();
loadFromCache();
// view update observer
ViewUpdateObserver.getInstance().addObserver(new Observer() {
@Override
public void update(Observable observable, Object data) {
loadFromCache();
}
});
}
private void loadCase() {
RequestQueue rq = Volley.newRequestQueue(getActivity());
String uri = String.format(UriManager.getDealerCase(),
GlobalConstant.NEW);
final ProgressDialog loading = ProgressDialog.show(getContext(), "Refreshing...", "Please wait...", false, false);
CustomAuthObjectRequest caseRequest = new CustomAuthObjectRequest(getContext(), StringRequest.Method.GET, uri, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
caseList.clear();
Gson gson = new Gson();
DealerCase caseModel = gson.fromJson(response, DealerCase.class);
if(caseModel.getData().getRfqs() != null){
int b = caseModel.getData().getRfqs().size();
ArrayList<Rfq> ca = new ArrayList<>();
for(int i=0;i<b;i++){
// if(caseList.get(i).getIsSeen()){
// caseModel.getData().getRfqs().get(i).setIsSeen(true);
// }else {
// caseModel.getData().getRfqs().get(i).setIsSeen(false);
// }
caseModel.getData().getRfqs().get(i).setIsSeen(false);
ca.add(i,caseModel.getData().getRfqs().get(i));
// caseList.add(i,caseModel.getData().getRfqs().get(i));
}
caseAdapter.setData(ca);
caseAdapter.notifyDataSetChanged();
//Toast.makeText(getContext(), String.valueOf(b), Toast.LENGTH_SHORT).show();
}
loading.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
errorHandling(error);
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = super.getParams();
param.put(GlobalConstant.TAB,GlobalConstant.NEW);
return param;
}
};
rq.add(caseRequest);
}
public void requestNewCase(){
final ProgressDialog loading = ProgressDialog.show(getContext(), "Refreshing...", "Please wait...", false, false);
String uri = String.format(UriManager.getDealerCase(),
GlobalConstant.NEW);
ApiManager.getInstance().authUrlRequest(getActivity(), KeyConfig.CACHE_NEW, false, false, StringRequest.Method.GET, uri, new OnResponseListener() {
@Override
public void onResponse(String response, boolean fromCache) {
if(response != null && !fromCache && response.length() > 0) {
Gson gson = new Gson();
DealerCase caseModel = gson.fromJson(response, DealerCase.class);
String newList = CacheManager.getInstance().getCache().get("test_cache");
if(caseModel.getData().getRfqs() != null) {
int b = caseModel.getData().getRfqs().size();
//Toast.makeText(getContext(), String.valueOf(ca.size())+" - " + String.valueOf(b),Toast.LENGTH_LONG).show();
for (int i = 0; i < b; i++) {
for(int n=0 ; n< caseList.size(); n++){
if(caseList.get(n).getUid().equals(caseModel.getData().getRfqs().get(i).getUid())){
caseModel.getData().getRfqs().get(i).setIsSeen(true);
if(caseList.get(n).getIsSeen() == null){
caseModel.getData().getRfqs().get(i).setIsSeen(false);
}else {
if(caseList.get(n).getIsSeen()){
caseModel.getData().getRfqs().get(i).setIsSeen(true);
}else
caseModel.getData().getRfqs().get(i).setIsSeen(false);
}
//Toast.makeText(getContext(), String.valueOf(n) +" __ " + String.valueOf(ca.get(n).getIsSeen())+" - " + String.valueOf(caseModel.getData().getRfqs().get(i).getIsSeen()),Toast.LENGTH_LONG).show();
}
}
caseList.add(i,caseModel.getData().getRfqs().get(i));
}
caseAdapter.setData(caseList);
caseAdapter.notifyDataSetChanged();
}
CacheManager.getInstance().getCache().put(KeyConfig.CACHE_NEW, gson.toJson(caseModel));
Toast.makeText(getContext(),String.valueOf(caseModel.getData().getRfqs().get(0).getIsSeen()),Toast.LENGTH_LONG).show();
ViewUpdateObserver.getInstance().notifySubscribers();
}
loadFromCache();
loading.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
}
});
}
@Override
public void onClick(View v) {
}
@Override
public void onResume() {
super.onResume();
requestNewCase();
}
}