]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / BackupsActivity.java
1 /*
2  * Copyright © 2022 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger;
19
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.view.MenuItem;
25 import android.view.View;
26
27 import androidx.activity.result.ActivityResultLauncher;
28 import androidx.activity.result.contract.ActivityResultContracts;
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
31
32 import com.google.android.material.snackbar.BaseTransientBottomBar;
33 import com.google.android.material.snackbar.Snackbar;
34
35 import net.ktnx.mobileledger.backup.ConfigReader;
36 import net.ktnx.mobileledger.backup.ConfigWriter;
37 import net.ktnx.mobileledger.databinding.FragmentBackupsBinding;
38 import net.ktnx.mobileledger.model.Data;
39
40 import java.io.IOException;
41 import java.text.DateFormat;
42 import java.text.SimpleDateFormat;
43 import java.util.Date;
44 import java.util.Locale;
45
46 public class BackupsActivity extends AppCompatActivity {
47     private FragmentBackupsBinding b;
48     private ActivityResultLauncher<String> backupChooserLauncher;
49     private ActivityResultLauncher<String[]> restoreChooserLauncher;
50     public static void start(Context context) {
51         Intent starter = new Intent(context, BackupsActivity.class);
52         context.startActivity(starter);
53     }
54     @Override
55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         b = FragmentBackupsBinding.inflate(getLayoutInflater());
58         setContentView(b.getRoot());
59
60         setSupportActionBar(b.toolbar);
61         // Show the Up button in the action bar.
62         ActionBar actionBar = getSupportActionBar();
63         if (actionBar != null) {
64             actionBar.setDisplayHomeAsUpEnabled(true);
65         }
66
67         b.backupButton.setOnClickListener(this::backupClicked);
68         b.restoreButton.setOnClickListener(this::restoreClicked);
69
70
71         backupChooserLauncher = registerForActivityResult(
72                 new ActivityResultContracts.CreateDocument("application/json"), this::storeConfig);
73         restoreChooserLauncher =
74                 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
75                         this::readConfig);
76
77         Data.observeProfile(this, p -> {
78             if (p == null) {
79                 b.backupButton.setEnabled(false);
80                 b.backupExplanationText.setEnabled(false);
81             }
82             else {
83                 b.backupButton.setEnabled(true);
84                 b.backupExplanationText.setEnabled(true);
85             }
86         });
87     }
88     @Override
89     public boolean onOptionsItemSelected(MenuItem item) {
90         if (item.getItemId() == android.R.id.home) {
91             finish();
92
93             return true;
94         }
95         return super.onOptionsItemSelected(item);
96     }
97     private void storeConfig(Uri result) {
98         if (result == null)
99             return;
100
101         try {
102             ConfigWriter saver =
103                     new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
104                         @Override
105                         public void error(Exception e) {
106                             Snackbar.make(b.backupButton, e.toString(),
107                                     BaseTransientBottomBar.LENGTH_LONG)
108                                     .show();
109                         }
110                     }, new ConfigWriter.OnDoneListener() {
111                         public void done() {
112                             Snackbar.make(b.backupButton, R.string.config_saved,
113                                     Snackbar.LENGTH_LONG)
114                                     .show();
115                         }
116                     });
117             saver.start();
118         }
119         catch (IOException e) {
120             e.printStackTrace();
121         }
122
123     }
124     private void readConfig(Uri result) {
125         if (result == null)
126             return;
127
128         try {
129             ConfigReader reader =
130                     new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
131                         @Override
132                         public void error(Exception e) {
133                             Snackbar.make(b.backupButton, e.toString(),
134                                     BaseTransientBottomBar.LENGTH_LONG)
135                                     .show();
136                         }
137                     }, new ConfigReader.OnDoneListener() {
138                         public void done() {
139                             Snackbar.make(b.backupButton, R.string.config_restored,
140                                     Snackbar.LENGTH_LONG)
141                                     .show();
142                         }
143                     });
144             reader.start();
145         }
146         catch (IOException e) {
147             e.printStackTrace();
148         }
149
150     }
151     private void backupClicked(View view) {
152         final Date now = new Date();
153         DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
154         df.format(now);
155         backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
156     }
157     private void restoreClicked(View view) {
158         restoreChooserLauncher.launch(new String[]{"application/json"});
159     }
160
161 }