Android - Domains List - cache in memory only on this instance (deleted if app restarts) allows for fast list display. Showing again also asks for update so in some seconds the list gets updated in case of changes

This commit is contained in:
Cristian Luis Duarte 2018-08-21 16:35:47 -03:00
parent c8addd0581
commit af5c7a7a67
2 changed files with 47 additions and 16 deletions

View file

@ -76,18 +76,22 @@ public class HomeFragment extends Fragment {
}); });
mDomainAdapter.setListener(new DomainAdapter.AdapterListener() { mDomainAdapter.setListener(new DomainAdapter.AdapterListener() {
@Override @Override
public void onEmptyAdapter() { public void onEmptyAdapter(boolean shouldStopRefreshing) {
searchNoResultsView.setText(R.string.search_no_results); searchNoResultsView.setText(R.string.search_no_results);
searchNoResultsView.setVisibility(View.VISIBLE); searchNoResultsView.setVisibility(View.VISIBLE);
mDomainsView.setVisibility(View.GONE); mDomainsView.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false); if (shouldStopRefreshing) {
mSwipeRefreshLayout.setRefreshing(false);
}
} }
@Override @Override
public void onNonEmptyAdapter() { public void onNonEmptyAdapter(boolean shouldStopRefreshing) {
searchNoResultsView.setVisibility(View.GONE); searchNoResultsView.setVisibility(View.GONE);
mDomainsView.setVisibility(View.VISIBLE); mDomainsView.setVisibility(View.VISIBLE);
mSwipeRefreshLayout.setRefreshing(false); if (shouldStopRefreshing) {
mSwipeRefreshLayout.setRefreshing(false);
}
} }
@Override @Override
@ -96,11 +100,20 @@ public class HomeFragment extends Fragment {
} }
}); });
mDomainsView.setAdapter(mDomainAdapter); mDomainsView.setAdapter(mDomainAdapter);
mDomainAdapter.startLoad();
mSearchView = rootView.findViewById(R.id.searchView); mSearchView = rootView.findViewById(R.id.searchView);
mSearchIconView = rootView.findViewById(R.id.search_mag_icon); mSearchIconView = rootView.findViewById(R.id.search_mag_icon);
mClearSearch = rootView.findViewById(R.id.search_clear); mClearSearch = rootView.findViewById(R.id.search_clear);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return rootView;
}
@Override
public void onStart() {
super.onStart();
mSearchView.addTextChangedListener(new TextWatcher() { mSearchView.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@ -142,10 +155,6 @@ public class HomeFragment extends Fragment {
mDomainAdapter.loadDomains(mSearchView.getText().toString(), true); mDomainAdapter.loadDomains(mSearchView.getText().toString(), true);
} }
}); });
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return rootView;
} }
@Override @Override

View file

@ -12,6 +12,7 @@ import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.highfidelity.hifiinterface.R; import io.highfidelity.hifiinterface.R;
@ -36,19 +37,39 @@ public class DomainAdapter extends RecyclerView.Adapter<DomainAdapter.ViewHolder
// references to our domains // references to our domains
private Domain[] mDomains = {}; private Domain[] mDomains = {};
private static Domain[] DOMAINS_TMP_CACHE = {};
public DomainAdapter(Context c, String protocol, String lastLocation) { public DomainAdapter(Context c, String protocol, String lastLocation) {
mContext = c; mContext = c;
this.mInflater = LayoutInflater.from(mContext); this.mInflater = LayoutInflater.from(mContext);
mProtocol = protocol; mProtocol = protocol;
mLastLocation = lastLocation; mLastLocation = lastLocation;
domainProvider = new UserStoryDomainProvider(mProtocol); domainProvider = new UserStoryDomainProvider(mProtocol);
loadDomains("", true);
} }
public void setListener(AdapterListener adapterListener) { public void setListener(AdapterListener adapterListener) {
mAdapterListener = adapterListener; mAdapterListener = adapterListener;
} }
public void startLoad() {
useTmpCachedDomains();
loadDomains("", true);
}
private void useTmpCachedDomains() {
if (DOMAINS_TMP_CACHE != null && DOMAINS_TMP_CACHE.length > 0) {
mDomains = Arrays.copyOf(DOMAINS_TMP_CACHE, DOMAINS_TMP_CACHE.length);
notifyDataSetChanged();
if (mAdapterListener != null) {
if (mDomains.length == 0) {
mAdapterListener.onEmptyAdapter(false);
} else {
mAdapterListener.onNonEmptyAdapter(false);
}
}
}
}
public void loadDomains(String filterText, boolean forceRefresh) { public void loadDomains(String filterText, boolean forceRefresh) {
domainProvider.retrieve(filterText, new DomainProvider.DomainCallback() { domainProvider.retrieve(filterText, new DomainProvider.DomainCallback() {
@Override @Override
@ -60,13 +81,16 @@ public class DomainAdapter extends RecyclerView.Adapter<DomainAdapter.ViewHolder
overrideDefaultThumbnails(domain); overrideDefaultThumbnails(domain);
mDomains = new Domain[domain.size()]; mDomains = new Domain[domain.size()];
mDomains = domain.toArray(mDomains); domain.toArray(mDomains);
if (filterText.isEmpty()) {
DOMAINS_TMP_CACHE = Arrays.copyOf(mDomains, mDomains.length);
}
notifyDataSetChanged(); notifyDataSetChanged();
if (mAdapterListener != null) { if (mAdapterListener != null) {
if (mDomains.length == 0) { if (mDomains.length == 0) {
mAdapterListener.onEmptyAdapter(); mAdapterListener.onEmptyAdapter(true);
} else { } else {
mAdapterListener.onNonEmptyAdapter(); mAdapterListener.onNonEmptyAdapter(true);
} }
} }
} }
@ -112,8 +136,6 @@ public class DomainAdapter extends RecyclerView.Adapter<DomainAdapter.ViewHolder
@Override @Override
public void onBindViewHolder(ViewHolder holder, int position) { public void onBindViewHolder(ViewHolder holder, int position) {
// TODO
//holder.thumbnail.setImageResource(mDomains[position].thumbnail);
Domain domain = mDomains[position]; Domain domain = mDomains[position];
holder.mDomainName.setText(domain.name); holder.mDomainName.setText(domain.name);
Uri uri = Uri.parse(domain.thumbnail); Uri uri = Uri.parse(domain.thumbnail);
@ -164,8 +186,8 @@ public class DomainAdapter extends RecyclerView.Adapter<DomainAdapter.ViewHolder
} }
public interface AdapterListener { public interface AdapterListener {
void onEmptyAdapter(); void onEmptyAdapter(boolean shouldStopRefreshing);
void onNonEmptyAdapter(); void onNonEmptyAdapter(boolean shouldStopRefreshing);
void onError(Exception e, String message); void onError(Exception e, String message);
} }
} }