Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce cognitive complexity. #1390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@Module
public class QuranDataModule {

private QuranDataModule(){}

@Provides
static PageProvider provideQuranPageProvider(Map<String, PageProvider> providers,
QuranSettings quranSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.quran.labs.androidquran.ui.util.TypefaceManager;

public class QuranFileConstants {

private QuranFileConstants() {}

// server urls
public static final int FONT_TYPE = TypefaceManager.TYPE_UTHMANI_HAFS;

Expand Down
125 changes: 75 additions & 50 deletions app/src/main/java/com/quran/labs/androidquran/QuranDataActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ private boolean canWriteSdcardAfterPermissions() {
quranFileUtils.makeQuranDirectory(this, quranScreenInfo.getWidthParam())) {
File f = new File(location, "" + System.currentTimeMillis());
if (f.createNewFile()) {
f.delete();
if (!f.delete()) {
// file delete failed; take appropriate action
}
return true;
}
}
Expand Down Expand Up @@ -360,61 +362,84 @@ public void onStorageNotAvailable() {
public void onPagesChecked(QuranDataPresenter.QuranDataStatus quranDataStatus) {
this.quranDataStatus = quranDataStatus;

getPagesIfNecessary(quranDataStatus);
}

private void getPagesIfNecessary(QuranDataPresenter.QuranDataStatus quranDataStatus) {
if (!quranDataStatus.havePages()) {
if (quranSettings.didDownloadPages()) {
// log if we downloaded pages once before
try {
onPagesLost();
} catch (Exception e) {
Crashlytics.logException(e);
}
// clear the "pages downloaded" flag
quranSettings.removeDidDownloadPages();
}
logDownloadedPagesIfNecessary();
} else {
createFilesInQuranDir(quranDataStatus);
}
}

String lastErrorItem = quranSettings.getLastDownloadItemWithError();
Timber.d("checkPages: need to download pages... lastError: %s", lastErrorItem);
if (PAGES_DOWNLOAD_KEY.equals(lastErrorItem)) {
int lastError = quranSettings.getLastDownloadErrorCode();
int errorId = ServiceIntentHelper
.getErrorResourceFromErrorCode(lastError, false);
showFatalErrorDialog(errorId);
} else if (quranSettings.shouldFetchPages()) {
downloadQuranImages(false);
} else {
promptForDownload();
}
private void createFilesInQuranDir(QuranDataPresenter.QuranDataStatus quranDataStatus) {
final String appLocation = quranSettings.getAppCustomLocation();
final String baseDirectory = quranFileUtils.getQuranBaseDirectory();
try {
// try to write a directory to distinguish between the entire Quran directory
// being removed versus just the images being somehow removed.

//noinspection ResultOfMethodCallIgnored
new File(baseDirectory, QURAN_DIRECTORY_MARKER_FILE).createNewFile();
//noinspection ResultOfMethodCallIgnored
new File(baseDirectory, QURAN_HIDDEN_DIRECTORY_MARKER_FILE).createNewFile();
makeQuranHiddenDirMarkerFileIfNecessary();

quranSettings.setDownloadedPages(System.currentTimeMillis(), appLocation,
quranDataStatus.getPortraitWidth() + "_" + quranDataStatus.getLandscapeWidth());
} catch (IOException ioe) {
Crashlytics.logException(ioe);
}

promptForDownloadIfNecessary(quranDataStatus);
}

private void promptForDownloadIfNecessary(QuranDataPresenter.QuranDataStatus quranDataStatus) {
final String patchParam = quranDataStatus.getPatchParam();
if (!TextUtils.isEmpty(patchParam)) {
Timber.d("checkPages: have pages, but need patch %s", patchParam);
promptForDownload();
} else {
final String appLocation = quranSettings.getAppCustomLocation();
final String baseDirectory = quranFileUtils.getQuranBaseDirectory();
try {
// try to write a directory to distinguish between the entire Quran directory
// being removed versus just the images being somehow removed.

//noinspection ResultOfMethodCallIgnored
new File(baseDirectory, QURAN_DIRECTORY_MARKER_FILE).createNewFile();
//noinspection ResultOfMethodCallIgnored
new File(baseDirectory, QURAN_HIDDEN_DIRECTORY_MARKER_FILE).createNewFile();

// try writing a file to the app's internal no_backup directory
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//noinspection ResultOfMethodCallIgnored
new File(getNoBackupFilesDir(), QURAN_HIDDEN_DIRECTORY_MARKER_FILE).createNewFile();
}
runListView();
}
}

quranSettings.setDownloadedPages(System.currentTimeMillis(), appLocation,
quranDataStatus.getPortraitWidth() + "_" + quranDataStatus.getLandscapeWidth());
} catch (IOException ioe) {
Crashlytics.logException(ioe);
}
private void makeQuranHiddenDirMarkerFileIfNecessary() throws IOException {
// try writing a file to the app's internal no_backup directory
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//noinspection ResultOfMethodCallIgnored
new File(getNoBackupFilesDir(), QURAN_HIDDEN_DIRECTORY_MARKER_FILE).createNewFile();
}
}

final String patchParam = quranDataStatus.getPatchParam();
if (!TextUtils.isEmpty(patchParam)) {
Timber.d("checkPages: have pages, but need patch %s", patchParam);
promptForDownload();
} else {
runListView();
private void logDownloadedPagesIfNecessary() {
if (quranSettings.didDownloadPages()) {
// log if we downloaded pages once before
try {
onPagesLost();
} catch (Exception e) {
Crashlytics.logException(e);
}
// clear the "pages downloaded" flag
quranSettings.removeDidDownloadPages();
}

displayErrorIfNecessary();
}

private void displayErrorIfNecessary() {
String lastErrorItem = quranSettings.getLastDownloadItemWithError();
Timber.d("checkPages: need to download pages... lastError: %s", lastErrorItem);
if (PAGES_DOWNLOAD_KEY.equals(lastErrorItem)) {
int lastError = quranSettings.getLastDownloadErrorCode();
int errorId = ServiceIntentHelper
.getErrorResourceFromErrorCode(lastError, false);
showFatalErrorDialog(errorId);
} else if (quranSettings.shouldFetchPages()) {
downloadQuranImages(false);
} else {
promptForDownload();
}
}

Expand Down