Skip to content

Commit 7e4c7e0

Browse files
#Backup and restore methods and it design
1 parent 60e9e3f commit 7e4c7e0

24 files changed

+985
-33
lines changed
+14
Loading
-2 KB
Loading
-432 Bytes
Loading
1.09 KB
Loading
-3.43 KB
Loading
4.77 KB
Loading
1.62 KB
Loading
1.47 KB
Loading
2.17 KB
Loading

assets/locales/en.json

+35-1
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,39 @@
523523
"callEnded" : "Call Ended!",
524524
"wentWrong" : "Something went wrong. Please try again",
525525
"couldNotProcess" : "Couldn't process the link. Please try again.",
526-
"returnToChat" : "Return to chat"
526+
"returnToChat" : "Return to chat",
527+
"chatBackup" : "Chat Backup",
528+
"autoBackup": "Auto Backup",
529+
"restore": "Restore",
530+
"skip": "Skip",
531+
"backupFound": "Backup Found",
532+
"backupNotFound": "Backup Not Found",
533+
"backupTime": "Last Backup: --",
534+
"backupSize": "Backup Size: --",
535+
"googleAccount": "Google Account",
536+
"googleAccountEmail": "Mail ID",
537+
"scheduleBackUp": "Scheduled your backup",
538+
"restoreDescAndroid": "Restore your messages and media from Google Drive. If you don't restore now, you want be able to restore later.",
539+
"restoreDescIos": "In order to check and restore messages from iCloud backups, please sign in with your iCloud account.",
540+
"addAccount": "Add account",
541+
"addGoogleAccount": "Add Google account",
542+
"addAccountDesc": "As a new user you will need to add account. Please add account to sync your data to cloud.",
543+
"download": "Download",
544+
"localBackupDesc": "Download the cloud file to save it to local storage, if you tap on restore button the restore process will start from the file saved in your local storage.",
545+
"localBackUpRestore": "Local Backup & Restore",
546+
"googleDriveSettings": "Google Drive Settings",
547+
"lastBackUp": "Last Backup: ",
548+
"totalSize": "Total size: ",
549+
"backupNow": "Backup Now",
550+
"backUpOver": "Back up Over",
551+
"wifiOnly": "Wi-Fi only",
552+
"restoreBackup": "Restore Backup",
553+
"lastBackUpDesc": "Back up your chat history and media to Google Drive so if you loose your device or switch to new one, your chat history is safe.You can restore your chat history when you reinstall Mirrorfly.",
554+
"backupScheduleTitle" : "Schedule to run backups automatically",
555+
"iCloudInstructions" : "Sign in to iCloud to back up you're history. Tap here for instructions.",
556+
"openIphoneSettings" : "Open iPhone Settings.",
557+
"iCloudSignInDesc" : "Tap 'iCloud' and make sure you're signed in.",
558+
"iCloudDriveOnDesc": "Tap 'iCloud Drive' and set it to ON.",
559+
"iCloudDriveMirrorFlyOnDesc" : "In 'iCloud Drive', make sure 'Mirrorfly App' is also ON.",
560+
"iCloudTitleDesc" : "To back up your chat history:"
527561
}

lib/app/common/constants.dart

+18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ const Color previewTextColor = Color(0xff7f7f7f);
4343
const Color callsSubText = Color(0Xff737373);
4444
const Color notificationAlertBg = Color(0xffEFF4FD);
4545

