Skip to content

Commit

Permalink
Working on updating version download state correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
GottaGetSwifty committed Dec 16, 2015
1 parent c0f47ee commit 6028da5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package adapters.versions;

import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
Expand Down Expand Up @@ -59,13 +60,14 @@ public VersionViewModel.ResourceViewModel getViewModel() {
return viewModel;
}

public void updateViews(VersionViewModel.ResourceViewModel model){
public void updateViews(final VersionViewModel.ResourceViewModel model){

this.viewModel = model;
model.getDownloadStateAsync(new DataFileManager.GetDownloadStateResponse() {
@Override
public void foundDownloadState(DownloadState state) {
setupForDownloadState(state);
Log.d(TAG, "updated Views for model: " + model.toString() + "to download state: " + state.toString());
}
});
resourceImage.setImageResource(model.getImageResource());
Expand Down
41 changes: 27 additions & 14 deletions app/app/src/main/java/adapters/versions/VersionViewModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adapters.versions;

import android.content.Context;
import android.util.Log;

import org.unfoldingword.mobile.R;

Expand Down Expand Up @@ -49,9 +50,9 @@ public VersionViewModel(Context context, Version version, VersionViewModelListen

public void updateContent(){
version = Version.getVersionForId(version.getId(), DaoDBHelper.getDaoSession(context));
for(ResourceViewModel model : resources){
model.setState(DownloadState.DOWNLOAD_STATE_DOWNLOADING);
}
// for(ResourceViewModel model : resources){
// model.setState(DownloadState.DOWNLOAD_STATE_DOWNLOADING);
// }
}

private void setupResources(){
Expand Down Expand Up @@ -98,6 +99,7 @@ private void showCheckingLevel(MediaType type){

public class ResourceViewModel{

private static final String TAG = "ResourceViewModel";
private DownloadState state = DownloadState.DOWNLOAD_STATE_DOWNLOADING;
private MediaType type;

Expand Down Expand Up @@ -149,21 +151,23 @@ public void foundDownloadState(DownloadState newState) {
public void getDownloadStateAsync(final DataFileManager.GetDownloadStateResponse response){

if(isInLoadingEvent()){
Log.d(TAG, "is in loading event");
state = DownloadState.DOWNLOAD_STATE_DOWNLOADING;
response.foundDownloadState(state);
return;
}
response.foundDownloadState(state);

DataFileManager.getStateOfContent(context, version, type, new DataFileManager.GetDownloadStateResponse() {
@Override
public void foundDownloadState(DownloadState newState) {
state = newState;
if(response != null) {
response.foundDownloadState(newState);
else {
Log.d(TAG, "isn't loading event");
response.foundDownloadState(state);
DataFileManager.getStateOfContent(context, version, type, new DataFileManager.GetDownloadStateResponse() {
@Override
public void foundDownloadState(DownloadState newState) {
state = newState;
if (response != null) {
response.foundDownloadState(newState);
}
}
}
});
});
}
}

private boolean isInLoadingEvent(){
Expand Down Expand Up @@ -222,6 +226,15 @@ public void foundDownloadState(DownloadState state) {
public void checkingLevelClicked(){
showCheckingLevel(type);
}


@Override
public String toString() {
return "ResourceViewModel{" +
"state=" + state.toString() +
", type=" + type.toString() +
'}';
}
}

public interface VersionViewModelListener{
Expand Down
12 changes: 12 additions & 0 deletions app/app/src/main/java/eventbusmodels/DownloadingVersionsEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eventbusmodels;

import android.support.annotation.Nullable;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -14,6 +15,7 @@
*/
public class DownloadingVersionsEvent {

private static final String TAG = "DownloadingVrsionsEvent";
private Map<String, DownloadTrackingModel> models;

public DownloadingVersionsEvent() {
Expand Down Expand Up @@ -49,6 +51,7 @@ public static boolean containsModel(Version version, MediaType type){
return false;
}
else{
Log.d(TAG, "checked if version: " + version.getSlug() + " : " + type.toString() + " Is on event: " + event.toString());
return event.getModels().containsKey(getKey(version, type));
}

Expand All @@ -68,6 +71,15 @@ public static DownloadingVersionsEvent getEventAdding(Version version, MediaType
return (event.addModel(version, type))? event : null;
}

public static DownloadingVersionsEvent forceGetEventAdding(Version version, MediaType type){

DownloadingVersionsEvent event = EventBus.getDefault().getStickyEvent(DownloadingVersionsEvent.class);
if(event == null){
event = new DownloadingVersionsEvent();
}
return event;
}

@Nullable
public static DownloadingVersionsEvent getEventRemoving(Version version, MediaType type){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,31 @@ public void onStop() {
}

public void onEventMainThread(DownloadingVersionsEvent event){

Log.d(TAG, "Received Event: " + event.toString());

// need to wait for the event to finish calling before reloading the list
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try{
synchronized (this){
wait(500);
}
}
catch (InterruptedException e){
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
reloadData();
}
}.execute();

}

public void onEventMainThread(DownloadResult event){
Expand Down Expand Up @@ -379,13 +403,14 @@ private void stopDownload(VersionViewModel viewModel, MediaType type){

private void downloadText(VersionViewModel viewModel){

downloadText(viewModel.getVersion().getId());
downloadText(viewModel.getVersion());
}

private void downloadText(long versionId){
private void downloadText(Version version){
Intent downloadIntent = new Intent(getContext(), UWVersionDownloaderService.class);
downloadIntent.putExtra(UWVersionDownloaderService.VERSION_PARAM, versionId);
downloadIntent.putExtra(UWVersionDownloaderService.VERSION_PARAM, version.getId());
getContext().startService(downloadIntent);
EventBus.getDefault().postSticky(DownloadingVersionsEvent.forceGetEventAdding(version, MediaType.MEDIA_TYPE_TEXT));
}

private void downloadAudio(final VersionViewModel viewModel){
Expand All @@ -404,8 +429,7 @@ public void foundDownloadState(DownloadState state) {
if(state == DownloadState.DOWNLOAD_STATE_NONE){
for(VersionViewModel.ResourceViewModel model : viewModel.getResources()){
if(model.getType() == MediaType.MEDIA_TYPE_TEXT){
downloadText(viewModel.getVersion().getId());
reloadData();
downloadText(viewModel.getVersion());
break;
}
}
Expand All @@ -426,11 +450,12 @@ public void dismissed() {

private void downloadAudio(final VersionViewModel viewModel, AudioBitrate bitrate){

Intent downloadIntent = new Intent(getContext(), UWMediaDownloaderService.class)
.putExtra(UWMediaDownloaderService.VERSION_PARAM, viewModel.getVersion().getId())
.putExtra(UWMediaDownloaderService.IS_VIDEO_PARAM, false)
.putExtra(UWMediaDownloaderService.BITRATE_PARAM, bitrate);
getContext().startService(downloadIntent);
Intent downloadIntent = new Intent(getContext(), UWMediaDownloaderService.class)
.putExtra(UWMediaDownloaderService.VERSION_PARAM, viewModel.getVersion().getId())
.putExtra(UWMediaDownloaderService.IS_VIDEO_PARAM, false)
.putExtra(UWMediaDownloaderService.BITRATE_PARAM, bitrate);
getContext().startService(downloadIntent);
EventBus.getDefault().postSticky(DownloadingVersionsEvent.forceGetEventAdding(viewModel.getVersion(), MediaType.MEDIA_TYPE_AUDIO));
}

private void downloadVideo(final VersionViewModel viewModel){
Expand Down
7 changes: 7 additions & 0 deletions app/app/src/main/java/model/DataFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public static void getStateOfContent(final Context context, final Version versio

@Override
protected DownloadState doInBackground(Void... params) {

Log.d(TAG, "started checking of content state asynctask");
File mediaFolder = getFileForDownload(context, type, version);
if(!mediaFolder.exists()){
Log.d(TAG, "Media folder didn't exist");
return DownloadState.DOWNLOAD_STATE_NONE;
}
else {
Expand Down Expand Up @@ -142,15 +145,19 @@ private static DownloadState verifyStateForContent(Version version, MediaType ty
int expectedSize = getCountForMediaType(version, type);
int numberOfFiles = folder.listFiles().length;
if (expectedSize < 1) {
Log.d(TAG, "expected size is < 1");
return DownloadState.DOWNLOAD_STATE_NONE;
}
else if(expectedSize > numberOfFiles){
Log.d(TAG, "expected size is " + expectedSize + " but number of files is " + numberOfFiles);
return DownloadState.DOWNLOAD_STATE_DOWNLOADING;
}
else if (expectedSize == numberOfFiles) {
Log.d(TAG, "expected size is good!");
return DownloadState.DOWNLOAD_STATE_DOWNLOADED;
}
else{
Log.d(TAG, "error that shouldn't happen");
return DownloadState.DOWNLOAD_STATE_ERROR;
}
}
Expand Down
23 changes: 23 additions & 0 deletions app/app/src/main/java/model/DownloadState.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,27 @@ public static DownloadState createState(int value) {
}
}
}

private String getAsString(){

switch (this){
case DOWNLOAD_STATE_NONE:{
return "DOWNLOAD_STATE_NONE";
}
case DOWNLOAD_STATE_DOWNLOADING:{
return "DOWNLOAD_STATE_DOWNLOADING";
}
case DOWNLOAD_STATE_DOWNLOADED:{
return "DOWNLOAD_STATE_DOWNLOADED";
}
default:{
return "DOWNLOAD_STATE_ERROR";
}
}
}

@Override
public String toString() {
return "DownloadState{" + getAsString() + "}";
}
}

0 comments on commit 6028da5

Please sign in to comment.