Skip to content

Commit

Permalink
Remove FocusProvider
Browse files Browse the repository at this point in the history
- Rework Observer system from chips
  • Loading branch information
LuanRoger committed Oct 23, 2022
1 parent e2633cd commit b3f1526
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 112 deletions.
3 changes: 0 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';
import 'package:window_manager/window_manager.dart';
import 'package:xbox_launcher/controllers/xinput_controller.dart';
import 'package:xbox_launcher/providers/focus_element_provider.dart';
import 'package:xbox_launcher/providers/keyboard_action_provider.dart';
import 'package:xbox_launcher/providers/profile_provider.dart';
import 'package:xbox_launcher/providers/controller_action_provider.dart';
Expand All @@ -24,8 +23,6 @@ void main() async {
ChangeNotifierProvider<ProfileProvider>(
create: (_) => profileProvider,
),
ChangeNotifierProvider<FocusElementProvider>(
create: (_) => FocusElementProvider(null)),
Provider<ControllerActionProvider>(
create: (_) => ControllerActionProvider(xInputController)),
Provider(create: (_) => KeyboardActionProvider())
Expand Down
40 changes: 0 additions & 40 deletions lib/pages/my_library_page/my_library_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:xbox_launcher/pages/my_library_page/sections/full_library_sectio
import 'package:xbox_launcher/pages/my_library_page/sections/manage_section.dart';
import 'package:xbox_launcher/pages/my_library_page/sections/my_apps_section.dart';
import 'package:xbox_launcher/pages/my_library_page/sections/my_games_section.dart';
import 'package:xbox_launcher/providers/focus_element_provider.dart';
import 'package:xbox_launcher/shared/widgets/buttons/text_button.dart';
import 'package:xbox_launcher/shared/widgets/dialogs/context_menu/context_menu.dart';
import 'package:xbox_launcher/shared/widgets/dialogs/context_menu/context_menu_add_group.dart';
Expand Down Expand Up @@ -46,45 +45,6 @@ class _MyGamesPageState extends XboxPageState<MyLibraryPage> {
const SingleActivator(LogicalKeyboardKey.escape),
ControllerButton.B_BUTTON),
action: (context) => Navigator.pop(context)),
ShortcutOption("More options",
controllerKeyboardPair: ControllerKeyboardPair(
const SingleActivator(LogicalKeyboardKey.f1),
ControllerButton.BACK),
action: (context) => ContextMenu("Options", contextItems: [
ContextMenuItem("Add to a group", icon: FluentIcons.add,
onPressed: () {
Object? focusObject =
context.read<FocusElementProvider>().currentValue;
if (focusObject == null && focusObject is! AppModel) return;

AppModel appModel = focusObject as AppModel;
ContextMenuAddGroup(appModel.name, appModel: appModel)
.show(context);
}),
ContextMenuItem("See on Microsoft Store",
icon: FluentIcons.store_logo16, onPressed: () async {
Object? focusObject =
context.read<FocusElementProvider>().currentValue;
if (focusObject == null && focusObject is! AppModel) return;

GameModel gameModel = focusObject as GameModel;
await SystemDialog(
title: "Access a external site.",
content: "Do you want to go to a external site?",
actions: [
TextButton(
title: "Confirm",
onPressed: () async {
await launchUrl(Uri.parse(gameModel.storeUrl));
Navigator.pop(context);
}),
TextButton(
title: "Cancel",
onPressed: () => Navigator.pop(context))
],
).show(context);
}),
]).show(context))
];

@override
Expand Down
24 changes: 0 additions & 24 deletions lib/providers/focus_element_provider.dart

This file was deleted.

14 changes: 6 additions & 8 deletions lib/shared/widgets/chip/chip_base.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';
import 'package:xbox_launcher/providers/profile_provider.dart';
import 'package:xbox_launcher/shared/widgets/chip/chip_notify_check.dart';
import 'package:xbox_launcher/shared/widgets/chip/chip_notify_observer.dart';
import 'package:xbox_launcher/shared/widgets/utils/observers/chip_notify_check.dart';
import 'package:xbox_launcher/shared/widgets/utils/observers/observer.dart';

