1
0
mirror of https://github.com/stonega/tsacdop synced 2025-01-19 02:12:12 +01:00
tsacdop-podcast-app-android/lib/class/podcast_group.dart
stonegate 9d4bbc895a modified: .circleci/config.yml
modified:   lib/class/episodebrief.dart
	modified:   lib/class/podcast_group.dart
	deleted:    lib/class/podcastrss.dart
	deleted:    lib/class/podcastrss.g.dart
	deleted:    lib/class/podcasts.dart
	deleted:    lib/class/podcasts.g.dart
	modified:   lib/episodes/episodedetail.dart
	modified:   lib/home/appbar/about.dart
	modified:   lib/home/appbar/addpodcast.dart
	modified:   lib/home/appbar/popupmenu.dart
	modified:   lib/home/homescroll.dart
	modified:   lib/local_storage/sqflite_localpodcast.dart
	modified:   lib/main.dart
	modified:   lib/podcasts/podcastdetail.dart
	modified:   lib/podcasts/podcastgroup.dart
	modified:   lib/podcasts/podcastlist.dart
	modified:   lib/podcasts/podcastmanage.dart
	modified:   lib/util/episodegrid.dart
	modified:   lib/webfeed/domain/rss_itunes.dart
	modified:   pubspec.lock
	modified:   pubspec.yaml
2020-02-21 23:04:02 +08:00

179 lines
4.6 KiB
Dart

import 'dart:collection';
import 'dart:core';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:tsacdop/local_storage/key_value_storage.dart';
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
import 'package:uuid/uuid.dart';
import 'package:tsacdop/class/podcastlocal.dart';
class GroupEntity {
final String name;
final String id;
final String color;
final List<String> podcastList;
GroupEntity(this.name, this.id, this.color, this.podcastList);
Map<String, Object> toJson() {
return {'name': name, 'id': id, 'color': color, 'podcastList': podcastList};
}
static GroupEntity fromJson(Map<String, Object> json) {
List<String> list = List.from(json['podcastList']);
print(json['[podcastList']);
return GroupEntity(json['name'] as String, json['id'] as String,
json['color'] as String, list);
}
}
class PodcastGroup {
final String name;
final String id;
final String color;
List<String> podcastList;
PodcastGroup(this.name,
{this.color = '#000000', String id, List<String> podcastList})
: id = id ?? Uuid().v4(),
podcastList = podcastList ?? [];
Future getPodcasts() async {
var dbHelper = DBHelper();
if (podcastList != []) {
_podcasts = await dbHelper.getPodcastLocal(podcastList);
}
}
List<PodcastLocal> _podcasts;
List<PodcastLocal> get podcasts => _podcasts;
GroupEntity toEntity() {
return GroupEntity(name, id, color, podcastList);
}
static PodcastGroup fromEntity(GroupEntity entity) {
return PodcastGroup(
entity.name,
id: entity.id,
color: entity.color,
podcastList: entity.podcastList,
);
}
}
class GroupList extends ChangeNotifier {
List<PodcastGroup> _groups;
DBHelper dbHelper = DBHelper();
UnmodifiableListView<PodcastGroup> get groups =>
UnmodifiableListView(_groups);
KeyValueStorage storage = KeyValueStorage('groups');
GroupList({List<PodcastGroup> groups}) : _groups = groups ?? [];
bool _isLoading = false;
bool get isLoading => _isLoading;
@override
void addListener(VoidCallback listener) {
super.addListener(listener);
loadGroups();
}
Future loadGroups() async {
_isLoading = true;
notifyListeners();
storage.getGroups().then((loadgroups) async {
_groups.addAll(loadgroups.map((e) => PodcastGroup.fromEntity(e)));
await Future.forEach(_groups, (group) async {
await group.getPodcasts();
});
_isLoading = false;
notifyListeners();
});
}
Future addGroup(PodcastGroup podcastGroup) async {
_groups.add(podcastGroup);
_saveGroup();
notifyListeners();
}
Future delGroup(PodcastGroup podcastGroup) async {
_groups.remove(podcastGroup);
notifyListeners();
_saveGroup();
}
void updateGroup(PodcastGroup podcastGroup) {
var oldGroup = _groups.firstWhere((it) => it.id == podcastGroup.id);
var index = _groups.indexOf(oldGroup);
_groups.replaceRange(index, index + 1, [podcastGroup]);
notifyListeners();
_saveGroup();
}
void _saveGroup() {
storage.saveGroup(_groups.map((it) => it.toEntity()).toList());
}
Future subscribe(PodcastLocal podcastLocal) async {
_groups[0].podcastList.insert(0, podcastLocal.id);
_saveGroup();
await dbHelper.savePodcastLocal(podcastLocal);
await _groups[0].getPodcasts();
notifyListeners();
}
List<PodcastGroup> getPodcastGroup(String id) {
List<PodcastGroup> result = [];
_groups.forEach((group) {
if (group.podcastList.contains(id)) {
result.add(group);
}
});
return result;
}
changeGroup(String id, List<PodcastGroup> list) async {
_isLoading = true;
notifyListeners();
getPodcastGroup(id).forEach((group) {
group.podcastList.remove(id);
});
list.forEach((s) {
s.podcastList.insert(0, id);
});
_saveGroup();
await Future.forEach(_groups, (group) async {
await group.getPodcasts();
});
_isLoading = false;
notifyListeners();
}
removePodcast(String id) async {
_isLoading = true;
notifyListeners();
_groups.forEach((group) async {
group.podcastList.remove(id);
});
_saveGroup();
await dbHelper.delPodcastLocal(id);
await Future.forEach(_groups, (group) async {
await group.getPodcasts();
});
_isLoading = false;
notifyListeners();
}
saveOrder(PodcastGroup group, List<PodcastLocal> podcasts) async {
group.podcastList = podcasts.map((e) => e.id).toList();
_saveGroup();
await group.getPodcasts();
notifyListeners();
}
}