mirror of
				https://github.com/MinimalBible/MinimalBible
				synced 2025-11-03 18:10:27 -05:00 
			
		
		
		
	Refactor a bit to make testing easier
This commit is contained in:
		@ -2,7 +2,6 @@ package org.bspeice.minimalbible.activity.downloader;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import android.app.Activity;
 | 
					import android.app.Activity;
 | 
				
			||||||
import android.app.AlertDialog;
 | 
					import android.app.AlertDialog;
 | 
				
			||||||
import android.app.ProgressDialog;
 | 
					 | 
				
			||||||
import android.content.DialogInterface;
 | 
					import android.content.DialogInterface;
 | 
				
			||||||
import android.os.Bundle;
 | 
					import android.os.Bundle;
 | 
				
			||||||
import android.view.LayoutInflater;
 | 
					import android.view.LayoutInflater;
 | 
				
			||||||
@ -11,7 +10,6 @@ import android.view.ViewGroup;
 | 
				
			|||||||
import android.widget.ArrayAdapter;
 | 
					import android.widget.ArrayAdapter;
 | 
				
			||||||
import android.widget.ListView;
 | 
					import android.widget.ListView;
 | 
				
			||||||
import android.widget.Spinner;
 | 
					import android.widget.Spinner;
 | 
				
			||||||
import android.widget.SpinnerAdapter;
 | 
					 | 
				
			||||||
import android.widget.Toast;
 | 
					import android.widget.Toast;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.bspeice.minimalbible.Injector;
 | 
					import org.bspeice.minimalbible.Injector;
 | 
				
			||||||
