Skip to content

Commit

Permalink
Test background
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Mar 14, 2024
1 parent 2ad7957 commit ec84537
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 4 deletions.
40 changes: 40 additions & 0 deletions .idea/libraries/Dart_Packages.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = '1.9.23'
repositories {
google()
mavenCentral()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.gms:google-services:4.4.0"
classpath "com.google.gms:google-services:4.4.1"
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
org.gradle.jvmargs=-Xmx4G
android.useAndroidX=true
android.enableJetifier=true
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
2 changes: 1 addition & 1 deletion app/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
2 changes: 1 addition & 1 deletion app/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "com.android.application" version '8.3.0' apply false
}

include ":app"
6 changes: 6 additions & 0 deletions app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ PODS:
- video_player_avfoundation (0.0.1):
- Flutter
- FlutterMacOS
- workmanager (0.0.1):
- Flutter

DEPENDENCIES:
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
Expand All @@ -75,6 +77,7 @@ DEPENDENCIES:
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/darwin`)
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/darwin`)
- workmanager (from `.symlinks/plugins/workmanager/ios`)

SPEC REPOS:
trunk:
Expand Down Expand Up @@ -112,6 +115,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/sqflite/darwin"
video_player_avfoundation:
:path: ".symlinks/plugins/video_player_avfoundation/darwin"
workmanager:
:path: ".symlinks/plugins/workmanager/ios"

SPEC CHECKSUMS:
AppAuth: 3bb1d1cd9340bd09f5ed189fb00b1cc28e1e8570
Expand All @@ -135,6 +140,7 @@ SPEC CHECKSUMS:
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
Toast: ec33c32b8688982cecc6348adeae667c1b9938da
video_player_avfoundation: 02011213dab73ae3687df27ce441fbbcc82b5579
workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6

PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796

Expand Down
26 changes: 26 additions & 0 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart';
import 'ui/app.dart';

Future<void> main() async {
Expand All @@ -23,6 +24,16 @@ Future<void> main() async {

final container = await _configureContainerWithAsyncDependency();

Workmanager().initialize(
callbackDispatcher,
isInDebugMode: true,
);
Workmanager().registerPeriodicTask(
initialDelay: const Duration(seconds: 10),
frequency: const Duration(seconds: 10),
"auto-back-up", "google-drive-auto-back-up",
tag: "google-drive-auto-back-up");

runApp(
UncontrolledProviderScope(
container: container,
Expand All @@ -40,3 +51,18 @@ Future<ProviderContainer> _configureContainerWithAsyncDependency() async {
);
return container;
}

@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
for (var i = 1; i <= 15; i++) {
await Future.delayed(const Duration(seconds: 1));
print("Second: $i");
}

print("background service: code 2306");
final instance = await SharedPreferences.getInstance();
instance.setBool("is_onboard_complete", false);
return Future.value(true);
});
}
8 changes: 8 additions & 0 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.2.0"
workmanager:
dependency: "direct main"
description:
name: workmanager
sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00
url: "https://pub.dev"
source: hosted
version: "0.5.2"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies:
# core
firebase_core: ^2.24.2
collection: ^1.18.0
workmanager: ^0.5.2

# storage
shared_preferences: ^2.2.2
Expand Down
87 changes: 87 additions & 0 deletions data/lib/repositories/google_drive_auto_back_up_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'dart:async';
import 'package:data/services/auth_service.dart';
import 'package:data/services/google_drive_service.dart';
import 'package:data/services/local_media_service.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_sign_in/google_sign_in.dart';
import '../storage/app_preferences.dart';

final autoBackUpRepoProvider = Provider<GoogleDriveAutoBackUpRepo>((ref) {
final authService = ref.read(authServiceProvider);
final repo = GoogleDriveAutoBackUpRepo(
ref.read(googleDriveServiceProvider),
ref.read(localMediaServiceProvider),
authService,
ref.read(AppPreferences.canTakeAutoBackUpInGoogleDrive),
authService.googleAccount,
);
final subscription =
ref.listen(AppPreferences.canTakeAutoBackUpInGoogleDrive, (_, next) {
repo.updateGoogleDriveAutoBackUpStatus(value: next);
});
ref.onDispose(() {
subscription.close();
repo.dispose();
});
return repo;
});

class GoogleDriveAutoBackUpRepo {
final GoogleDriveService _googleDriveService;
final LocalMediaService _localMediaService;
final AuthService _authService;
GoogleSignInAccount? _googleAccount;
StreamSubscription? _googleAccountSubscription;
bool _googleDriveAutoBackUpStatus;

GoogleDriveAutoBackUpRepo(
this._googleDriveService,
this._localMediaService,
this._authService,
this._googleDriveAutoBackUpStatus,
this._googleAccount) {
_googleAccountSubscription =
_authService.onGoogleAccountChange.listen((googleAccount) {
_googleAccount = googleAccount;
});
}

Future<void> updateGoogleDriveAutoBackUpStatus({required bool value}) async {
_googleDriveAutoBackUpStatus = value;
}



Future<void> autoBackUp() async {
if (_googleAccount == null || !_googleDriveAutoBackUpStatus) {
return;
}
final hasAccess = await _localMediaService.requestPermission();
if (!hasAccess) return;
final backUpFolderId = await _googleDriveService.getBackupFolderId();
if (backUpFolderId == null) return;

final mediaCount = await _localMediaService.getMediaCount();
final localMedias =
await _localMediaService.getLocalMedia(start: 0, end: mediaCount);
print("localMedias: $localMedias");
final googleDriveMedias = await _googleDriveService.getDriveMedias(
backUpFolderId: backUpFolderId);
final Set<String> googleDrivePaths =
googleDriveMedias.map((media) => media.path).toSet();
final uploadingMedias = localMedias
.where((localMedia) => !googleDrivePaths.contains(localMedia.path))
.toList();
for (final media in uploadingMedias) {
await _googleDriveService.uploadInGoogleDrive(
media: media,
folderID: backUpFolderId,
);
}
}

Future<void> dispose() async {
_googleAccountSubscription?.cancel();
_googleAccount = null;
}
}

0 comments on commit ec84537

Please sign in to comment.