-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
299 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
abstract class TaskBasic { | ||
late Function? callback; | ||
startUp({Function? callback}); | ||
} |
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,46 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
import 'package:miofeed/tasks/subscribe_update.dart'; | ||
|
||
class AndroidSubscribeUpdateTask { | ||
@pragma('vm:entry-point') | ||
static void backgroundFetchHeadlessTask(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
if (isTimeout) { | ||
// This task has exceeded its allowed running-time. | ||
// You must stop what you're doing and immediately .finish(taskId) | ||
print("[BackgroundFetch] Headless task timed-out: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
return; | ||
} | ||
print('[BackgroundFetch] Headless event received.'); | ||
// Do your work here... | ||
await SubscribeUpdateTask.run(); | ||
BackgroundFetch.finish(taskId); | ||
} | ||
} | ||
|
||
// void _onClickEnable(enabled) { | ||
// setState(() { | ||
// _enabled = enabled; | ||
// }); | ||
// if (enabled) { | ||
// BackgroundFetch.start().then((int status) { | ||
// print('[BackgroundFetch] start success: $status'); | ||
// }).catchError((e) { | ||
// print('[BackgroundFetch] start FAILURE: $e'); | ||
// }); | ||
// } else { | ||
// BackgroundFetch.stop().then((int status) { | ||
// print('[BackgroundFetch] stop success: $status'); | ||
// }); | ||
// } | ||
// } | ||
// | ||
// void _onClickStatus() async { | ||
// int status = await BackgroundFetch.status; | ||
// print('[BackgroundFetch] status: $status'); | ||
// setState(() { | ||
// _status = status; | ||
// }); | ||
// } |
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 |
---|---|---|
@@ -1,44 +1,78 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
|
||
class SubscribeUpdateTask { | ||
@pragma('vm:entry-point') | ||
static void backgroundFetchHeadlessTask(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
if (isTimeout) { | ||
// This task has exceeded its allowed running-time. | ||
// You must stop what you're doing and immediately .finish(taskId) | ||
print("[BackgroundFetch] Headless task timed-out: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
return; | ||
import 'package:dart_rss/domain/atom_feed.dart'; | ||
import 'package:dart_rss/domain/rss1_feed.dart'; | ||
import 'package:dart_rss/domain/rss_feed.dart'; | ||
import 'package:miofeed/models/task_basic.dart'; | ||
import 'package:miofeed/storages/rss/rss_storage.dart'; | ||
|
||
import '../models/rss.dart'; | ||
import '../models/universal_feed.dart'; | ||
import '../storages/rss/rss_cache.dart'; | ||
import '../utils/network/get_rss.dart'; | ||
|
||
class SubscribeUpdateTask extends TaskBasic { | ||
|
||
static final _storage = RssStorage(); | ||
|
||
static Future<void> run() async { | ||
final list = await _storage.getRssList() ?? []; | ||
for (var sub in list) { | ||
RSS data = await _storage.getRss(sub); | ||
|
||
// 如果没开自动更新直接跳过 | ||
if (!data.autoUpdate) break; | ||
|
||
print('Updating subscribe: $sub'); | ||
|
||
final url = data.subscribeUrl; | ||
final String res; | ||
try { | ||
res = await NetworkGetRss().get(url); | ||
} catch (e, s) { | ||
print('Update RSS sub ${data.name} failed:'); | ||
print(e); | ||
print(s); | ||
return; | ||
} | ||
//print(res); | ||
late final UniversalFeed parsed; | ||
try { | ||
final parsedData = _parse(res, data.type); | ||
if (parsedData is AtomFeed) { | ||
parsed = UniversalFeed.fromAtom(parsedData); | ||
// print(data.items.first.links.first.href); | ||
} else if (parsedData is RssFeed) { | ||
parsed = UniversalFeed.fromRss(parsedData); | ||
} else if (parsedData is Rss1Feed) { | ||
parsed = UniversalFeed.fromRss1(parsedData); | ||
} | ||
} catch (e, s) { | ||
print('Parse result for RSS sub ${data.name} failed:'); | ||
print(e); | ||
print(s); | ||
return; | ||
} | ||
data.data = parsed; | ||
// print(parsed); | ||
await RssCache.save(data, res); | ||
await RssCache.removeMemCacheBySubName(sub); | ||
await RssCache.toMemCache(data, data.data); | ||
print('Update for subscribe finished: $sub'); | ||
} | ||
} | ||
|
||
static _parse(String data, int type) { | ||
switch (type) { | ||
case 0: | ||
return AtomFeed.parse(data); | ||
case 1: | ||
return Rss1Feed.parse(data); | ||
case 2: | ||
return RssFeed.parse(data); | ||
} | ||
print('[BackgroundFetch] Headless event received.'); | ||
// Do your work here... | ||
BackgroundFetch.finish(taskId); | ||
} | ||
} | ||
|
||
// void _onClickEnable(enabled) { | ||
// setState(() { | ||
// _enabled = enabled; | ||
// }); | ||
// if (enabled) { | ||
// BackgroundFetch.start().then((int status) { | ||
// print('[BackgroundFetch] start success: $status'); | ||
// }).catchError((e) { | ||
// print('[BackgroundFetch] start FAILURE: $e'); | ||
// }); | ||
// } else { | ||
// BackgroundFetch.stop().then((int status) { | ||
// print('[BackgroundFetch] stop success: $status'); | ||
// }); | ||
// } | ||
// } | ||
// | ||
// void _onClickStatus() async { | ||
// int status = await BackgroundFetch.status; | ||
// print('[BackgroundFetch] status: $status'); | ||
// setState(() { | ||
// _status = status; | ||
// }); | ||
// } | ||
|
||
@override | ||
startUp({Function? callback}) async { | ||
await run(); | ||
} | ||
} |
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
Oops, something went wrong.