Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
#96[1h]. List backups.
  • Loading branch information
Evgenii Kanivets committed Aug 10, 2016
commit 2c436ce9e8df4d3e58def4d9100c429032e4340e
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.blogspot.e_kanivets.moneytracker.activity.external;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.blogspot.e_kanivets.moneytracker.R;
Expand All @@ -9,9 +11,10 @@
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.session.AppKeyPair;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
Expand Down Expand Up @@ -49,7 +52,10 @@ protected boolean initData() {
AndroidAuthSession session = new AndroidAuthSession(appKeys);
dbApi = new DropboxAPI<>(session);
if (accessToken == null) dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
else dbApi.getSession().setOAuth2AccessToken(accessToken);
else {
dbApi.getSession().setOAuth2AccessToken(accessToken);
fetchBackups();
}

return super.initData();
}
Expand All @@ -70,6 +76,7 @@ protected void onResume() {
dbApi.getSession().finishAuthentication();
preferenceController.writeDropboxAccessToken(dbApi.getSession().getOAuth2AccessToken());
btnBackupNow.setEnabled(true);
fetchBackups();
} catch (IllegalStateException e) {
Timber.e("Error authenticating: %s", e.getMessage());
}
Expand All @@ -84,6 +91,7 @@ public void backupNow() {
public void onBackupSuccess() {
Timber.d("Backup success.");
stopProgress();
fetchBackups();
}

@Override
Expand All @@ -100,4 +108,17 @@ public void onBackupFailure(String reason) {
}
});
}

private void fetchBackups() {
startProgress();
backupController.fetchBackups(dbApi, new BackupController.OnFetchBackupListListener() {
@Override
public void onBackupsFetched(@NonNull List<String> backupList) {
stopProgress();
ArrayAdapter<String> adapter = new ArrayAdapter<>(BackupActivity.this,
android.R.layout.simple_list_item_1, backupList);
listView.setAdapter(adapter);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Controller class to encapsulate backup logic.
Expand Down Expand Up @@ -43,6 +46,12 @@ public void makeBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
asyncTask.execute();
}

public void fetchBackups(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
@Nullable OnFetchBackupListListener listener) {
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbApi, listener);
asyncTask.execute();
}

@Nullable
private FileInputStream readAppDb() {
File dbFile = new File(getAppDbFileName());
Expand Down Expand Up @@ -114,6 +123,47 @@ protected void onPostExecute(String result) {
}
}

private class DropboxFetchBackupListAsyncTask extends AsyncTask<Void, List<String>, List<String>> {
private DropboxAPI<AndroidAuthSession> dbApi;

@Nullable
private OnFetchBackupListListener listener;

public DropboxFetchBackupListAsyncTask(DropboxAPI<AndroidAuthSession> dbApi,
@Nullable OnFetchBackupListListener listener) {
this.dbApi = dbApi;
this.listener = listener;
}

@Override
protected List<String> doInBackground(Void... params) {
List<DropboxAPI.Entry> entryList = new ArrayList<>();
List<String> backupList = new ArrayList<>();

try {
DropboxAPI.Entry entry = dbApi.metadata("/", -1, null, true, null);
entryList = entry.contents;
} catch (DropboxException e) {
e.printStackTrace();
}

for (DropboxAPI.Entry entry : entryList) {
backupList.add(entry.fileName());
}

return backupList;
}

@Override
protected void onPostExecute(List<String> backupList) {
super.onPostExecute(backupList);
if (listener == null) return;

Collections.reverse(backupList);
listener.onBackupsFetched(backupList);
}
}

public interface OnBackupListener {
String SUCCESS = "success";
String ERROR_AUTHENTICATION = "error_authentication";
Expand All @@ -122,4 +172,8 @@ public interface OnBackupListener {

void onBackupFailure(String reason);
}

public interface OnFetchBackupListListener {
void onBackupsFetched(@NonNull List<String> backupList);
}
}