Merge pull request #328 from kiwix/fix-library-report

improvments to library UI status reporting
This commit is contained in:
Isaac Hutt 2018-02-02 00:59:42 +00:00 committed by GitHub
commit ada23c5e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 71 deletions

View File

@ -30,7 +30,6 @@
android:label="@string/app_name"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>

View File

@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
@ -26,6 +27,7 @@ import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.Locale; import java.util.Locale;
import org.kiwix.kiwixmobile.KiwixApplication; import org.kiwix.kiwixmobile.KiwixApplication;
import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.KiwixMobileActivity;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
@ -64,6 +66,7 @@ public class DownloadFragment extends Fragment {
zimManageActivity = (ZimManageActivity) super.getActivity(); zimManageActivity = (ZimManageActivity) super.getActivity();
listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list); listView = (ListView) relLayout.findViewById(R.id.zim_downloader_list);
downloadAdapter = new DownloadAdapter(mDownloads); downloadAdapter = new DownloadAdapter(mDownloads);
downloadAdapter.registerDataSetObserver(this);
listView.setAdapter(downloadAdapter); listView.setAdapter(downloadAdapter);
mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity); mainLayout = (CoordinatorLayout) faActivity.findViewById(R.id.zim_manager_main_activity);
return relLayout; return relLayout;
@ -88,6 +91,12 @@ public class DownloadFragment extends Fragment {
} }
} }
@Override
public void onDestroy() {
super.onDestroy();
downloadAdapter.unRegisterDataSetObserver();
}
public static void showNoWiFiWarning(Context context, Runnable yesAction) { public static void showNoWiFiWarning(Context context, Runnable yesAction) {
new AlertDialog.Builder(context) new AlertDialog.Builder(context)
.setTitle(R.string.wifi_only_title) .setTitle(R.string.wifi_only_title)
@ -123,13 +132,14 @@ public class DownloadFragment extends Fragment {
KiwixApplication.getInstance().getResources().getString(R.string.time_left)); KiwixApplication.getInstance().getResources().getString(R.string.time_left));
return String.format(Locale.getDefault(), "%d %s %s", seconds, return String.format(Locale.getDefault(), "%d %s %s", seconds,
KiwixApplication.getInstance().getResources().getString(R.string.time_second), KiwixApplication.getInstance().getResources().getString(R.string.time_second),
KiwixApplication.getInstance().getResources().getString(R.string.time_left)); KiwixApplication.getInstance().getResources().getString(R.string.time_left));
} }
public class DownloadAdapter extends BaseAdapter { public class DownloadAdapter extends BaseAdapter {
private LinkedHashMap<Integer, LibraryNetworkEntity.Book> mData = new LinkedHashMap<>(); private LinkedHashMap<Integer, LibraryNetworkEntity.Book> mData = new LinkedHashMap<>();
private Integer[] mKeys; private Integer[] mKeys;
private DataSetObserver dataSetObserver;
public DownloadAdapter(LinkedHashMap<Integer, LibraryNetworkEntity.Book> data) { public DownloadAdapter(LinkedHashMap<Integer, LibraryNetworkEntity.Book> data) {
mData = data; mData = data;
@ -158,10 +168,10 @@ public class DownloadFragment extends Fragment {
int position = Arrays.asList(mKeys).indexOf(notificationID); int position = Arrays.asList(mKeys).indexOf(notificationID);
ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition());
if (viewGroup == null) { if (viewGroup == null) {
mDownloads.remove(mKeys[position]); mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]); mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged(); downloadAdapter.notifyDataSetChanged();
updateNoDownloads(); updateNoDownloads();
} }
ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause); ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause);
pause.setEnabled(false); pause.setEnabled(false);
@ -195,13 +205,13 @@ public class DownloadFragment extends Fragment {
} }
private void setPlayState(ImageView pauseButton, int position, int newPlayState) { private void setPlayState(ImageView pauseButton, int position, int newPlayState) {
if(newPlayState == DownloadService.PLAY) { //Playing if (newPlayState == DownloadService.PLAY) { //Playing
if (LibraryFragment.mService.playDownload(mKeys[position])) if (LibraryFragment.mService.playDownload(mKeys[position]))
pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp)); pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_24dp));
} else { //Pausing } else { //Pausing
LibraryFragment.mService.pauseDownload(mKeys[position]); LibraryFragment.mService.pauseDownload(mKeys[position]);
pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp)); pauseButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_24dp));
} }
} }
@Override @Override
@ -243,7 +253,9 @@ public class DownloadFragment extends Fragment {
int newPlayPauseState = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY ? DownloadService.PAUSE : DownloadService.PLAY; int newPlayPauseState = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY ? DownloadService.PAUSE : DownloadService.PLAY;
if (newPlayPauseState == DownloadService.PLAY && KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) { if (newPlayPauseState == DownloadService.PLAY && KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) {
showNoWiFiWarning(getContext(), () -> {setPlayState(pause, position, newPlayPauseState);}); showNoWiFiWarning(getContext(), () -> {
setPlayState(pause, position, newPlayPauseState);
});
return; return;
} }
@ -258,30 +270,56 @@ public class DownloadFragment extends Fragment {
hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY; hasArtificiallyPaused = LibraryFragment.mService.downloadStatus.get(mKeys[position]) == DownloadService.PLAY;
setPlayState(pause, position, DownloadService.PAUSE); setPlayState(pause, position, DownloadService.PAUSE);
new AlertDialog.Builder(faActivity, dialogStyle()) new AlertDialog.Builder(faActivity, dialogStyle())
.setTitle(R.string.confirm_stop_download_title) .setTitle(R.string.confirm_stop_download_title)
.setMessage(R.string.confirm_stop_download_msg) .setMessage(R.string.confirm_stop_download_msg)
.setPositiveButton(R.string.yes, (dialog, i) -> { .setPositiveButton(R.string.yes, (dialog, i) -> {
LibraryFragment.mService.stopDownload(mKeys[position]); LibraryFragment.mService.stopDownload(mKeys[position]);
mDownloads.remove(mKeys[position]); mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]); mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged(); downloadAdapter.notifyDataSetChanged();
updateNoDownloads(); updateNoDownloads();
if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) { if (zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter != null) {
zimManageActivity .mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery()); zimManageActivity.mSectionsPagerAdapter.libraryFragment.libraryAdapter.getFilter().filter(((ZimManageActivity) getActivity()).searchView.getQuery());
} }
}) })
.setNegativeButton(R.string.no, (dialog, i) -> { .setNegativeButton(R.string.no, (dialog, i) -> {
if(hasArtificiallyPaused) { if (hasArtificiallyPaused) {
hasArtificiallyPaused = false; hasArtificiallyPaused = false;
setPlayState(pause, position, DownloadService.PLAY); setPlayState(pause, position, DownloadService.PLAY);
} }
}) })
.show(); .show();
}); });
// Return the completed view to render on screen // Return the completed view to render on screen
return convertView; return convertView;
} }
public void registerDataSetObserver(DownloadFragment downloadFragment) {
if (dataSetObserver == null) {
dataSetObserver = new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
downloadFragment.updateNoDownloads();
}
@Override
public void onInvalidated() {
super.onInvalidated();
downloadFragment.updateNoDownloads();
}
};
registerDataSetObserver(dataSetObserver);
}
}
public void unRegisterDataSetObserver() {
if (dataSetObserver != null) {
unregisterDataSetObserver(dataSetObserver);
}
}
} }
public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) { public void addDownload(int position, LibraryNetworkEntity.Book book, String fileName) {

View File

@ -58,6 +58,7 @@ import butterknife.ButterKnife;
import eu.mhutti1.utils.storage.StorageDevice; import eu.mhutti1.utils.storage.StorageDevice;
import eu.mhutti1.utils.storage.support.StorageSelectDialog; import eu.mhutti1.utils.storage.support.StorageSelectDialog;
import static android.view.View.GONE;
import static org.kiwix.kiwixmobile.downloader.DownloadService.KIWIX_ROOT; import static org.kiwix.kiwixmobile.downloader.DownloadService.KIWIX_ROOT;
import static org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book; import static org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_BOOK; import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_BOOK;
@ -81,7 +82,8 @@ public class LibraryFragment extends Fragment
public LinearLayout llLayout; public LinearLayout llLayout;
public SwipeRefreshLayout swipeRefreshLayout; @BindView(R.id.library_swiperefresh)
SwipeRefreshLayout swipeRefreshLayout;
private ArrayList<Book> books = new ArrayList<>(); private ArrayList<Book> books = new ArrayList<>();
@ -93,7 +95,8 @@ public class LibraryFragment extends Fragment
private DownloadServiceConnection mConnection = new DownloadServiceConnection(); private DownloadServiceConnection mConnection = new DownloadServiceConnection();
@Inject ConnectivityManager conMan; @Inject
ConnectivityManager conMan;
private ZimManageActivity faActivity; private ZimManageActivity faActivity;
@ -114,31 +117,20 @@ public class LibraryFragment extends Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
setupDagger(); setupDagger();
TestingUtils.bindResource(LibraryFragment.class); TestingUtils.bindResource(LibraryFragment.class);
faActivity = (ZimManageActivity) super.getActivity();
// Replace LinearLayout by the type of the root element of the layout you're trying to load
llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false); llLayout = (LinearLayout) inflater.inflate(R.layout.activity_library, container, false);
ButterKnife.bind(this, llLayout); ButterKnife.bind(this, llLayout);
// SwipeRefreshLayout for the list view
swipeRefreshLayout = (SwipeRefreshLayout) llLayout.findViewById(R.id.library_swiperefresh);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFragment();
}
});
displayScanningContent();
libraryAdapter = new LibraryAdapter(super.getContext());
libraryList.setAdapter(libraryAdapter);
presenter.attachView(this); presenter.attachView(this);
faActivity = (ZimManageActivity) super.getActivity();
swipeRefreshLayout.setOnRefreshListener(() -> refreshFragment());
libraryAdapter = new LibraryAdapter(super.getContext());
libraryList.setAdapter(libraryAdapter);
DownloadService.setDownloadFragment(faActivity.mSectionsPagerAdapter.getDownloadFragment()); DownloadService.setDownloadFragment(faActivity.mSectionsPagerAdapter.getDownloadFragment());
NetworkInfo network = conMan.getActiveNetworkInfo(); NetworkInfo network = conMan.getActiveNetworkInfo();
if (network == null || !network.isConnected()) { if (network == null || !network.isConnected()) {
displayNoNetworkConnection(); displayNoNetworkConnection();
@ -150,11 +142,7 @@ public class LibraryFragment extends Fragment
presenter.loadRunningDownloadsFromDb(getActivity()); presenter.loadRunningDownloadsFromDb(getActivity());
// The FragmentActivity doesn't contain the layout directly so we must use our instance of LinearLayout : return llLayout;
// llLayout.findViewById(R.id.someGuiElement);
// Instead of :
// findViewById(R.id.someGuiElement);
return llLayout; // We must return the loaded Layout
} }
@ -172,7 +160,7 @@ public class LibraryFragment extends Fragment
faActivity.searchView.getQuery(), faActivity.searchView.getQuery(),
i -> stopScanningContent()); i -> stopScanningContent());
} else { } else {
libraryAdapter.getFilter().filter("", i -> stopScanningContent() ); libraryAdapter.getFilter().filter("", i -> stopScanningContent());
} }
libraryAdapter.notifyDataSetChanged(); libraryAdapter.notifyDataSetChanged();
libraryList.setOnItemClickListener(this); libraryList.setOnItemClickListener(this);
@ -187,9 +175,19 @@ public class LibraryFragment extends Fragment
networkText.setText(R.string.no_network_connection); networkText.setText(R.string.no_network_connection);
networkText.setVisibility(View.VISIBLE); networkText.setVisibility(View.VISIBLE);
permissionButton.setVisibility(View.GONE); permissionButton.setVisibility(GONE);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.setEnabled(false); swipeRefreshLayout.setEnabled(false);
libraryList.setVisibility(View.INVISIBLE);
TestingUtils.unbindResource(LibraryFragment.class);
}
@Override
public void displayNoItemsFound() {
networkText.setText(R.string.no_items_msg);
networkText.setVisibility(View.VISIBLE);
permissionButton.setVisibility(GONE);
swipeRefreshLayout.setRefreshing(false);
TestingUtils.unbindResource(LibraryFragment.class); TestingUtils.unbindResource(LibraryFragment.class);
} }
@ -210,8 +208,8 @@ public class LibraryFragment extends Fragment
@Override @Override
public void displayScanningContent() { public void displayScanningContent() {
if (!swipeRefreshLayout.isRefreshing()) { if (!swipeRefreshLayout.isRefreshing()) {
networkText.setVisibility(View.GONE); networkText.setVisibility(GONE);
permissionButton.setVisibility(View.GONE); permissionButton.setVisibility(GONE);
swipeRefreshLayout.setEnabled(true); swipeRefreshLayout.setEnabled(true);
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
TestingUtils.bindResource(LibraryFragment.class); TestingUtils.bindResource(LibraryFragment.class);
@ -221,8 +219,8 @@ public class LibraryFragment extends Fragment
@Override @Override
public void stopScanningContent() { public void stopScanningContent() {
networkText.setVisibility(View.GONE); networkText.setVisibility(GONE);
permissionButton.setVisibility(View.GONE); permissionButton.setVisibility(GONE);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
TestingUtils.unbindResource(LibraryFragment.class); TestingUtils.unbindResource(LibraryFragment.class);
} }
@ -234,14 +232,13 @@ public class LibraryFragment extends Fragment
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
return; return;
} }
networkBroadcastReceiver.onReceive(super.getActivity(), null); networkBroadcastReceiver.onReceive(super.getActivity(), null);
} }
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
if (mBound) { if (mBound && super.getActivity() != null) {
super.getActivity().unbindService(mConnection.downloadServiceInterface); super.getActivity().unbindService(mConnection.downloadServiceInterface);
mBound = false; mBound = false;
} }
@ -289,7 +286,9 @@ public class LibraryFragment extends Fragment
} }
if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) { if (KiwixMobileActivity.wifiOnly && !NetworkUtils.isWiFi(getContext())) {
DownloadFragment.showNoWiFiWarning(getContext(), () -> {downloadFile((Book) parent.getAdapter().getItem(position));}); DownloadFragment.showNoWiFiWarning(getContext(), () -> {
downloadFile((Book) parent.getAdapter().getItem(position));
});
} else { } else {
downloadFile((Book) parent.getAdapter().getItem(position)); downloadFile((Book) parent.getAdapter().getItem(position));
} }
@ -337,7 +336,6 @@ public class LibraryFragment extends Fragment
editor.apply(); editor.apply();
} }
public class DownloadServiceConnection { public class DownloadServiceConnection {
public DownloadServiceInterface downloadServiceInterface; public DownloadServiceInterface downloadServiceInterface;
@ -356,7 +354,8 @@ public class LibraryFragment extends Fragment
} }
@Override @Override
public void onServiceDisconnected(ComponentName arg0) { } public void onServiceDisconnected(ComponentName arg0) {
}
} }
} }
@ -365,11 +364,17 @@ public class LibraryFragment extends Fragment
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
NetworkInfo network = conMan.getActiveNetworkInfo(); NetworkInfo network = conMan.getActiveNetworkInfo();
if (network == null || !network.isConnected()) {
displayNoNetworkConnection();
}
if ((books == null || books.isEmpty()) && network != null && network.isConnected()) { if ((books == null || books.isEmpty()) && network != null && network.isConnected()) {
presenter.loadBooks(); presenter.loadBooks();
permissionButton.setVisibility(View.GONE); permissionButton.setVisibility(GONE);
networkText.setVisibility(View.GONE); networkText.setVisibility(GONE);
libraryList.setVisibility(View.VISIBLE);
} }
} }
} }
} }

