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

Fixes NullPointerExceptions due to callbacks in MainActivity #1039

Merged
merged 2 commits into from Mar 10, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 51 additions & 36 deletions app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java
Expand Up @@ -532,15 +532,20 @@ void goForward() {
}

private void updateBottomToolbarArrowsAlpha() {
if (getCurrentWebView().canGoForward()) {
bottomToolbarArrowForward.setAlpha(1f);
} else {
bottomToolbarArrowForward.setAlpha(0.6f);
if (checkNull(bottomToolbarArrowBack)) {
if (getCurrentWebView().canGoForward()) {
bottomToolbarArrowForward.setAlpha(1f);
} else {
bottomToolbarArrowForward.setAlpha(0.6f);
}
}
if (getCurrentWebView().canGoBack()) {
bottomToolbarArrowBack.setAlpha(1f);
} else {
bottomToolbarArrowBack.setAlpha(0.6f);

if (checkNull(bottomToolbarArrowForward)) {
if (getCurrentWebView().canGoBack()) {
bottomToolbarArrowBack.setAlpha(1f);
} else {
bottomToolbarArrowBack.setAlpha(0.6f);
}
}
}

Expand Down Expand Up @@ -1284,20 +1289,22 @@ public void onResume() {
}

private void updateBottomToolbarVisibility() {
if (sharedPreferenceUtil.getPrefBottomToolbar() && !HOME_URL.equals(
getCurrentWebView().getUrl())
&& tabSwitcherRoot.getVisibility() != View.VISIBLE) {
bottomToolbar.setVisibility(View.VISIBLE);
if (getCurrentWebView() instanceof ToolbarStaticKiwixWebView
&& sharedPreferenceUtil.getPrefBottomToolbar()) {
contentFrame.setPadding(0, 0, 0,
(int) getResources().getDimension(R.dimen.bottom_toolbar_height));
if (checkNull(bottomToolbar)) {
if (sharedPreferenceUtil.getPrefBottomToolbar() && !HOME_URL.equals(
getCurrentWebView().getUrl())
&& tabSwitcherRoot.getVisibility() != View.VISIBLE) {
bottomToolbar.setVisibility(View.VISIBLE);
if (getCurrentWebView() instanceof ToolbarStaticKiwixWebView
&& sharedPreferenceUtil.getPrefBottomToolbar()) {
contentFrame.setPadding(0, 0, 0,
(int) getResources().getDimension(R.dimen.bottom_toolbar_height));
} else {
contentFrame.setPadding(0, 0, 0, 0);
}
} else {
bottomToolbar.setVisibility(View.GONE);
contentFrame.setPadding(0, 0, 0, 0);
}
} else {
bottomToolbar.setVisibility(View.GONE);
contentFrame.setPadding(0, 0, 0, 0);
}
}

Expand Down Expand Up @@ -1574,14 +1581,16 @@ private void updateTabSwitcherIcon() {
}

private void refreshBookmarkSymbol() {
if (getCurrentWebView().getUrl() != null &&
ZimContentProvider.getId() != null &&
!getCurrentWebView().getUrl().equals(HOME_URL)) {
int icon = bookmarks.contains(getCurrentWebView().getUrl()) ? R.drawable.ic_bookmark_24dp
: R.drawable.ic_bookmark_border_24dp;
bottomToolbarBookmark.setImageResource(icon);
} else {
bottomToolbarBookmark.setImageResource(R.drawable.ic_bookmark_border_24dp);
if (checkNull(bottomToolbarBookmark)) {
if (getCurrentWebView().getUrl() != null &&
ZimContentProvider.getId() != null &&
!getCurrentWebView().getUrl().equals(HOME_URL)) {
int icon = bookmarks.contains(getCurrentWebView().getUrl()) ? R.drawable.ic_bookmark_24dp
: R.drawable.ic_bookmark_border_24dp;
bottomToolbarBookmark.setImageResource(icon);
} else {
bottomToolbarBookmark.setImageResource(R.drawable.ic_bookmark_border_24dp);
}
}
}

Expand Down Expand Up @@ -1832,16 +1841,18 @@ public void webViewFailedLoading(String url) {

@Override
public void webViewProgressChanged(int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
if (requestClearHistoryAfterLoad) {
Log.d(TAG_KIWIX,
"Loading article finished and requestClearHistoryAfterLoad -> clearHistory");
getCurrentWebView().clearHistory();
requestClearHistoryAfterLoad = false;
}
if (checkNull(progressBar)) {
progressBar.setProgress(progress);
if (progress == 100) {
if (requestClearHistoryAfterLoad) {
Log.d(TAG_KIWIX,
"Loading article finished and requestClearHistoryAfterLoad -> clearHistory");
getCurrentWebView().clearHistory();
requestClearHistoryAfterLoad = false;
}

Log.d(TAG_KIWIX, "Loaded URL: " + getCurrentWebView().getUrl());
Log.d(TAG_KIWIX, "Loaded URL: " + getCurrentWebView().getUrl());
}
}
}

Expand Down Expand Up @@ -1950,4 +1961,8 @@ private void searchFiles() {
fileSearch.scan(sharedPreferenceUtil.getPrefStorage());
}
}

public boolean checkNull(View view) {
return view != null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, encapsulating such small functionality in a separate function is overkill. You are anyways going to write if(checkNull(view_name)){}. Why not just if(view_name != null). Every function call has an overhead and hence I am not in favor of it. If readability is your main intention, then we might leave it like you have done but I find explicit check equally readable.

}