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

WIP (Help needed): Add switch to only download with wifi connection. #336

Closed
wants to merge 2 commits into from
Closed
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
31 changes: 31 additions & 0 deletions lib/components/SettingsScreen/download_settings_switches.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:finamp/services/finamp_settings_helper.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';

import '../../models/finamp_models.dart';

class DownloadSettingsSwitches extends StatefulWidget {
const DownloadSettingsSwitches({Key? key}) : super(key: key);

@override
State<DownloadSettingsSwitches> createState() =>
_DownloadSettingsSwitchesState();
}

class _DownloadSettingsSwitchesState extends State<DownloadSettingsSwitches> {
bool onlyDownloadWithWifi = false;

@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Box<FinampSettings>>(
valueListenable: FinampSettingsHelper.finampSettingsListener,
builder: (_, box, __) {
return SwitchListTile(
value: FinampSettingsHelper.finampSettings.onlyDownloadWithWifi,
onChanged: (value) =>
FinampSettingsHelper.setOnlyDownloadWithWifi(value),
);
},
);
}
}
5 changes: 5 additions & 0 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const _showTextOnGridView = true;
const _sleepTimerSeconds = 1800; // 30 Minutes
const _showCoverAsPlayerBackground = true;
const _hideSongArtistsIfSameAsAlbumArtists = true;
const _onlyDownloadWithWifi = false;

@HiveType(typeId: 28)
class FinampSettings {
Expand Down Expand Up @@ -73,6 +74,7 @@ class FinampSettings {
this.showCoverAsPlayerBackground = _showCoverAsPlayerBackground,
this.hideSongArtistsIfSameAsAlbumArtists =
_hideSongArtistsIfSameAsAlbumArtists,
this.onlyDownloadWithWifi = _onlyDownloadWithWifi,
});

@HiveField(0)
Expand Down Expand Up @@ -142,6 +144,9 @@ class FinampSettings {
bool hideSongArtistsIfSameAsAlbumArtists =
_hideSongArtistsIfSameAsAlbumArtists;

@HiveField(18, defaultValue: _onlyDownloadWithWifi)
bool onlyDownloadWithWifi = _onlyDownloadWithWifi;

static Future<FinampSettings> create() async {
final internalSongDir = await getInternalSongDir();
final downloadLocation = DownloadLocation.create(
Expand Down
7 changes: 5 additions & 2 deletions lib/models/finamp_models.g.dart

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

4 changes: 3 additions & 1 deletion lib/screens/downloads_settings_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../components/SettingsScreen/download_settings_switches.dart';
import 'add_download_location_screen.dart';
import '../components/DownloadLocationSettingsScreen/download_location_list.dart';

Expand All @@ -20,7 +21,8 @@ class DownloadsSettingsScreen extends StatelessWidget {
onPressed: () => Navigator.of(context)
.pushNamed(AddDownloadLocationScreen.routeName),
),
body: const DownloadLocationList(),
body: ListView(
children: const [DownloadSettingsSwitches(), DownloadLocationList()]),
);
}
}
56 changes: 56 additions & 0 deletions lib/services/connectivity_change_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:get_it/get_it.dart';

import 'downloads_helper.dart';

class ConnectivityChangeNotifier extends ChangeNotifier {
DownloadsHelper downloadsHelper = GetIt.instance<DownloadsHelper>();
ConnectivityResult connectivityResult = ConnectivityResult.none;

ConnectivityChangeNotifier() {
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
resultHandler(result);
});
}

// TODO: Load the initial load from main.dart
void initialLoad() async {
ConnectivityResult connectivityResult =
await (Connectivity().checkConnectivity());
resultHandler(connectivityResult);
}

void resultHandler(ConnectivityResult result) async {
if (connectivityResult != result) {
connectivityResult = result;
// TODO: ... and check for switch
if (connectivityResult == ConnectivityResult.mobile) {
downloadsHelper.pause(await getRunningDownloadTaskIds());
// TODO: ... and check for switch
} else if (connectivityResult == ConnectivityResult.wifi) {}
downloadsHelper.resume(await getPausedDownloadTaskIds());
}
notifyListeners();
}

Future<List<String>> getRunningDownloadTaskIds() async {
List<DownloadTask> downloads = [];
downloads.addAll(await downloadsHelper
.getDownloadsWithStatus(DownloadTaskStatus.enqueued) ??
[]);
downloads.addAll(await downloadsHelper
.getDownloadsWithStatus(DownloadTaskStatus.running) ??
[]);
return downloads.map((e) => e.taskId).toList();
}

Future<List<String>> getPausedDownloadTaskIds() async {
List<DownloadTask> downloads = [];
downloads.addAll(await downloadsHelper
.getDownloadsWithStatus(DownloadTaskStatus.paused) ??
[]);
return downloads.map((e) => e.taskId).toList();
}
}
14 changes: 14 additions & 0 deletions lib/services/downloads_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ class DownloadsHelper {
_downloadedParentsBox.values;

Iterable<DownloadedSong> get downloadedItems => _downloadedItemsBox.values;

Iterable<DownloadedImage> get downloadedImages => _downloadedImagesBox.values;

ValueListenable<Box<DownloadedSong>> getDownloadedItemsListenable(
Expand Down Expand Up @@ -872,6 +873,18 @@ class DownloadsHelper {

return directory;
}

Future<void> pause(List<String> tasks) async {
for (String task in tasks) {
FlutterDownloader.pause(taskId: task);
}
}

Future<void> resume(List<String> tasks) async {
for (String task in tasks) {
FlutterDownloader.resume(taskId: task);
}
}
}

@HiveType(typeId: 3)
Expand Down Expand Up @@ -964,6 +977,7 @@ class DownloadedSong {

factory DownloadedSong.fromJson(Map<String, dynamic> json) =>
_$DownloadedSongFromJson(json);

Map<String, dynamic> toJson() => _$DownloadedSongToJson(this);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/services/finamp_settings_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,11 @@ class FinampSettingsHelper {
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setOnlyDownloadWithWifi(bool onlyDownloadWithWifi) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.onlyDownloadWithWifi = onlyDownloadWithWifi;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}
}
Loading