some work on background downloading
This commit is contained in:
parent
8b56745ccf
commit
b2c71b1403
|
|
@ -1,16 +1,72 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:extera_next/generated/l10n/l10n.dart';
|
import 'package:extera_next/generated/l10n/l10n.dart';
|
||||||
|
import 'package:extera_next/pages/download_manager/download_manager_view.dart';
|
||||||
|
import 'package:extera_next/widgets/matrix.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class DownloadManager extends StatefulWidget {
|
class Download {
|
||||||
@override
|
final String url;
|
||||||
State<StatefulWidget> createState() => DownloadManagerController();
|
final String name;
|
||||||
|
final BuildContext context;
|
||||||
|
late Future<Response<dynamic>> response;
|
||||||
|
late int receivedBytes = 0;
|
||||||
|
late int totalBytes = 1;
|
||||||
|
late double progress = 0;
|
||||||
|
Download(this.context, this.url, this.name);
|
||||||
|
|
||||||
|
void start() async {
|
||||||
|
try {
|
||||||
|
final mx = Matrix.of(context).client;
|
||||||
|
final directory = await getExternalStorageDirectory();
|
||||||
|
final downloadPath =
|
||||||
|
directory != null ? "${directory.path}/Download" : null;
|
||||||
|
|
||||||
|
if (downloadPath != null) {
|
||||||
|
// Create Dio instance
|
||||||
|
final dio = Dio();
|
||||||
|
// Progress status variables
|
||||||
|
|
||||||
|
// Download the file
|
||||||
|
response = dio.download(
|
||||||
|
url,
|
||||||
|
"$downloadPath/$name",
|
||||||
|
onReceiveProgress: (received, total) {
|
||||||
|
receivedBytes = received;
|
||||||
|
totalBytes = total;
|
||||||
|
progress = (receivedBytes / totalBytes) * 100;
|
||||||
|
print("Download progress: $progress%");
|
||||||
|
},
|
||||||
|
options: Options(
|
||||||
|
responseType: ResponseType.bytes,
|
||||||
|
headers: {'authorization': "Bearer ${mx.accessToken}"}),
|
||||||
|
);
|
||||||
|
print("Download completed and saved to $downloadPath/$name");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print("Error during download: $e");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DownloadManagerController extends State<DownloadManager> {
|
class DownloadManager extends StatefulWidget {
|
||||||
@override
|
final BuildContext context;
|
||||||
Widget build(BuildContext context) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
const DownloadManager(this.context);
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<StatefulWidget> createState() =>
|
||||||
|
Provider.of<DownloadManagerController>(this.context);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DownloadManagerController extends State<DownloadManager>
|
||||||
|
with ChangeNotifier {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) => DownloadManagerView(this);
|
||||||
|
|
||||||
|
final List<Download> downloads = [];
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,27 @@ class DownloadManagerView extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
body: ListView.builder(
|
||||||
|
itemCount: controller.downloads.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final download = controller.downloads[index];
|
||||||
|
return ListTile(
|
||||||
|
title: Text(download.name),
|
||||||
|
subtitle: LinearProgressIndicator(
|
||||||
|
value: download.progress,
|
||||||
|
backgroundColor: Colors.grey[200],
|
||||||
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
|
||||||
|
),
|
||||||
|
trailing: ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
// "Cancel" button action can be added here
|
||||||
|
},
|
||||||
|
child: Text("Cancel"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
|
import 'package:extera_next/pages/download_manager/download_manager.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:extera_next/generated/l10n/l10n.dart';
|
import 'package:extera_next/generated/l10n/l10n.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
import 'package:extera_next/config/routes.dart';
|
import 'package:extera_next/config/routes.dart';
|
||||||
|
|
@ -42,12 +44,14 @@ class FluffyChatApp extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ThemeBuilder(
|
return ThemeBuilder(
|
||||||
builder: (context, themeMode, primaryColor, pureBlack) => MaterialApp.router(
|
builder: (context, themeMode, primaryColor, pureBlack) =>
|
||||||
|
MaterialApp.router(
|
||||||
title: AppConfig.applicationName,
|
title: AppConfig.applicationName,
|
||||||
themeMode: themeMode,
|
themeMode: themeMode,
|
||||||
theme: FluffyThemes.buildTheme(context, Brightness.light, primaryColor, pureBlack),
|
theme: FluffyThemes.buildTheme(
|
||||||
darkTheme:
|
context, Brightness.light, primaryColor, pureBlack),
|
||||||
FluffyThemes.buildTheme(context, Brightness.dark, primaryColor, pureBlack),
|
darkTheme: FluffyThemes.buildTheme(
|
||||||
|
context, Brightness.dark, primaryColor, pureBlack),
|
||||||
scrollBehavior: CustomScrollBehavior(),
|
scrollBehavior: CustomScrollBehavior(),
|
||||||
localizationsDelegates: L10n.localizationsDelegates,
|
localizationsDelegates: L10n.localizationsDelegates,
|
||||||
supportedLocales: L10n.supportedLocales,
|
supportedLocales: L10n.supportedLocales,
|
||||||
|
|
@ -57,10 +61,13 @@ class FluffyChatApp extends StatelessWidget {
|
||||||
clients: clients,
|
clients: clients,
|
||||||
// Need a navigator above the Matrix widget for
|
// Need a navigator above the Matrix widget for
|
||||||
// displaying dialogs
|
// displaying dialogs
|
||||||
child: Matrix(
|
child: ChangeNotifierProvider(
|
||||||
clients: clients,
|
create: (context) => DownloadManagerController(),
|
||||||
store: store,
|
child: Matrix(
|
||||||
child: testWidget ?? child,
|
clients: clients,
|
||||||
|
store: store,
|
||||||
|
child: testWidget ?? child,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
16
pubspec.lock
16
pubspec.lock
|
|
@ -329,6 +329,22 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "7.0.2"
|
version: "7.0.2"
|
||||||
|
dio:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: dio
|
||||||
|
sha256: d90ee57923d1828ac14e492ca49440f65477f4bb1263575900be731a3dac66a9
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.9.0"
|
||||||
|
dio_web_adapter:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: dio_web_adapter
|
||||||
|
sha256: "7586e476d70caecaf1686d21eee7247ea43ef5c345eab9e0cc3583ff13378d78"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.1"
|
||||||
dynamic_color:
|
dynamic_color:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ dependencies:
|
||||||
vodozemac: ^0.2.0
|
vodozemac: ^0.2.0
|
||||||
wakelock_plus: ^1.2.2
|
wakelock_plus: ^1.2.2
|
||||||
webrtc_interface: ^1.0.13
|
webrtc_interface: ^1.0.13
|
||||||
|
dio: ^5.9.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_lints: ^3.0.0
|
flutter_lints: ^3.0.0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue