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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

View File

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

View File

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

View File

@ -36,7 +36,7 @@ public class LibraryPresenter extends BasePresenter<LibraryViewCallback> {
}, error -> {
String msg = error.getLocalizedMessage();
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 displayNoItemsFound();
void displayNoItemsAvailable();
void displayScanningContent();

View File

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