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';
|
2020-07-22 11:34:32 +02:00
|
|
|
import '../util/extension_helper.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
|
|
|
|
class Import extends StatelessWidget {
|
2020-04-18 06:48:02 +02:00
|
|
|
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);
|
2020-04-18 06:48:02 +02:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
2020-07-14 17:45:45 +02:00
|
|
|
Consumer<GroupList>(
|
2020-04-18 06:48:02 +02:00
|
|
|
builder: (_, subscribeWorker, __) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var item = subscribeWorker.currentSubscribeItem;
|
2020-04-18 06:48:02 +02:00
|
|
|
switch (item.subscribeState) {
|
|
|
|
case SubscribeState.start:
|
2020-07-04 16:42:56 +02:00
|
|
|
return importColumn(
|
|
|
|
s.notificationSubscribe(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
case SubscribeState.subscribe:
|
2020-07-04 16:42:56 +02:00
|
|
|
return importColumn(s.notificaitonFatch(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
case SubscribeState.fetch:
|
2020-07-04 16:42:56 +02:00
|
|
|
return importColumn(s.notificationSuccess(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
case SubscribeState.exist:
|
|
|
|
return importColumn(
|
2020-07-04 16:42:56 +02:00
|
|
|
s.notificationSubscribeExisted(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
case SubscribeState.error:
|
|
|
|
return importColumn(
|
2020-07-04 16:42:56 +02:00
|
|
|
s.notificationNetworkError(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
default:
|
|
|
|
return Center();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Consumer<RefreshWorker>(
|
|
|
|
builder: (context, refreshWorker, child) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var item = refreshWorker.currentRefreshItem;
|
2020-04-22 20:10:57 +02:00
|
|
|
if (refreshWorker.complete) {
|
|
|
|
groupList.updateGroups();
|
2020-06-11 17:13:10 +02:00
|
|
|
_autoDownloadNew(context);
|
2020-04-22 20:10:57 +02:00
|
|
|
}
|
2020-04-18 06:48:02 +02:00
|
|
|
switch (item.refreshState) {
|
|
|
|
case RefreshState.fetch:
|
2020-07-04 16:42:56 +02:00
|
|
|
return importColumn(s.notificationUpdate(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
case RefreshState.error:
|
2020-07-04 16:42:56 +02:00
|
|
|
return importColumn(
|
|
|
|
s.notificationUpdateError(item.title), context);
|
2020-04-18 06:48:02 +02:00
|
|
|
default:
|
|
|
|
return Center();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
2020-02-11 14:01:57 +01:00
|
|
|
);
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|