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() {
@Override
public void onEmptyAdapter() {
public void onEmptyAdapter(boolean shouldStopRefreshing) {
searchNoResultsView.setText(R.string.search_no_results);
searchNoResultsView.setVisibility(View.VISIBLE);
mDomainsView.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
if (shouldStopRefreshing) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
@Override
public void onNonEmptyAdapter() {
public void onNonEmptyAdapter(boolean shouldStopRefreshing) {
searchNoResultsView.setVisibility(View.GONE);
mDomainsView.setVisibility(View.VISIBLE);
mSwipeRefreshLayout.setRefreshing(false);
if (shouldStopRefreshing) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
@Override
@ -96,11 +100,20 @@ public class HomeFragment extends Fragment {
}
});
mDomainsView.setAdapter(mDomainAdapter);
mDomainAdapter.startLoad();
mSearchView = rootView.findViewById(R.id.searchView);
mSearchIconView = rootView.findViewById(R.id.search_mag_icon);
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() {
@Override
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);
}
});
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return rootView;
}
@Override

View file

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