46+
47+
const Color backUpDbColor = Color(0xffFFFFFF);
48+
const Color backupPhoneColor = Color(0xff3276E2);
49+
const Color backupTimerColor = Color(0xffC4D1E0);
50+
4651
class AppColors{
4752
static const Color callerBackground = Color(0xff152134);
4853
static const Color bottomCallOptionBackground = Color(0xff162337);
@@ -306,6 +311,19 @@ const String backupTimer = "assets/backup_restore/backup_timer.svg";
306311
const String backupDatabase = "assets/backup_restore/database.svg";
307312
const String backupSmartPhone = "assets/backup_restore/smartphone.svg";
308313

314+
const String backupAnimation1 = "assets/backup_restore/backup_animation_1.png";
315+
const String backupAnimation2 = "assets/backup_restore/backup_animation_2.png";
316+
const String backupAnimation3 = "assets/backup_restore/backup_animation_3.png";
317+
const String backupAnimation4 = "assets/backup_restore/backup_animation_4.png";
318+
const String backupAnimation5 = "assets/backup_restore/backup_animation_5.png";
319+
const String backupAnimation6 = "assets/backup_restore/backup_animation_6.png";
320+
321+
const String backupHistoryIcon = "assets/backup_restore/backup_history.png";
322+
const String addAccountUser = "assets/backup_restore/add_account_user.svg";
323+
324+
const String restoreCloud = "assets/backup_restore/restore_cloud.png";
325+
const String restoreSetting = "assets/backup_restore/restore_settings.png";
326+
309327

310328
//About us
311329
const String titleContactMsg = "Mirror Fly is a ready-to-go messaging solution for building enterprise-grade real-time chat IM applications that meet various degrees of requirements like team discussion, data sharing, task delegation and information handling on the go.";

lib/app/data/session_management.dart

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ class SessionManagement {
140140
await _preferences.setBool('${prefix}show_alert', false);
141141
}
142142

143+
static void setBackUpState(String backUpState) async {
144+
await _preferences.setString("${prefix}backUpState", backUpState);
145+
}
146+
143147
static Future clear()async{
144148
// await _preferences.clear();
145149
final keys = _preferences.getKeys();
@@ -215,4 +219,5 @@ class SessionManagement {
215219
static int lastPinChangedAt() => _preferences.getInt("$prefix${Constants.changedPinAt}") ?? DateTime.now().millisecondsSinceEpoch;
216220
static bool showAlert() => _preferences.getBool('${prefix}show_alert') ?? true;
217221
// static String getTopicId() => Constants.enableTopic ? Constants.topicId/*_preferences.getString('${prefix}topicId')*/ ?? ("5d3788c1-78ef-4158-a92b-a48f092da0b9") : "";
222+
static String? getBackUpState() => _preferences.getString('${prefix}backUpState') ?? "";
218223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
2+
3+
import 'dart:io';
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:icloud_storage_sync/icloud_storage_sync.dart';
7+
import 'package:icloud_storage_sync/models/icloud_file_download.dart';
8+
import 'package:mirror_fly_demo/app/data/session_management.dart';
9+
import 'package:mirrorfly_plugin/flychat.dart';
10+
import 'package:mirrorfly_plugin/logmessage.dart';
11+
12+
import 'package:googleapis/drive/v3.dart' as drive;
13+
import 'package:googleapis_auth/auth_io.dart';
14+
15+
16+
class BackupRestoreManager {
17+
18+
// Private constructor
19+
BackupRestoreManager._internal();
20+
21+
// Singleton instance
22+
static final BackupRestoreManager _instance = BackupRestoreManager._internal();
23+
24+
factory BackupRestoreManager() {
25+
return _instance;
26+
}
27+
28+
final icloudSyncPlugin = IcloudStorageSync();
29+
30+
var _backupFileName = '';
31+
32+
get backupFileName => _backupFileName;
33+
34+
var _iCloudContainerID = '';
35+
36+
final _iCloudRelativePath = 'Backups/MirrorFlyFlutter/UserData/';
37+
38+
get iCloudRelativePath => _iCloudRelativePath;
39+
40+
bool _isInitialized = false;
41+
42+
/// Check if the manager is initialized
43+
bool get isInitialized => _isInitialized;
44+
45+
var _clientId = '';
46+
47+
get clientId => _clientId;
48+
49+
final List<String> _scopes = [drive.DriveApi.driveReadonlyScope];
50+
51+
initialize({required iCloudContainerID, required googleClientId}){
52+
53+
if (_isInitialized) {
54+
LogMessage.d("BackupRestoreManager", "Already initialized.");
55+
return;
56+
}
57+
58+
_iCloudContainerID = iCloudContainerID;
59+
_clientId = googleClientId;
60+
_backupFileName = "Backup_${SessionManagement.getUsername()}.txt";
61+
62+
initializeEventListeners();
63+
64+
}
65+
66+
Future<bool> checkDriveAccess() async {
67+
if(Platform.isAndroid){
68+
return _checkGoogleDriveAccess();
69+
}else if (Platform.isIOS){
70+
return _checkICloudAccess();
71+
}else{
72+
LogMessage.d("Backup and Restore", "Platform is not Supported");
73+
return true;
74+
}
75+
}
76+
77+
Future<bool> checkBackUpFiles() async {
78+
if (Platform.isAndroid) {
79+
return false;
80+
}else if (Platform.isIOS){
81+
List<CloudFiles> iCloudFiles = await icloudSyncPlugin.getCloudFiles(containerId: _iCloudContainerID);
82+
return iCloudFiles.any((file) => file.filePath == backupFileName);
83+
}else{
84+
LogMessage.d("Backup and Restore", "Platform is not Supported");
85+
return false;
86+
}
87+
}
88+
89+
Future<void> uploadBackupFile({required String filePath}) async {
90+
if(Platform.isAndroid){
91+
92+
}else if (Platform.isIOS){
93+
await icloudSyncPlugin.upload(
94+
containerId: _iCloudContainerID,
95+
filePath: filePath,
96+
destinationRelativePath: iCloudRelativePath,
97+
onProgress: onBackupUploadProgress,
98+
);
99+
100+
}else{
101+
LogMessage.d("Backup and Restore", "Platform is not Supported");
102+
}
103+
}
104+
105+
Future<void> backupFile() async {
106+
await Mirrorfly.startBackup();
107+
}
108+
109+
void onBackupUploadProgress(Stream<double> p1) {
110+
debugPrint("Uploading to server progress $p1");
111+
}
112+
113+
Future<bool> _checkICloudAccess() async {
114+
try {
115+
var iCloudFile = await icloudSyncPlugin.gather(containerId: _iCloudContainerID);
116+
LogMessage.d("BackupRestoreManager Access Success", iCloudFile);
117+
return true;
118+
} catch (e) {
119+
LogMessage.d("BackupRestoreManager", "iCloud access check failed: $e");
120+
return false;
121+
}
122+
}
123+
124+
125+
Future<bool> _checkGoogleDriveAccess() async {
126+
try {
127+
// Authenticate using Google Sign-In
128+
final authClient = await clientViaUserConsent(
129+
ClientId(_clientId, null),
130+
_scopes,
131+
(url) {
132+
LogMessage.d("BackupRestoreManager", "Please visit the following URL and grant access: $url");
133+
134+
},
135+
);
136+
137+
// Create an instance of the Drive API
138+
final driveApi = drive.DriveApi(authClient);
139+
140+
// Attempt to list files in Google Drive
141+
final fileList = await driveApi.files.list();
142+
LogMessage.d("BackupRestoreManager", "Files in Drive: ${fileList.files?.length ?? 0}");
143+
144+
// If no exception is thrown, Drive access is available
145+
authClient.close();
146+
return true;
147+
} catch (e) {
148+
// If an error occurs, Drive access is not available
149+
LogMessage.d("BackupRestoreManager", "Error checking Google Drive access: $e");
150+
return false;
151+
}
152+
}
153+
154+
155+
void initializeEventListeners() {
156+
Mirrorfly.onBackupSuccess.listen((event) {
157+
debugPrint("onBackupSuccess==> $event");
158+
uploadBackupFile(filePath: "filePath");
159+
});
160+
161+
Mirrorfly.onBackupFailure.listen((event) {
162+
163+
});
164+
165+
Mirrorfly.onBackupProgressChanged.listen((event) {
166+
167+
});
168+
169+
Mirrorfly.onRestoreSuccess.listen((event) {
170+
171+
});
172+
173+
Mirrorfly.onRestoreFailure.listen((event) {
174+
175+
});
176+
177+
Mirrorfly.onRestoreProgressChanged.listen((event) {
178+
179+
});
180+
}
181+
182+
void destroy() {
183+
LogMessage.d("BackupRestoreManager", "Destroying manager...");
184+
_isInitialized = false;
185+
_iCloudContainerID = '';
186+
_backupFileName = '';
187+
}
188+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
import 'package:get/get.dart';
4+
5+
class BackupController extends GetxController {
6+
7+
}

0 commit comments

Comments
 (0)