tsacdop-podcast-app-android/lib/home/import_ompl.dart

106 lines
3.7 KiB
Dart
Raw Normal View History

2020-06-11 17:13:10 +02:00
import 'package:connectivity/connectivity.dart';
2020-02-09 13:29:09 +01:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2020-06-11 17:13:10 +02:00
import '../local_storage/key_value_storage.dart';
import '../local_storage/sqflite_localpodcast.dart';
import '../state/download_state.dart';
2020-07-26 12:20:42 +02:00
import '../state/podcast_group.dart';
2020-05-06 18:50:32 +02:00
import '../state/refresh_podcast.dart';
import '../util/extension_helper.dart';
2020-02-09 13:29:09 +01:00
class Import extends StatelessWidget {
Widget importColumn(String text, BuildContext context) {
return Container(
color: context.primaryColorDark,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(height: 2.0, child: LinearProgressIndicator()),
Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
height: 20.0,
alignment: Alignment.centerLeft,
child: Text(text),
),
]),
);
}
2020-06-11 17:13:10 +02:00
_autoDownloadNew(BuildContext context) async {
2020-07-26 12:20:42 +02:00
final dbHelper = DBHelper();
2020-06-11 17:13:10 +02:00
var downloader = Provider.of<DownloadState>(context, listen: false);
var result = await Connectivity().checkConnectivity();
2020-07-26 12:20:42 +02:00
var autoDownloadStorage = KeyValueStorage(autoDownloadNetworkKey);
var autoDownloadNetwork = await autoDownloadStorage.getInt();
2020-06-11 17:13:10 +02:00
if (autoDownloadNetwork == 1) {
2020-07-26 12:20:42 +02:00
var episodes = await dbHelper.getNewEpisodes('all');
2020-06-11 17:13:10 +02:00
// For safety
2020-07-26 12:20:42 +02:00
if (episodes.length < 100 && episodes.length > 0) {
2020-07-13 09:41:59 +02:00
for (var episode in episodes) {
2020-06-16 06:40:51 +02:00
await downloader.startTask(episode, showNotification: true);
2020-07-13 09:41:59 +02:00
}
2020-07-26 12:20:42 +02:00
}
2020-06-11 17:13:10 +02:00
} else if (result == ConnectivityResult.wifi) {
2020-07-26 12:20:42 +02:00
var episodes = await dbHelper.getNewEpisodes('all');
2020-06-11 17:13:10 +02:00
//For safety
2020-07-26 12:20:42 +02:00
if (episodes.length < 100 && episodes.length > 0) {
2020-07-13 09:41:59 +02:00
for (var episode in episodes) {
2020-06-16 06:40:51 +02:00
await downloader.startTask(episode, showNotification: true);
2020-07-13 09:41:59 +02:00
}
2020-07-26 12:20:42 +02:00
}
2020-06-11 17:13:10 +02:00
}
}
2020-02-09 13:29:09 +01:00
@override
Widget build(BuildContext context) {
2020-07-04 16:42:56 +02:00
final s = context.s;
2020-07-26 12:20:42 +02:00
var groupList = Provider.of<GroupList>(context, listen: false);
return Column(
children: <Widget>[
Consumer<GroupList>(
builder: (_, subscribeWorker, __) {
2020-07-26 12:20:42 +02:00
var item = subscribeWorker.currentSubscribeItem;
switch (item.subscribeState) {
case SubscribeState.start:
2020-07-04 16:42:56 +02:00
return importColumn(
s.notificationSubscribe(item.title), context);
case SubscribeState.subscribe:
2020-07-04 16:42:56 +02:00
return importColumn(s.notificaitonFatch(item.title), context);
case SubscribeState.fetch:
2020-07-04 16:42:56 +02:00
return importColumn(s.notificationSuccess(item.title), context);
case SubscribeState.exist:
return importColumn(
2020-07-04 16:42:56 +02:00
s.notificationSubscribeExisted(item.title), context);
case SubscribeState.error:
return importColumn(
2020-07-04 16:42:56 +02:00
s.notificationNetworkError(item.title), context);
default:
return Center();
}
},
),
Consumer<RefreshWorker>(
builder: (context, refreshWorker, child) {
2020-07-26 12:20:42 +02:00
var item = refreshWorker.currentRefreshItem;
if (refreshWorker.complete) {
groupList.updateGroups();
2020-06-11 17:13:10 +02:00
_autoDownloadNew(context);
}
switch (item.refreshState) {
case RefreshState.fetch:
2020-07-04 16:42:56 +02:00
return importColumn(s.notificationUpdate(item.title), context);
case RefreshState.error:
2020-07-04 16:42:56 +02:00
return importColumn(
s.notificationUpdateError(item.title), context);
default:
return Center();
}
},
)
],
2020-02-11 14:01:57 +01:00
);
2020-02-09 13:29:09 +01:00
}
}