Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/backup #17

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion flutter.compileSdkVersion

compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<queries>
<intent>
<action android:name="android.speech.RecognitionService" />
Expand Down
42 changes: 25 additions & 17 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/adapters.dart';
Expand All @@ -17,24 +18,10 @@ import 'pages/splash/splash_screen.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeHive();
await initializeGetX();

// Initialize Hive and Hive Flutter
await Hive.initFlutter();
registerAdapters();
Hive.openBox<HiveUser>('user');
Hive.openBox<HiveShowPreview>('collection');
Hive.openBox<HiveShowPreview>('watchlist');
Hive.openBox<HiveShowPreview>('history');
Hive.openBox<HiveShowPreview>('artists');

// Initialize the controllers
Get.put(MainController());
Get.put(HomeDataController());
Get.put(SearchBarController());
Get.put(ProfileController());
Get.put(CacheData());

runApp(const ProviderScope(child: App()));
runApp(ProviderScope(child: Phoenix(child: const App())));
}

class App extends StatelessWidget {
Expand All @@ -54,3 +41,24 @@ class App extends StatelessWidget {
);
}
}

Future? initializeHive() async {
// Initialize Hive and Hive Flutter
await Hive.initFlutter();
registerAdapters();
Hive.openBox<HiveUser>('user');
Hive.openBox<HiveShowPreview>('collection');
Hive.openBox<HiveShowPreview>('watchlist');
Hive.openBox<HiveShowPreview>('history');
Hive.openBox<HiveShowPreview>('artists');
}

Future? initializeGetX() {
// Initialize the controllers
Get.put(MainController());
Get.put(HomeDataController());
Get.put(SearchBarController());
Get.put(ProfileController());
Get.put(CacheData());
return null;
}
4 changes: 4 additions & 0 deletions lib/models/hive/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class HiveUser extends HiveObject {
late String username;
@HiveField(UserFields.imageUrl)
late String imageUrl;

// factory HiveUser.fromJson(Map<String, dynamic> json) {
// return HiveUser..name = json['name']..username = json['username']..imageUrl = json['imageUrl'];
// }
}
32 changes: 32 additions & 0 deletions lib/models/show_models/show_preview_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,49 @@ class ShowPreview {
domestic: json['domestic'] ?? "",
foreignLifetimeGross: json['foreignLifetimeGross'] ?? "",
foreign: json['foreign'] ?? "",
companies: json['companies'] ?? "",
contentRating: json['contentRating'] ?? "",
countries: json['countries'] ?? "",
genres: json['genres'] ?? "",
languages: json['languages'] ?? "",
similars: json['similars'] == null
? null
: [
for (var similar in json['similars']!)
ShowPreview.fromJson(similar)
],
watchDate:
json["watchDate"] == null ? null : DateTime.parse(json["watchDate"]),
watchTime: json['watchTime'] == null
? null
: TimeOfDay.fromDateTime(DateTime.parse(json["watchDate"])),
);
}

static Map<String, dynamic> toMap(ShowPreview show) => {
'id': show.id,
'rank': show.rank,
'title': show.title,
'type': show.type,
'crew': show.crew,
'image': show.image,
'year': show.year,
'imDbRating': show.imDbRating,
'imDbVotes': show.imDbVotes,
'released': show.released,
'seasonNumber': show.seasonNumber,
'episodeNumber': show.episodeNumber,
'plot': show.plot,
'genres': show.genres,
'countries': show.countries,
'languages': show.languages,
'companies': show.companies,
'contentRating': show.contentRating,
'watchDate': show.watchDate?.toString(),
'watchTime': show.watchTime?.toString(),
'similars': [
for (var similar in show.similars!) ShowPreview.toMap(similar)
]
};

static String encode(List<ShowPreview> shows) => json.encode(
Expand Down
1 change: 1 addition & 0 deletions lib/modules/Recommender/Recommender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Future recommender() async {
PreferencesShareholder preferencesShareholder = PreferencesShareholder();
HomeDataController homeDataController = Get.find<HomeDataController>();
List<List<ShowPreview>> allLists = await preferencesShareholder.getAllLists();
print(allLists);
List<ShowPreview> allSimilars = await getAllSimilars(allLists: allLists);
Map<String, int> allSimilarsStats = {};
int i = 0;
Expand Down
185 changes: 185 additions & 0 deletions lib/modules/preferences/backup.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import 'dart:convert';
import 'dart:io';
import 'package:external_path/external_path.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:movielab/models/hive/models/show_preview.dart';
import 'package:movielab/models/hive/models/user.dart';
import 'package:movielab/models/show_models/show_preview_model.dart';
import 'package:movielab/models/user_model/user_model.dart';
import 'package:movielab/modules/preferences/preferences_shareholder.dart';
import 'package:movielab/modules/recommender/recommender.dart';
import 'package:movielab/pages/splash/get_user_data.dart';
import 'package:permission_handler/permission_handler.dart';

Future<bool> createBackup() async {
try {
Map<String, dynamic> userData = {};

PreferencesShareholder preferencesShareholder = PreferencesShareholder();
User user = await preferencesShareholder.getUser();
userData['personal'] = {
"name": user.name,
"username": user.username,
"imageUrl": user.imageUrl
};

List<ShowPreview> watchlist =
await preferencesShareholder.getList(listName: "watchlist");
userData['watchlist'] = [
for (ShowPreview show in watchlist) ShowPreview.toMap(show)
];
List<ShowPreview> history =
await preferencesShareholder.getList(listName: "history");
userData['history'] = [
for (ShowPreview show in history) ShowPreview.toMap(show)
];
List<ShowPreview> collection =
await preferencesShareholder.getList(listName: "collection");
userData['collection'] = [
for (ShowPreview show in collection) ShowPreview.toMap(show)
];
List<ShowPreview> artists =
await preferencesShareholder.getList(listName: "artists");
userData['artists'] = [
for (ShowPreview show in artists) ShowPreview.toMap(show)
];
String jsonData = jsonEncode(userData);
await Permission.storage.request();
String formattedDate = DateTime.now()
.toString()
.replaceAll('.', '-')
.replaceAll(' ', '-')
.replaceAll(':', '-');
String? dir = await FilePicker.platform.getDirectoryPath();
String path = '$dir/MovieLab-backup-$formattedDate.db';
File backupFile = File(path);
await backupFile.writeAsString(jsonData);
return true;
} catch (e) {
return false;
}
}

Future<bool> restoreBackup() async {
PreferencesShareholder shareholder = PreferencesShareholder();
FilePickerResult? file = await FilePicker.platform.pickFiles(
allowMultiple: false,
initialDirectory: await ExternalPath.getExternalStoragePublicDirectory(
ExternalPath.DIRECTORY_DOCUMENTS),
dialogTitle: "MovieLab backup file");
bool success = false;
if (file != null) {
File files = File(file.files.single.path.toString());

await backupFileTester(files).then((value) async {
if (value) {
Map<String, dynamic> backup = jsonDecode(await files.readAsString());
Box<HiveUser> user = Hive.box<HiveUser>('user');
Box<HiveShowPreview> watchlist = Hive.box<HiveShowPreview>('watchlist');
Box<HiveShowPreview> history = Hive.box<HiveShowPreview>('history');
Box<HiveShowPreview> collection =
Hive.box<HiveShowPreview>('collection');
Box<HiveShowPreview> artists = Hive.box<HiveShowPreview>('artists');

user.deleteAt(0);
shareholder.deleteList("watchlist");
shareholder.deleteList("history");
shareholder.deleteList("collection");
shareholder.deleteList("artists");

// print("history: ${history.length}");
// for (int i = 0; i <= history.length + 1; i++) {
// print("history $i deliting");
// history.delete(i);
// print("done");
// }
// print(collection.length);
// for (int i = 0; i <= collection.length + 1; i++) {
// collection.delete(i);
// }
// print(artists.length);
// for (int i = 0; i <= artists.length; i++) {
// artists.delete(i);
// }
user.put(
0,
HiveUser()
..name = backup['personal']['name']
..username = backup['personal']['username']
..imageUrl = backup['personal']['imageUrl']);

for (var item in backup['watchlist']) {
shareholder.addShowToList(
listName: 'watchlist',
showPreview: ShowPreview.fromJson(item),
genres: item['genres'],
countries: item['countries'],
languages: item['languages'],
companies: item['companies'],
contentRating: item['contentRating'],
similars: [
for (Map<String, dynamic> show in item['similars'])
ShowPreview.fromJson(show)
],
);
}

for (var item in backup['history']) {
shareholder.addShowToList(
listName: 'history',
showPreview: ShowPreview.fromJson(item),
genres: item['genres'],
countries: item['countries'],
languages: item['languages'],
companies: item['companies'],
contentRating: item['contentRating'],
similars: [
for (Map<String, dynamic> show in item['similars'])
ShowPreview.fromJson(show)
],
);
}
for (var item in backup['collection']) {
shareholder.addShowToList(
listName: 'collection',
showPreview: ShowPreview.fromJson(item),
genres: item['genres'],
countries: item['countries'],
languages: item['languages'],
companies: item['companies'],
contentRating: item['contentRating'],
similars: [
for (Map<String, dynamic> show in item['similars'])
ShowPreview.fromJson(show)
],
);
}

recommender();
getUserData();
success = true;
}
});
return success;
} else {
return success;
}
}

Future<bool> backupFileTester(final File files) async {
try {
Map<String, dynamic> backup = jsonDecode(await files.readAsString());
backup['personal'];
if (kDebugMode) {
print("The imported file is a real backup.");
}
return true;
} catch (e) {
if (kDebugMode) {
print('There\'s a problem with the imported file.');
}
return false;
}
}
4 changes: 2 additions & 2 deletions lib/modules/preferences/preferences_shareholder.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:movielab/models/hive/convertor.dart';
import 'package:movielab/models/hive/models/show_preview.dart';
import 'package:movielab/models/hive/models/user.dart';
import 'package:movielab/models/show_models/show_preview_model.dart';
import 'package:movielab/models/user_model/user_model.dart';
import 'package:movielab/modules/Recommender/Recommender.dart';
import 'package:movielab/pages/splash/get_user_data.dart';
import '../../models/hive/convertor.dart';
import '../../models/hive/models/show_preview.dart';

class PreferencesShareholder {
// Delete all items of a list in the shared preferences
Expand Down
Loading