OpenMediacenterMobileFlutter/lib/utils/feature_context.dart

93 lines
2.6 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
2022-10-20 07:28:14 +00:00
import 'package:openmediacentermobile/login/login_context.dart';
import '../api/settings_api.dart';
// todo maybe instead of feature context a context for all settings?
class FeatureContext extends InheritedWidget {
const FeatureContext(this.tvShowEnabled, this.fullDeleteEnabled,
{Key? key, required Widget child})
: super(key: key, child: child);
final bool tvShowEnabled;
final bool fullDeleteEnabled;
static FeatureContext of(BuildContext context) {
final FeatureContext? result =
context.dependOnInheritedWidgetOfExactType<FeatureContext>();
assert(result != null, 'No LoginContext found in context');
return result!;
}
@override
bool updateShouldNotify(FeatureContext old) {
return tvShowEnabled != old.tvShowEnabled ||
fullDeleteEnabled != old.fullDeleteEnabled;
}
}
class FeatureContainer extends StatefulWidget {
const FeatureContainer({Key? key, required this.child}) : super(key: key);
final Widget child;
@override
State<FeatureContainer> createState() => _FeatureContainerState();
}
class _FeatureContainerState extends State<FeatureContainer> {
bool? tvShowEnabled;
bool? fullDeleteEnabled;
2022-10-20 07:28:14 +00:00
bool fetcherror = false;
@override
void initState() {
2022-10-20 07:28:14 +00:00
loadInitialData()
.then((value) => setState(
() {
fullDeleteEnabled = value.fullDeleteEnabled;
tvShowEnabled = value.tvShowEnabled;
},
))
.catchError((err) => setState(() => fetcherror = true));
super.initState();
}
@override
Widget build(BuildContext context) {
2022-10-20 07:28:14 +00:00
if (fetcherror) {
final loginctx = LoginContext.of(context);
return MaterialApp(
home: Scaffold(
body: Center(
child:
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text("Fetch error"),
MaterialButton(
onPressed: () => loginctx.onLoggin(false),
child: Text("Logout"),
)
]),
),
),
);
} else if (tvShowEnabled == null || fullDeleteEnabled == null) {
return MaterialApp(
2022-10-20 07:28:14 +00:00
home: Scaffold(
body: Container(
color: Colors.white,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [CircularProgressIndicator(), Text("loading features")],
)),
),
),
);
} else {
return FeatureContext(tvShowEnabled!, fullDeleteEnabled!,
child: widget.child);
}
}
}