Migrate Lumen to Dart and Flutter 2.0
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
class Ads {
|
||||
Ads({this.enabled = true});
|
||||
final bool enabled;
|
||||
RewardedAd? _reward;
|
||||
InterstitialAd? _interstitial;
|
||||
bool _started = false,
|
||||
_rewardLoading = false,
|
||||
_interstitialLoading = false,
|
||||
_disposed = false;
|
||||
DateTime? _rewardAt, _interstitialAt;
|
||||
bool get supported =>
|
||||
enabled &&
|
||||
!kIsWeb &&
|
||||
(defaultTargetPlatform == TargetPlatform.android ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS);
|
||||
Future<void> start() async {
|
||||
if (!supported) return;
|
||||
ConsentInformation.instance.requestConsentInfoUpdate(
|
||||
ConsentRequestParameters(),
|
||||
() {
|
||||
ConsentForm.loadAndShowConsentFormIfRequired((_) => _initialize());
|
||||
},
|
||||
(_) => _initialize(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _initialize() async {
|
||||
if (_disposed ||
|
||||
_started ||
|
||||
!await ConsentInformation.instance.canRequestAds()) {
|
||||
return;
|
||||
}
|
||||
_started = true;
|
||||
await MobileAds.instance.initialize();
|
||||
await _load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
if (_disposed ||
|
||||
!supported ||
|
||||
!await ConsentInformation.instance.canRequestAds()) {
|
||||
return;
|
||||
}
|
||||
final ios = defaultTargetPlatform == TargetPlatform.iOS;
|
||||
if (_reward == null && !_rewardLoading) {
|
||||
_rewardLoading = true;
|
||||
RewardedAd.load(
|
||||
adUnitId: const String.fromEnvironment('ADMOB_REWARDED_ID').isNotEmpty
|
||||
? const String.fromEnvironment('ADMOB_REWARDED_ID')
|
||||
: ios
|
||||
? 'ca-app-pub-3940256099942544/1712485313'
|
||||
: 'ca-app-pub-3940256099942544/5224354917',
|
||||
request: const AdRequest(),
|
||||
rewardedAdLoadCallback: RewardedAdLoadCallback(
|
||||
onAdLoaded: (a) {
|
||||
_rewardLoading = false;
|
||||
if (_disposed) {
|
||||
a.dispose();
|
||||
return;
|
||||
}
|
||||
_reward = a;
|
||||
_rewardAt = DateTime.now();
|
||||
},
|
||||
onAdFailedToLoad: (_) {
|
||||
_rewardLoading = false;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
if (_interstitial == null && !_interstitialLoading) {
|
||||
_interstitialLoading = true;
|
||||
InterstitialAd.load(
|
||||
adUnitId:
|
||||
const String.fromEnvironment('ADMOB_INTERSTITIAL_ID').isNotEmpty
|
||||
? const String.fromEnvironment('ADMOB_INTERSTITIAL_ID')
|
||||
: ios
|
||||
? 'ca-app-pub-3940256099942544/4411468910'
|
||||
: 'ca-app-pub-3940256099942544/1033173712',
|
||||
request: const AdRequest(),
|
||||
adLoadCallback: InterstitialAdLoadCallback(
|
||||
onAdLoaded: (a) {
|
||||
_interstitialLoading = false;
|
||||
if (_disposed) {
|
||||
a.dispose();
|
||||
return;
|
||||
}
|
||||
_interstitial = a;
|
||||
_interstitialAt = DateTime.now();
|
||||
},
|
||||
onAdFailedToLoad: (_) {
|
||||
_interstitialLoading = false;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> hint(VoidCallback earned) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
return false;
|
||||
}
|
||||
final a = _reward;
|
||||
_reward = null;
|
||||
if (a == null ||
|
||||
_rewardAt == null ||
|
||||
DateTime.now().difference(_rewardAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
unawaited(_load());
|
||||
return false;
|
||||
}
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, e) {
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
);
|
||||
a.show(
|
||||
onUserEarnedReward: (ad, reward) {
|
||||
earned();
|
||||
},
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> between(VoidCallback next) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
final a = _interstitial;
|
||||
_interstitial = null;
|
||||
if (a == null ||
|
||||
_interstitialAt == null ||
|
||||
DateTime.now().difference(_interstitialAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
next();
|
||||
unawaited(_load());
|
||||
return;
|
||||
}
|
||||
var routed = false;
|
||||
void finish() {
|
||||
if (routed) return;
|
||||
routed = true;
|
||||
a.dispose();
|
||||
next();
|
||||
_load();
|
||||
}
|
||||
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (_) => finish(),
|
||||
onAdFailedToShowFullScreenContent: (_, e) => finish(),
|
||||
);
|
||||
a.show();
|
||||
}
|
||||
|
||||
Future<bool> privacy() async {
|
||||
if (!supported) return false;
|
||||
if (await ConsentInformation.instance
|
||||
.getPrivacyOptionsRequirementStatus() !=
|
||||
PrivacyOptionsRequirementStatus.required) {
|
||||
return false;
|
||||
}
|
||||
ConsentForm.showPrivacyOptionsForm((_) {});
|
||||
return true;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_reward?.dispose();
|
||||
_interstitial?.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import 'package:audio_session/audio_session.dart';
|
||||
|
||||
class Narrator extends ChangeNotifier {
|
||||
Narrator(this.tracks) {
|
||||
_subscriptions.add(
|
||||
_player.playerStateStream.listen((_) => notifyListeners()),
|
||||
);
|
||||
_subscriptions.add(_player.positionStream.listen((_) => notifyListeners()));
|
||||
}
|
||||
final Map<String, dynamic> tracks;
|
||||
final _player = AudioPlayer();
|
||||
final List<StreamSubscription<dynamic>> _subscriptions = [];
|
||||
String page = '', error = '';
|
||||
bool loading = false;
|
||||
int _generation = 0;
|
||||
bool get playing =>
|
||||
_player.playing && _player.processingState != ProcessingState.completed;
|
||||
bool get finished => _player.processingState == ProcessingState.completed;
|
||||
bool get paused => page.isNotEmpty && !playing && !finished && !loading;
|
||||
Duration get position => _player.position;
|
||||
String get label => loading
|
||||
? 'Opening recording…'
|
||||
: playing
|
||||
? 'Pause recording'
|
||||
: finished
|
||||
? 'Replay recording'
|
||||
: paused
|
||||
? 'Resume recording'
|
||||
: 'Play this reading';
|
||||
String _time(Duration d) =>
|
||||
'${d.inMinutes}:${(d.inSeconds % 60).toString().padLeft(2, '0')}';
|
||||
String get status => error.isNotEmpty
|
||||
? error
|
||||
: page.isEmpty
|
||||
? 'A recorded reading, kept with this page'
|
||||
: finished
|
||||
? 'End of the reading · listen again whenever you like'
|
||||
: '${playing ? 'Playing' : 'Paused'} · ${_time(position)} / ${_time(_player.duration ?? Duration.zero)}';
|
||||
String get passage {
|
||||
if (page.isEmpty || finished) return '';
|
||||
for (final cue in tracks[page]?['cues'] ?? []) {
|
||||
if (position.inMilliseconds >= cue['startMs'] &&
|
||||
position.inMilliseconds < cue['endMs']) {
|
||||
return cue['text'];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
Future<void> toggle(String id, double speed) async {
|
||||
if (loading) return;
|
||||
if (playing && page == id) {
|
||||
await _player.pause();
|
||||
return;
|
||||
}
|
||||
final generation = ++_generation;
|
||||
try {
|
||||
if (page != id) {
|
||||
loading = true;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
if (generation != _generation) return;
|
||||
page = id;
|
||||
await _player.setAsset('assets/audio/${tracks[id]['file']}');
|
||||
if (generation != _generation) return;
|
||||
} else if (finished) {
|
||||
await _player.seek(Duration.zero);
|
||||
}
|
||||
await _player.setSpeed(speed);
|
||||
if (generation != _generation) return;
|
||||
loading = false;
|
||||
notifyListeners();
|
||||
unawaited(
|
||||
_player.play().catchError((Object e) {
|
||||
error = 'This recording could not be played. You can still read the entry.';
|
||||
notifyListeners();
|
||||
}),
|
||||
);
|
||||
} catch (e) {
|
||||
if (generation == _generation) {
|
||||
error =
|
||||
'This recording could not be played. You can still read the entry.';
|
||||
loading = false;
|
||||
page = '';
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> pause() async {
|
||||
await _player.pause();
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
++_generation;
|
||||
page = '';
|
||||
loading = false;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
await _player.stop();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final s in _subscriptions) {
|
||||
unawaited(s.cancel());
|
||||
}
|
||||
unawaited(_player.dispose());
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Flutter has its own store; the former Android `lumen.xml` is never imported.
|
||||
class Progress {
|
||||
Progress._(this._disk, this._values);
|
||||
final SharedPreferencesAsync? _disk;
|
||||
final Map<String, Object> _values;
|
||||
static Future<Progress> load() async {
|
||||
final disk = SharedPreferencesAsync();
|
||||
return Progress._(disk, (await disk.getAll()).cast<String, Object>());
|
||||
}
|
||||
|
||||
factory Progress.memory([Map<String, Object> values = const {}]) =>
|
||||
Progress._(null, Map.of(values));
|
||||
int number(String key, [int fallback = 0]) =>
|
||||
(_values[key] as num?)?.toInt() ?? fallback;
|
||||
double decimal(String key, [double fallback = 1]) =>
|
||||
(_values[key] as num?)?.toDouble() ?? fallback;
|
||||
bool flag(String key) => _values[key] == true;
|
||||
String text(String key, [String fallback = '']) =>
|
||||
_values[key] as String? ?? fallback;
|
||||
bool contains(String key) => _values.containsKey(key);
|
||||
Map<String, Object> get snapshot => Map.unmodifiable(_values);
|
||||
Future<void> _writes = Future.value();
|
||||
Future<void> set(String key, Object value) {
|
||||
_values[key] = value;
|
||||
final disk = _disk;
|
||||
if (disk == null) return Future.value();
|
||||
_writes = _writes.then((_) async {
|
||||
switch (value) {
|
||||
case bool v:
|
||||
await disk.setBool(key, v);
|
||||
case int v:
|
||||
await disk.setInt(key, v);
|
||||
case double v:
|
||||
await disk.setDouble(key, v);
|
||||
case String v:
|
||||
await disk.setString(key, v);
|
||||
default:
|
||||
throw ArgumentError('Unsupported preference');
|
||||
}
|
||||
});
|
||||
return _writes;
|
||||
}
|
||||
|
||||
Future<void> get flushed => _writes;
|
||||
}
|
||||
Reference in New Issue
Block a user