abstract class ChipBase extends StatelessWidget implements ChipNotifyCheck {
late bool isSelected;
Expand All @@ -12,9 +12,9 @@ abstract class ChipBase extends StatelessWidget implements ChipNotifyCheck {
double? width;

@override
late ChipNotifyObserver observer;
late Observer observer;
@override
void notifyObserver() => observer.notificationReact(this, value);
void notifyObserver() => observer.react(this, value);

void Function(void Function())? _rebuildChip;
void rebuildChip(void Function() newStateChipCallback) =>
Expand Down Expand Up @@ -42,12 +42,10 @@ abstract class ChipBase extends StatelessWidget implements ChipNotifyCheck {
height: height,
child: Button(
child: buttonChipChild(context),
onPressed: () {
onPressed: () => _rebuildChip?.call(() {
isSelected = !isSelected;
onCheck?.call(isSelected, value);
notifyObserver();
_rebuildChip?.call(() {});
},
}),
style: ButtonStyle(
padding: ButtonState.all(const EdgeInsets.all(13.0)),
backgroundColor: isSelected
Expand Down
6 changes: 0 additions & 6 deletions lib/shared/widgets/chip/chip_notify_check.dart

This file was deleted.

5 changes: 0 additions & 5 deletions lib/shared/widgets/chip/chip_notify_observer.dart

This file was deleted.

13 changes: 6 additions & 7 deletions lib/shared/widgets/chip/chip_row.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:xbox_launcher/shared/widgets/chip/chip_base.dart';
import 'package:xbox_launcher/shared/widgets/chip/chip_notify_observer.dart';
import 'package:collection/collection.dart';
import 'package:xbox_launcher/shared/widgets/utils/observers/observer.dart';

class ChipsRow extends StatefulWidget {
final List<ChipBase> chips;
Expand All @@ -14,28 +14,27 @@ class ChipsRow extends StatefulWidget {
State<ChipsRow> createState() => _ChipsRowState();
}

class _ChipsRowState extends State<ChipsRow> implements ChipNotifyObserver {
class _ChipsRowState extends State<ChipsRow> implements Observer {
late List<ChipBase> chips;

@override
void initState() {
super.initState();
chips = List.from(widget.chips);
// ignore: avoid_function_literals_in_foreach_calls
chips.forEach((chip) {
for (var chip in chips) {
chip.observer = this;
chip.onCheck = widget.onCheckChange;
});
}
}

@override
void notificationReact(ChipBase sender, Object? payload) {
void react(Object sender, Object? payload) {
ChipBase? currentSelected =
chips.firstWhereOrNull((chip) => chip != sender && chip.isSelected);
currentSelected?.rebuildChip(() {
currentSelected.isSelected = false;
});
widget.onCheckChange(sender.isSelected, payload);
widget.onCheckChange((sender as ChipBase).isSelected, payload);
}

@override
Expand Down
18 changes: 6 additions & 12 deletions lib/shared/widgets/tiles/button_tile.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';
import 'package:xbox_launcher/models/app_badge_info.dart';
import 'package:xbox_launcher/providers/focus_element_provider.dart';
import 'package:xbox_launcher/shared/enums/tile_size.dart';
import 'package:xbox_launcher/shared/widgets/tiles/tile_badges.dart';
import 'package:xbox_launcher/shared/widgets/tiles/tile_base_stateful.dart';
Expand All @@ -16,6 +14,7 @@ class ButtonTile extends TileBaseStateful {
final Widget? customCover;
final ImageProvider? image;
final void Function(BuildContext)? onPressed;
final FocusNode? focusNode;

@override
final Color? color;
Expand All @@ -40,7 +39,8 @@ class ButtonTile extends TileBaseStateful {
this.onPressed,
this.icon,
this.customCover,
this.image})
this.image,
this.focusNode})
: super(key: key, objectInfoSender: objectInfoSender) {
_tileSize = tileSize;
switch (tileSize) {
Expand All @@ -65,15 +65,17 @@ class ButtonTile extends TileBaseStateful {
}

class _ButtonTileState extends TileBaseStatefulState<ButtonTile> {
final FocusNode focusNode = FocusNode();
late bool getFocused;
TileTitleBar? _titleBar;
TileBadges? _tileBadges;
late final FocusNode focusNode;

@override
void initState() {
super.initState();

focusNode = widget.focusNode ?? FocusNode();

if (widget.appBadgeInfo != null) {
_tileBadges = TileBadges(widget.appBadgeInfo!);
}
Expand All @@ -87,14 +89,6 @@ class _ButtonTileState extends TileBaseStatefulState<ButtonTile> {
: null;
});
});
focusNode.addListener(widget.objectInfoSender != null
? () {
Provider.of<FocusElementProvider>(context, listen: false)
.setCurrentValue(this, widget.objectInfoSender!);
}
: () {
Provider.of<FocusElementProvider>(context, listen: false).clear();
});
}

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/shared/widgets/tiles/game_button_tile.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:xbox_launcher/models/app_models/game_model.dart';
import 'package:xbox_launcher/shared/enums/tile_size.dart';
import 'package:xbox_launcher/shared/widgets/commands/models/command_invoker.dart';
import 'package:xbox_launcher/shared/widgets/commands/open_app_command.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/models/command_invoker.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/open_app_command.dart';
import 'package:xbox_launcher/shared/widgets/tiles/button_tile.dart';

class GameButtonTile extends ButtonTile {
Expand Down
4 changes: 2 additions & 2 deletions lib/shared/widgets/tiles/system_app_tile.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:xbox_launcher/models/app_models/system_app_model.dart';
import 'package:xbox_launcher/shared/enums/tile_size.dart';
import 'package:xbox_launcher/shared/widgets/commands/models/command_invoker.dart';
import 'package:xbox_launcher/shared/widgets/commands/open_app_command.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/models/command_invoker.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/open_app_command.dart';
import 'package:xbox_launcher/shared/widgets/tiles/button_tile.dart';

class SystemAppButtonTile extends ButtonTile {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:xbox_launcher/shared/widgets/commands/models/shared_command.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/models/shared_command.dart';

class CommandInvoker {
final SharedCommand _command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:xbox_launcher/models/app_models/system_app_model.dart';
import 'package:xbox_launcher/providers/profile_provider.dart';
import 'package:xbox_launcher/routes/app_routes.dart';
import 'package:xbox_launcher/shared/enums/app_type.dart';
import 'package:xbox_launcher/shared/widgets/commands/models/shared_command.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/models/shared_command.dart';

class OpenAppCommand implements SharedCommand {
AppModel appModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:xbox_launcher/shared/widgets/commands/models/shared_command.dart';
import 'package:xbox_launcher/shared/widgets/utils/commands/models/shared_command.dart';

class PopContextCommand implements SharedCommand {
final BuildContext _context;
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/widgets/utils/observers/chip_notify_check.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:xbox_launcher/shared/widgets/utils/observers/observer.dart';
import 'package:xbox_launcher/shared/widgets/utils/observers/observer_notifier.dart';

abstract class ChipNotifyCheck implements ObserverNotifier {
late Observer observer;
@override
void notifyObserver();
}
3 changes: 3 additions & 0 deletions lib/shared/widgets/utils/observers/observer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class Observer {
void react(Object sender, Object? payload);
}
3 changes: 3 additions & 0 deletions lib/shared/widgets/utils/observers/observer_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class ObserverNotifier {
void notifyObserver();
}

0 comments on commit b3f1526

Please sign in to comment.