Refactor setProgress code for the header view.

Each call site of the `getHeaderView()` method needed to do a null
check and then it would call `setProgress()`. This has been replaced
with two methods `setProgress()` and `clearProgress()` to make it a
bit less repetative and harder to accidentally get a NPE in the future
by invoking `getHeaderView()` incorrectly.
This commit is contained in:
Peter Serwylo 2016-11-30 22:18:43 +11:00
parent 7d2d22cf96
commit 1eb7d389f2
2 changed files with 18 additions and 30 deletions

View File

@ -303,15 +303,11 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) { switch (intent.getAction()) {
case Downloader.ACTION_STARTED: case Downloader.ACTION_STARTED:
if (mAdapter.getHeaderView() != null) { mAdapter.setProgress(-1, -1, R.string.download_pending);
mAdapter.getHeaderView().setProgress(-1, -1, R.string.download_pending);
}
break; break;
case Downloader.ACTION_PROGRESS: case Downloader.ACTION_PROGRESS:
if (mAdapter.getHeaderView() != null) { mAdapter.setProgress(intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1),
mAdapter.getHeaderView().setProgress(intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1),
intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1), 0); intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1), 0);
}
break; break;
case Downloader.ACTION_COMPLETE: case Downloader.ACTION_COMPLETE:
// Starts the install process once the download is complete. // Starts the install process once the download is complete.
@ -341,21 +337,15 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) { switch (intent.getAction()) {
case Installer.ACTION_INSTALL_STARTED: case Installer.ACTION_INSTALL_STARTED:
if (mAdapter.getHeaderView() != null) { mAdapter.setProgress(-1, -1, R.string.installing);
mAdapter.getHeaderView().setProgress(-1, -1, R.string.installing);
}
break; break;
case Installer.ACTION_INSTALL_COMPLETE: case Installer.ACTION_INSTALL_COMPLETE:
if (mAdapter.getHeaderView() != null) { mAdapter.clearProgress();
mAdapter.getHeaderView().setProgress(0, 0, 0); // Remove
}
unregisterInstallReceiver(); unregisterInstallReceiver();
onAppChanged(); onAppChanged();
break; break;
case Installer.ACTION_INSTALL_INTERRUPTED: case Installer.ACTION_INSTALL_INTERRUPTED:
if (mAdapter.getHeaderView() != null) { mAdapter.clearProgress();
mAdapter.getHeaderView().setProgress(0, 0, 0); // Remove
}
onAppChanged(); onAppChanged();
String errorMessage = String errorMessage =
@ -398,21 +388,15 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) { switch (intent.getAction()) {
case Installer.ACTION_UNINSTALL_STARTED: case Installer.ACTION_UNINSTALL_STARTED:
if (mAdapter.getHeaderView() != null) { mAdapter.setProgress(-1, -1, R.string.uninstalling);
mAdapter.getHeaderView().setProgress(-1, -1, R.string.uninstalling);
}
break; break;
case Installer.ACTION_UNINSTALL_COMPLETE: case Installer.ACTION_UNINSTALL_COMPLETE:
if (mAdapter.getHeaderView() != null) { mAdapter.clearProgress();
mAdapter.getHeaderView().setProgress(0, 0, 0); // Remove
}
onAppChanged(); onAppChanged();
unregisterUninstallReceiver(); unregisterUninstallReceiver();
break; break;
case Installer.ACTION_UNINSTALL_INTERRUPTED: case Installer.ACTION_UNINSTALL_INTERRUPTED:
if (mAdapter.getHeaderView() != null) { mAdapter.clearProgress();
mAdapter.getHeaderView().setProgress(0, 0, 0); // Remove
}
String errorMessage = String errorMessage =
intent.getStringExtra(Installer.EXTRA_ERROR_MESSAGE); intent.getStringExtra(Installer.EXTRA_ERROR_MESSAGE);
@ -477,9 +461,7 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
*/ */
private void cleanUpFinishedDownload() { private void cleanUpFinishedDownload() {
mActiveDownloadUrlString = null; mActiveDownloadUrlString = null;
if (mAdapter.getHeaderView() != null) { mAdapter.clearProgress();
mAdapter.getHeaderView().setProgress(0, 0, 0); // Remove
}
unregisterDownloaderReceiver(); unregisterDownloaderReceiver();
} }

View File

@ -160,8 +160,14 @@ public class AppDetailsRecyclerViewAdapter
uriIsSetAndCanBeOpened(mApp.flattrID); uriIsSetAndCanBeOpened(mApp.flattrID);
} }
public HeaderViewHolder getHeaderView() { public void clearProgress() {
return mHeaderView; setProgress(0, 0, 0);
}
public void setProgress(int bytesDownloaded, int totalBytes, int resIdString) {
if (mHeaderView != null) {
mHeaderView.setProgress(bytesDownloaded, totalBytes, resIdString);
}
} }
@Override @Override