44import android .support .annotation .NonNull ;
55import android .support .v7 .app .AlertDialog ;
66import android .view .View ;
7- import android .widget .ArrayAdapter ;
87import android .widget .ListView ;
98
109import com .blogspot .e_kanivets .moneytracker .MtApp ;
1110import com .blogspot .e_kanivets .moneytracker .R ;
1211import com .blogspot .e_kanivets .moneytracker .activity .base .BaseBackActivity ;
13- import com .blogspot .e_kanivets .moneytracker .controller .BackupController ;
12+ import com .blogspot .e_kanivets .moneytracker .adapter .BackupAdapter ;
13+ import com .blogspot .e_kanivets .moneytracker .controller .backup .BackupController ;
1414import com .blogspot .e_kanivets .moneytracker .controller .PreferenceController ;
1515import com .blogspot .e_kanivets .moneytracker .util .AnswersProxy ;
1616import com .dropbox .core .DbxRequestConfig ;
2424import butterknife .BindView ;
2525import butterknife .OnClick ;
2626import butterknife .OnItemClick ;
27+ import org .jetbrains .annotations .NotNull ;
28+ import org .jetbrains .annotations .Nullable ;
2729import timber .log .Timber ;
2830
29- public class BackupActivity extends BaseBackActivity {
31+ public class BackupActivity extends BaseBackActivity
32+ implements BackupAdapter .OnBackupListener , BackupController .OnBackupListener {
3033 private static final String APP_KEY = "5lqugcckdy9y6lj" ;
3134
32- @ Inject
33- PreferenceController preferenceController ;
34- @ Inject
35- BackupController backupController ;
35+ @ Inject PreferenceController preferenceController ;
36+ @ Inject BackupController backupController ;
3637
3738 private DbxClientV2 dbClient ;
3839
39- @ BindView (R .id .btn_backup_now )
40- View btnBackupNow ;
41- @ BindView (R .id .list_view )
42- ListView listView ;
40+ @ BindView (R .id .btn_backup_now ) View btnBackupNow ;
41+ @ BindView (R .id .list_view ) ListView listView ;
4342
44- @ Override
45- protected int getContentViewId () {
43+ @ Override protected int getContentViewId () {
4644 return R .layout .activity_backup ;
4745 }
4846
49- @ Override
50- protected boolean initData () {
47+ @ Override protected boolean initData () {
5148 getAppComponent ().inject (BackupActivity .this );
5249
5350 String accessToken = preferenceController .readDropboxAccessToken ();
54- if (accessToken == null ) Auth .startOAuth2Authentication (BackupActivity .this , APP_KEY );
55- else {
51+ if (accessToken == null ) {
52+ Auth .startOAuth2Authentication (BackupActivity .this , APP_KEY );
53+ } else {
5654 DbxRequestConfig config = new DbxRequestConfig ("open_money_tracker" );
5755 dbClient = new DbxClientV2 (config , accessToken );
56+ backupController .setOnBackupListener (this );
5857 fetchBackups ();
5958 }
6059
6160 return super .initData ();
6261 }
6362
64- @ Override
65- protected void initViews () {
63+ @ Override protected void initViews () {
6664 super .initViews ();
6765 btnBackupNow .setEnabled (preferenceController .readDropboxAccessToken () != null );
6866 }
6967
70- @ Override
71- protected void onResume () {
68+ @ Override protected void onResume () {
7269 super .onResume ();
7370
7471 if (Auth .getOAuth2Token () != null ) {
@@ -84,46 +81,114 @@ protected void onResume() {
8481 }
8582 }
8683
87- @ OnClick (R .id .btn_backup_now )
88- public void backupNow () {
89- AnswersProxy .get ().logButton ("Make Backup" );
90- startProgress (getString (R .string .making_backup ));
91- backupController .makeBackup (dbClient , new BackupController .OnBackupListener () {
92- @ Override
93- public void onBackupSuccess () {
94- AnswersProxy .get ().logEvent ("Backup success" );
95- Timber .d ("Backup success." );
96- if (isFinishing ()) return ;
97-
98- stopProgress ();
99- fetchBackups ();
84+ @ Override public void onBackupDelete (@ NotNull final String backupName ) {
85+ AlertDialog .Builder builder = new AlertDialog .Builder (BackupActivity .this );
86+ builder .setTitle (getString (R .string .delete_backup_title ));
87+ builder .setMessage (getString (R .string .delete_backup_message , backupName ));
88+ builder .setPositiveButton (android .R .string .ok , new DialogInterface .OnClickListener () {
89+ @ Override public void onClick (DialogInterface dialog , int which ) {
90+ removeBackup (backupName );
10091 }
92+ });
93+ builder .setNegativeButton (android .R .string .cancel , null );
94+ builder .show ();
95+ }
10196
102- @ Override
103- public void onBackupFailure (String reason ) {
104- AnswersProxy .get ().logEvent ("Backup failure" );
105- Timber .d ("Backup failure." );
106- if (isFinishing ()) return ;
97+ @ Override public void onBackupsFetched (@ NonNull List <String > backupList ) {
98+ if (isFinishing ()) return ;
99+
100+ stopProgress ();
101+
102+ BackupAdapter backupAdapter = new BackupAdapter (BackupActivity .this , backupList );
103+ backupAdapter .setOnBackupListener (BackupActivity .this );
104+ listView .setAdapter (backupAdapter );
105+ }
107106
108- stopProgress ();
109- showToast (R .string .failed_create_backup );
107+ @ Override public void onBackupSuccess () {
108+ AnswersProxy .get ().logEvent ("Backup success" );
109+ Timber .d ("Backup success." );
110+ if (isFinishing ()) return ;
110111
111- if (BackupController .OnBackupListener .ERROR_AUTHENTICATION .equals (reason )) logout ();
112+ stopProgress ();
113+ fetchBackups ();
114+ }
115+
116+ @ Override public void onBackupFailure (String reason ) {
117+ AnswersProxy .get ().logEvent ("Backup failure" );
118+ Timber .d ("Backup failure." );
119+ if (isFinishing ()) return ;
120+
121+ stopProgress ();
122+ showToast (R .string .failed_create_backup );
123+
124+ if (BackupController .OnBackupListener .ERROR_AUTHENTICATION .equals (reason )) logout ();
125+ }
126+
127+ @ Override public void onRestoreSuccess (@ NonNull String backupName ) {
128+ AnswersProxy .get ().logEvent ("Restore Success" );
129+ Timber .d ("Restore success." );
130+ if (isFinishing ()) return ;
131+
132+ stopProgress ();
133+
134+ AlertDialog .Builder builder = new AlertDialog .Builder (BackupActivity .this );
135+ builder .setTitle (getString (R .string .backup_is_restored ));
136+ builder .setMessage (getString (R .string .backup_restored , backupName ));
137+ builder .setOnDismissListener (new DialogInterface .OnDismissListener () {
138+ @ Override public void onDismiss (DialogInterface dialog ) {
139+ MtApp .get ().buildAppComponent ();
140+ setResult (RESULT_OK );
141+ finish ();
112142 }
113143 });
144+ builder .setPositiveButton (android .R .string .ok , null );
145+ builder .show ();
146+ }
147+
148+ @ Override public void onRestoreFailure (String reason ) {
149+ AnswersProxy .get ().logEvent ("Restore Failure" );
150+ Timber .d ("Restore failure." );
151+ if (isFinishing ()) return ;
152+
153+ stopProgress ();
154+ showToast (R .string .failed_restore_backup );
155+
156+ if (BackupController .OnBackupListener .ERROR_AUTHENTICATION .equals (reason )) logout ();
157+ }
158+
159+ @ Override public void onRemoveSuccess () {
160+ AnswersProxy .get ().logEvent ("Remove Success" );
161+ Timber .d ("Remove success." );
162+ if (isFinishing ()) return ;
163+
164+ stopProgress ();
165+ fetchBackups ();
114166 }
115167
116- @ OnItemClick (R .id .list_view )
117- public void restoreBackupClicked (int position ) {
168+ @ Override public void onRemoveFailure (@ Nullable String reason ) {
169+ AnswersProxy .get ().logEvent ("Remove Failure" );
170+ Timber .d ("Remove failure." );
171+ if (isFinishing ()) return ;
172+
173+ stopProgress ();
174+ showToast (reason );
175+ }
176+
177+ @ OnClick (R .id .btn_backup_now ) public void backupNow () {
178+ AnswersProxy .get ().logButton ("Make Backup" );
179+ startProgress (getString (R .string .making_backup ));
180+ backupController .makeBackup (dbClient );
181+ }
182+
183+ @ OnItemClick (R .id .list_view ) public void restoreBackupClicked (int position ) {
118184 AnswersProxy .get ().logButton ("Restore backup" );
119185 final String backupName = listView .getAdapter ().getItem (position ).toString ();
120186
121187 AlertDialog .Builder builder = new AlertDialog .Builder (BackupActivity .this );
122188 builder .setTitle (getString (R .string .warning ));
123189 builder .setMessage (getString (R .string .want_erase_and_restore , backupName ));
124190 builder .setPositiveButton (android .R .string .ok , new DialogInterface .OnClickListener () {
125- @ Override
126- public void onClick (DialogInterface dialog , int which ) {
191+ @ Override public void onClick (DialogInterface dialog , int which ) {
127192 restoreBackup (backupName );
128193 }
129194 });
@@ -133,58 +198,17 @@ public void onClick(DialogInterface dialog, int which) {
133198
134199 private void restoreBackup (final String backupName ) {
135200 startProgress (getString (R .string .restoring_backup ));
136- backupController .restoreBackup (dbClient , backupName , new BackupController .OnRestoreBackupListener () {
137- @ Override
138- public void onRestoreSuccess () {
139- AnswersProxy .get ().logEvent ("Restore Success" );
140- Timber .d ("Restore success." );
141- if (isFinishing ()) return ;
142-
143- stopProgress ();
144-
145- AlertDialog .Builder builder = new AlertDialog .Builder (BackupActivity .this );
146- builder .setTitle (getString (R .string .backup_is_restored ));
147- builder .setMessage (getString (R .string .backup_restored , backupName ));
148- builder .setOnDismissListener (new DialogInterface .OnDismissListener () {
149- @ Override
150- public void onDismiss (DialogInterface dialog ) {
151- MtApp .get ().buildAppComponent ();
152- setResult (RESULT_OK );
153- finish ();
154- }
155- });
156- builder .setPositiveButton (android .R .string .ok , null );
157- builder .show ();
158- }
159-
160- @ Override
161- public void onRestoreFailure (String reason ) {
162- AnswersProxy .get ().logEvent ("Restore Failure" );
163- Timber .d ("Restore failure." );
164- if (isFinishing ()) return ;
165-
166- stopProgress ();
167- showToast (R .string .failed_restore_backup );
168-
169- if (BackupController .OnRestoreBackupListener .ERROR_AUTHENTICATION .equals (reason ))
170- logout ();
171- }
172- });
201+ backupController .restoreBackup (dbClient , backupName );
173202 }
174203
175204 private void fetchBackups () {
176205 startProgress (getString (R .string .fetching_backups ));
177- backupController .fetchBackups (dbClient , new BackupController .OnFetchBackupListListener () {
178- @ Override
179- public void onBackupsFetched (@ NonNull List <String > backupList ) {
180- if (isFinishing ()) return ;
181-
182- stopProgress ();
183- ArrayAdapter <String > adapter = new ArrayAdapter <>(BackupActivity .this ,
184- android .R .layout .simple_list_item_1 , backupList );
185- listView .setAdapter (adapter );
186- }
187- });
206+ backupController .fetchBackups (dbClient );
207+ }
208+
209+ private void removeBackup (String backupName ) {
210+ startProgress ("" );
211+ backupController .removeBackup (dbClient , backupName );
188212 }
189213
190214 private void logout () {
0 commit comments