Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
291 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:ubuntu_service/ubuntu_service.dart'; | ||
|
||
import '../../common.dart'; | ||
import '../../data.dart'; | ||
import '../../library.dart'; | ||
import 'download_model.dart'; | ||
|
||
class DownloadButton extends StatelessWidget { | ||
const DownloadButton({ | ||
super.key, | ||
this.iconSize, | ||
required this.audio, | ||
}); | ||
|
||
static Widget create({ | ||
required BuildContext context, | ||
double? iconSize, | ||
required Audio? audio, | ||
}) { | ||
return ChangeNotifierProvider( | ||
create: (_) => DownloadModel(getService<LibraryService>()), | ||
child: DownloadButton( | ||
iconSize: iconSize, | ||
audio: audio, | ||
), | ||
); | ||
} | ||
|
||
final double? iconSize; | ||
final Audio? audio; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final theme = Theme.of(context); | ||
final model = context.read<DownloadModel>(); | ||
final value = context.select((DownloadModel m) => m.value); | ||
return Stack( | ||
alignment: Alignment.center, | ||
children: [ | ||
Progress( | ||
value: value ?? 0, | ||
backgroundColor: Colors.transparent, | ||
), | ||
IconButton( | ||
icon: Icon(Iconz().download), | ||
// TODO: add remove download | ||
onPressed: () => model.startDownload(audio), | ||
iconSize: iconSize, | ||
color: audio?.path != null ? theme.colorScheme.primary : null, | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:safe_change_notifier/safe_change_notifier.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
import '../../data.dart'; | ||
import '../../library.dart'; | ||
|
||
class DownloadModel extends SafeChangeNotifier { | ||
DownloadModel(this._service); | ||
|
||
final LibraryService _service; | ||
|
||
double? _value; | ||
double? get value => _value; | ||
void setValue(int received, int total) { | ||
if (total <= 0) return; | ||
_value = received / total; | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> startDownload( | ||
Audio? audio, | ||
) async { | ||
_service.dio.interceptors.add(LogInterceptor()); | ||
// Assure the value of total argument of onReceiveProgress is not -1. | ||
_service.dio.options.headers = {HttpHeaders.acceptEncodingHeader: '*'}; | ||
final downloadsDir = _service.downloadsDir; | ||
|
||
if (audio?.url == null || downloadsDir == null) return; | ||
|
||
final downloadDir = p.join(downloadsDir, 'musicpod'); | ||
|
||
if (!Directory(downloadDir).existsSync()) { | ||
Directory(downloadDir).createSync(); | ||
} | ||
final fileName = | ||
'${audio?.artist}${audio?.title}${audio?.year}'.replaceAll(' ', '_'); | ||
final file = File( | ||
p.join( | ||
downloadDir, | ||
fileName, | ||
), | ||
); | ||
file.createSync(); | ||
|
||
download1(_service.dio, audio!.url!, file.path); | ||
} | ||
|
||
Future download1(Dio dio, String url, String path) async { | ||
final cancelToken = CancelToken(); | ||
try { | ||
await dio | ||
.download( | ||
url, | ||
path, | ||
onReceiveProgress: setValue, | ||
cancelToken: cancelToken, | ||
) | ||
.then((_) => _service.addDownload(url, path)); | ||
} catch (e) { | ||
//TODO: manage download exception | ||
} | ||
} | ||
} |
Oops, something went wrong.