Skip to content

Commit 57b5f1a

Browse files
committed
Refactor to use builder for CacheRequest
1 parent 68993be commit 57b5f1a

27 files changed

Lines changed: 1446 additions & 1397 deletions

src/main/java/org/quantumbadger/redreader/activities/ImageViewActivity.java

Lines changed: 138 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -881,152 +881,156 @@ private void makeCacheRequest(
881881
= new AtomicReference<>();
882882
final AtomicReference<String> videoMimetype = new AtomicReference<>();
883883

884-
CacheManager.getInstance(this).makeRequest(mImageOrVideoRequest = new CacheRequest(
885-
uri,
886-
RedditAccountManager.getAnon(),
887-
null,
888-
new Priority(Constants.Priority.IMAGE_VIEW),
889-
DownloadStrategyIfNotCached.INSTANCE,
890-
Constants.FileType.IMAGE,
891-
CacheRequest.DownloadQueueType.IMMEDIATE,
892-
CacheRequest.RequestMethod.GET,
893-
this,
894-
new CacheRequestCallbacks() {
895-
896-
private boolean mProgressTextSet = false;
897-
898-
@Override
899-
public void onFailure(@NonNull final RRError error) {
884+
CacheManager.getInstance(this).makeRequest(
885+
mImageOrVideoRequest = new CacheRequest.Builder()
886+
.setUrl(uri)
887+
.setUser(RedditAccountManager.getAnon())
888+
.setPriority(new Priority(Constants.Priority.IMAGE_VIEW))
889+
.setDownloadStrategy(DownloadStrategyIfNotCached.INSTANCE)
890+
.setFileType(Constants.FileType.IMAGE)
891+
.setQueueType(CacheRequest.DownloadQueueType.IMMEDIATE)
892+
.setRequestMethod(CacheRequest.RequestMethod.GET)
893+
.setCache(true)
894+
.setContext(this)
895+
.setCallbacks(new CacheRequestCallbacks() {
896+
private boolean mProgressTextSet = false;
897+
898+
@Override
899+
public void onFailure(@NonNull final RRError error) {
900+
901+
synchronized(resultLock) {
902+
903+
if(!failed.getAndSet(true)) {
904+
AndroidCommon.UI_THREAD_HANDLER.post(() -> {
905+
final LinearLayout layout
906+
= new LinearLayout(
907+
ImageViewActivity.this);
908+
final ErrorView errorView = new ErrorView(
909+
ImageViewActivity.this,
910+
error);
911+
layout.addView(errorView);
912+
General.setLayoutMatchWidthWrapHeight(errorView);
913+
setMainView(layout);
914+
});
915+
}
916+
}
917+
}
900918

901-
synchronized(resultLock) {
902-
903-
if(!failed.getAndSet(true)) {
904-
AndroidCommon.UI_THREAD_HANDLER.post(() -> {
905-
final LinearLayout layout
906-
= new LinearLayout(ImageViewActivity.this);
907-
final ErrorView errorView = new ErrorView(
908-
ImageViewActivity.this,
909-
error);
910-
layout.addView(errorView);
911-
General.setLayoutMatchWidthWrapHeight(errorView);
912-
setMainView(layout);
919+
@Override
920+
public void onDownloadNecessary() {
921+
AndroidCommon.runOnUiThread(() -> {
922+
progressBar.setVisibility(View.VISIBLE);
923+
progressBar.setIndeterminate(true);
924+
manageAspectRatioIndicator(progressBar);
913925
});
914926
}
915-
}
916-
}
917927

918-
@Override
919-
public void onDownloadNecessary() {
920-
AndroidCommon.runOnUiThread(() -> {
921-
progressBar.setVisibility(View.VISIBLE);
922-
progressBar.setIndeterminate(true);
923-
manageAspectRatioIndicator(progressBar);
924-
});
925-
}
926-
927-
@Override
928-
public void onProgress(
929-
final boolean authorizationInProgress,
930-
final long bytesRead,
931-
final long totalBytes) {
932-
933-
AndroidCommon.runOnUiThread(() -> {
934-
progressBar.setVisibility(View.VISIBLE);
935-
progressBar.setIndeterminate(authorizationInProgress);
936-
progressBar.setProgress(
937-
((float)((1000 * bytesRead) / totalBytes)) / 1000);
938-
manageAspectRatioIndicator(progressBar);
939-
940-
if(!mProgressTextSet) {
941-
mProgressText.setText(General.bytesToMegabytes(totalBytes));
942-
mProgressTextSet = true;
928+
@Override
929+
public void onProgress(
930+
final boolean authorizationInProgress,
931+
final long bytesRead,
932+
final long totalBytes) {
933+
934+
AndroidCommon.runOnUiThread(() -> {
935+
progressBar.setVisibility(View.VISIBLE);
936+
progressBar.setIndeterminate(authorizationInProgress);
937+
progressBar.setProgress(
938+
((float)((1000 * bytesRead) / totalBytes)) / 1000);
939+
manageAspectRatioIndicator(progressBar);
940+
941+
if(!mProgressTextSet) {
942+
mProgressText.setText(General.bytesToMegabytes(totalBytes));
943+
mProgressTextSet = true;
944+
}
945+
});
943946
}
944-
});
945-
}
946-
947-
@Override
948-
public void onDataStreamAvailable(
949-
@NonNull final GenericFactory<SeekableInputStream, IOException>
950-
streamFactory,
951-
final TimestampUTC timestamp,
952-
@NonNull final UUID session,
953-
final boolean fromCache,
954-
@Nullable final String mimetype) {
955-
956-
synchronized(resultLock) {
957-
958-
if(audio.get() != null || audioUri == null) {
959-
onImageStreamReady(
960-
!fromCache,
961-
streamFactory,
962-
audio.get(),
963-
mimetype,
964-
Uri.parse(uri.toString()));
965947

966-
} else {
967-
video.set(streamFactory);
968-
videoMimetype.set(mimetype);
948+
@Override
949+
public void onDataStreamAvailable(
950+
@NonNull final GenericFactory<SeekableInputStream, IOException>
951+
streamFactory,
952+
final TimestampUTC timestamp,
953+
@NonNull final UUID session,
954+
final boolean fromCache,
955+
@Nullable final String mimetype) {
956+
957+
synchronized(resultLock) {
958+
959+
if(audio.get() != null || audioUri == null) {
960+
onImageStreamReady(
961+
!fromCache,
962+
streamFactory,
963+
audio.get(),
964+
mimetype,
965+
Uri.parse(uri.toString()));
966+
967+
} else {
968+
video.set(streamFactory);
969+
videoMimetype.set(mimetype);
970+
}
971+
}
969972
}
970-
}
971-
}
972-
}));
973+
})
974+
.build());
973975

974976
if(audioUri != null) {
975-
CacheManager.getInstance(this).makeRequest(mAudioRequest = new CacheRequest(
976-
audioUri,
977-
RedditAccountManager.getAnon(),
978-
null,
979-
new Priority(Constants.Priority.IMAGE_VIEW),
980-
DownloadStrategyIfNotCached.INSTANCE,
981-
Constants.FileType.IMAGE,
982-
CacheRequest.DownloadQueueType.IMMEDIATE,
983-
CacheRequest.RequestMethod.GET,
984-
this,
985-
new CacheRequestCallbacks() {
986-
@Override
987-
public void onFailure(@NonNull final RRError error) {
988-
989-
synchronized(resultLock) {
990-
991-
if(!failed.getAndSet(true)) {
992-
993-
AndroidCommon.runOnUiThread(() -> {
994-
final LinearLayout layout
995-
= new LinearLayout(ImageViewActivity.this);
996-
final ErrorView errorView = new ErrorView(
997-
ImageViewActivity.this,
998-
error);
999-
layout.addView(errorView);
1000-
General.setLayoutMatchWidthWrapHeight(errorView);
1001-
setMainView(layout);
1002-
});
977+
CacheManager.getInstance(this)
978+
.makeRequest(mAudioRequest = new CacheRequest.Builder()
979+
.setUrl(audioUri)
980+
.setUser(RedditAccountManager.getAnon())
981+
.setPriority(new Priority(Constants.Priority.IMAGE_VIEW))
982+
.setDownloadStrategy(DownloadStrategyIfNotCached.INSTANCE)
983+
.setFileType(Constants.FileType.IMAGE)
984+
.setQueueType(CacheRequest.DownloadQueueType.IMMEDIATE)
985+
.setRequestMethod(CacheRequest.RequestMethod.GET)
986+
.setCache(true)
987+
.setContext(this)
988+
.setCallbacks(new CacheRequestCallbacks() {
989+
@Override
990+
public void onFailure(@NonNull final RRError error) {
991+
992+
synchronized (resultLock) {
993+
994+
if (!failed.getAndSet(true)) {
995+
996+
AndroidCommon.runOnUiThread(() -> {
997+
final LinearLayout layout
998+
= new LinearLayout(ImageViewActivity.this);
999+
final ErrorView errorView = new ErrorView(
1000+
ImageViewActivity.this,
1001+
error);
1002+
layout.addView(errorView);
1003+
General.setLayoutMatchWidthWrapHeight(errorView);
1004+
setMainView(layout);
1005+
});
1006+
}
1007+
}
10031008
}
1004-
}
1005-
}
10061009

1007-
@Override
1008-
public void onDataStreamAvailable(
1009-
@NonNull final GenericFactory<
1010-
SeekableInputStream, IOException> streamFactory,
1011-
final TimestampUTC timestamp,
1012-
@NonNull final UUID session,
1013-
final boolean fromCache,
1014-
@Nullable final String mimetype) {
1015-
1016-
synchronized(resultLock) {
1017-
if(video.get() != null) {
1018-
onImageStreamReady(
1019-
!fromCache,
1020-
video.get(),
1021-
streamFactory,
1022-
videoMimetype.get(),
1023-
Uri.parse(uri.toString()));
1024-
} else {
1025-
audio.set(streamFactory);
1010+
@Override
1011+
public void onDataStreamAvailable(
1012+
@NonNull final GenericFactory<
1013+
SeekableInputStream, IOException> streamFactory,
1014+
final TimestampUTC timestamp,
1015+
@NonNull final UUID session,
1016+
final boolean fromCache,
1017+
@Nullable final String mimetype) {
1018+
1019+
synchronized (resultLock) {
1020+
if (video.get() != null) {
1021+
onImageStreamReady(
1022+
!fromCache,
1023+
video.get(),
1024+
streamFactory,
1025+
videoMimetype.get(),
1026+
Uri.parse(uri.toString()));
1027+
} else {
1028+
audio.set(streamFactory);
1029+
}
1030+
}
10261031
}
1027-
}
1028-
}
1029-
}));
1032+
})
1033+
.build());
10301034
}
10311035
}
10321036

0 commit comments

Comments
 (0)