View File

@ -36,7 +36,7 @@ public class LibraryPresenter extends BasePresenter<LibraryViewCallback> {
}, error -> { }, error -> {
String msg = error.getLocalizedMessage(); String msg = error.getLocalizedMessage();
Log.w("kiwixLibrary", "Error loading books:" + (msg != null ? msg : "(null)")); Log.w("kiwixLibrary", "Error loading books:" + (msg != null ? msg : "(null)"));
getMvpView().displayNoNetworkConnection(); getMvpView().displayNoItemsFound();
}); });
} }

View File

@ -16,6 +16,8 @@ public interface LibraryViewCallback extends ViewCallback {
void displayNoNetworkConnection(); void displayNoNetworkConnection();
void displayNoItemsFound();
void displayNoItemsAvailable(); void displayNoItemsAvailable();
void displayScanningContent(); void displayScanningContent();

View File

@ -190,4 +190,5 @@
<string name="do_not_ask_anymore">Do not ask anymore</string> <string name="do_not_ask_anymore">Do not ask anymore</string>
<string name="your_languages">Selected languages:</string> <string name="your_languages">Selected languages:</string>
<string name="other_languages">Other languages:</string> <string name="other_languages">Other languages:</string>
<string name="no_items_msg">No items available</string>
</resources> </resources>