@ -45,9 +43,9 @@ public class BookListFragment extends BaseFragment {
 | 
				
			|||||||
    protected static final String ARG_BOOK_CATEGORY = "book_category";
 | 
					    protected static final String ARG_BOOK_CATEGORY = "book_category";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Inject
 | 
					    @Inject
 | 
				
			||||||
    protected DownloadPrefs downloadPrefs;
 | 
					    DownloadPrefs downloadPrefs;
 | 
				
			||||||
    protected ProgressDialog refreshDialog;
 | 
					    @Inject
 | 
				
			||||||
    @Inject RefreshManager refreshManager;
 | 
					    RefreshManager refreshManager;
 | 
				
			||||||
    @Inject
 | 
					    @Inject
 | 
				
			||||||
    List<Language> availableLanguages;
 | 
					    List<Language> availableLanguages;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -56,7 +54,7 @@ public class BookListFragment extends BaseFragment {
 | 
				
			|||||||
    @InjectView(R.id.spn_available_languages)
 | 
					    @InjectView(R.id.spn_available_languages)
 | 
				
			||||||
    Spinner languagesSpinner;
 | 
					    Spinner languagesSpinner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private LayoutInflater inflater;
 | 
					    LayoutInflater inflater;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Returns a new instance of this fragment for the given section number.
 | 
					     * Returns a new instance of this fragment for the given section number.
 | 
				
			||||||
@ -99,7 +97,7 @@ public class BookListFragment extends BaseFragment {
 | 
				
			|||||||
     * Trigger the functionality to display a list of modules. Prompts user if downloading
 | 
					     * Trigger the functionality to display a list of modules. Prompts user if downloading
 | 
				
			||||||
     * from the internet is allowable.
 | 
					     * from the internet is allowable.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 	protected void displayModules() {
 | 
					    void displayModules() {
 | 
				
			||||||
        boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog();
 | 
					        boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog();
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		if (!dialogDisplayed) {
 | 
							if (!dialogDisplayed) {
 | 
				
			||||||
@ -111,45 +109,22 @@ public class BookListFragment extends BaseFragment {
 | 
				
			|||||||
					.setNegativeButton("No", dialogListener)
 | 
										.setNegativeButton("No", dialogListener)
 | 
				
			||||||
					.setCancelable(false).show();
 | 
										.setCancelable(false).show();
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			refreshModules();
 | 
					            displayLanguageSpinner();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    void displayLanguageSpinner() {
 | 
				
			||||||
     * Do the work of refreshing modules (download manager handles using cached copy vs. actual
 | 
					 | 
				
			||||||
     * refresh), and then displaying them when ready.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
	private void refreshModules() {
 | 
					 | 
				
			||||||
        // Check if the downloadManager has already refreshed everything
 | 
					 | 
				
			||||||
        if (!refreshManager.getRefreshComplete().get()) {
 | 
					 | 
				
			||||||
            // downloadManager is in progress of refreshing
 | 
					 | 
				
			||||||
            refreshDialog = new ProgressDialog(getActivity());
 | 
					 | 
				
			||||||
            refreshDialog.setMessage("Refreshing available modules...");
 | 
					 | 
				
			||||||
            refreshDialog.setCancelable(false);
 | 
					 | 
				
			||||||
            refreshDialog.show();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        languagesSpinner.setAdapter(getLocaleSpinner());
 | 
					 | 
				
			||||||
        if (BookListFragment.this.getActivity() != null) {
 | 
					 | 
				
			||||||
            // On a screen rotate, getActivity() will be null. But, the activity
 | 
					 | 
				
			||||||
            // will already have been set up correctly, so we don't need to worry
 | 
					 | 
				
			||||||
            // about it.
 | 
					 | 
				
			||||||
            // If not null, we need to set it up now.
 | 
					 | 
				
			||||||
            setInsetsSpinner(BookListFragment.this.getActivity(), languagesSpinner);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if (refreshDialog != null) {
 | 
					 | 
				
			||||||
            refreshDialog.cancel();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @SuppressWarnings("ConstantConditions")
 | 
					 | 
				
			||||||
        // getAvailableLanguagesList() will not return null
 | 
					 | 
				
			||||||
    SpinnerAdapter getLocaleSpinner() {
 | 
					 | 
				
			||||||
        ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this.getActivity(),
 | 
					        ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this.getActivity(),
 | 
				
			||||||
                android.R.layout.simple_spinner_item,
 | 
					                android.R.layout.simple_spinner_item,
 | 
				
			||||||
                availableLanguages.toArray());
 | 
					                availableLanguages.toArray());
 | 
				
			||||||
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 | 
					        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 | 
				
			||||||
        return adapter;
 | 
					        languagesSpinner.setAdapter(adapter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (BookListFragment.this.getActivity() != null) {
 | 
				
			||||||
 | 
					            // On a screen rotate, getActivity() will be null, but the activity
 | 
				
			||||||
 | 
					            // will already have been set up. If not null, we need to set it up now.
 | 
				
			||||||
 | 
					            setInsetsSpinner(BookListFragment.this.getActivity(), languagesSpinner);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @SuppressWarnings("unused")
 | 
					    @SuppressWarnings("unused")
 | 
				
			||||||
@ -207,23 +182,27 @@ public class BookListFragment extends BaseFragment {
 | 
				
			|||||||
                    downloadPrefs.hasEnabledDownload(true);
 | 
					                    downloadPrefs.hasEnabledDownload(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    // And warn them that it has been enabled in the future.
 | 
					                    // And warn them that it has been enabled in the future.
 | 
				
			||||||
                    Toast.makeText(getActivity(),
 | 
					                    showToast("Downloading now enabled. Disable in settings");
 | 
				
			||||||
                            "Downloading now enabled. Disable in settings.",
 | 
					                    displayModules();
 | 
				
			||||||
                            Toast.LENGTH_SHORT).show();
 | 
					 | 
				
			||||||
                    refreshModules();
 | 
					 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // case DialogInterface.BUTTON_NEGATIVE:
 | 
					                // case DialogInterface.BUTTON_NEGATIVE:
 | 
				
			||||||
                default:
 | 
					                default:
 | 
				
			||||||
                    // Clicked to not download - Permanently disable downloading
 | 
					                    // Clicked to not download - Permanently disable downloading
 | 
				
			||||||
                    downloadPrefs.hasEnabledDownload(false);
 | 
					                    downloadPrefs.hasEnabledDownload(false);
 | 
				
			||||||
                    Toast.makeText(getActivity(),
 | 
					                    showToast("Disabling downloading. Re-enable it in settings.");
 | 
				
			||||||
                            "Disabling downloading. Re-enable it in settings.",
 | 
					                    shutdown();
 | 
				
			||||||
                            Toast.LENGTH_SHORT).show();
 | 
					 | 
				
			||||||
                    refreshModules();
 | 
					 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void showToast(String text) {
 | 
				
			||||||
 | 
					            Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void shutdown() {
 | 
				
			||||||
 | 
					            getActivity().finish();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -48,9 +48,9 @@ class RefreshManager(val installers: Collection<Installer>,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    val fifteenDaysAgo = Calendar.getInstance().getTime().getTime() - 1296000
 | 
					    val fifteenDaysAgo = Calendar.getInstance().getTime().getTime() - 1296000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun doReload(enabledDownload: Boolean, lastUpdated: Long,
 | 
					    fun doReload(downloadEnabled: Boolean, lastUpdated: Long,
 | 
				
			||||||
                 networkState: Int? = ConnectivityManager.TYPE_DUMMY): Boolean =
 | 
					                 networkState: Int? = ConnectivityManager.TYPE_DUMMY): Boolean =
 | 
				
			||||||
            if (!enabledDownload || networkState != ConnectivityManager.TYPE_WIFI)
 | 
					            if (!downloadEnabled || networkState != ConnectivityManager.TYPE_WIFI)
 | 
				
			||||||
                false
 | 
					                false
 | 
				
			||||||
            else if (lastUpdated < fifteenDaysAgo)
 | 
					            else if (lastUpdated < fifteenDaysAgo)
 | 
				
			||||||
                true
 | 
					                true
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user