Skip to content

Commit

Permalink
feat: update home tab navigation (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinenerio authored Dec 10, 2024
1 parent 50fa213 commit f83f824
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import '../../../l10n/l10n.dart';
import '../../../ui/app_bar.dart';
import '../../../ui/colors.dart';
import '../../../ui/tab_bar.dart';
import '../../router/models/activities_tab.dart';
import '../widgets/pending_activities_list.dart';
import '../widgets/transaction_list.dart';

enum ActivitiesTab { pending, transactions }

class ActivitiesScreen extends StatefulWidget {
const ActivitiesScreen({
super.key,
Expand All @@ -34,8 +33,9 @@ class _ActivitiesScreenState extends State<ActivitiesScreen>
@override
void didUpdateWidget(covariant ActivitiesScreen oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.initialTab != widget.initialTab) {
_controller.index = widget.initialTab.index;

if (_controller.index != widget.initialTab.index) {
_controller.animateTo(widget.initialTab.index);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,63 @@ import '../../dynamic_links/services/dynamic_links_notifier.dart';
import '../../incoming_link_payments/widgets/pending_ilp_listener.dart';
import '../../outgoing_direct_payments/widgets/link_listener.dart';
import '../../ramp/partners/coinflow/widgets/coinflow_link_listener.dart';
import '../../router/models/activities_tab.dart';
import '../../router/service/navigation_service.dart';
import '../../transaction_request/widgets/tr_link_listener.dart';
import '../../wallet_flow/screens/wallet_screen.dart';
import 'main_screen.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});

static void openWalletTab(BuildContext context) {
final _HomeScreenState? state =
context.findAncestorStateOfType<_HomeScreenState>();
if (state != null) {
state._pageController.jumpToPage(1);
state._tabNotifier.value = 1;
}
}
static void openWalletTab(BuildContext context) =>
sl<HomeNavigationService>().openWalletTab(context);

static void openActivitiesTab(
BuildContext context, {
// ignore: avoid-unused-parameters, fix later
ActivitiesTab tab = ActivitiesTab.pending,
}) {
final _HomeScreenState? state =
context.findAncestorStateOfType<_HomeScreenState>();
if (state != null) {
state._pageController.jumpToPage(2);
state._tabNotifier.value = 2;
}
}
}) =>
sl<HomeNavigationService>().openActivitiesTab(context, tab: tab);

@override
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
final _tabNotifier = TabNotifier();
final _pageController = PageController();

@override
void initState() {
super.initState();
sl<HomeNavigationService>().tabNotifier.addListener(_handleGlobalTabChange);
}

void _handleGlobalTabChange() {
if (mounted) {
_pageController.jumpToPage(sl<HomeNavigationService>().tabNotifier.value);
}
}

@override
void dispose() {
_tabNotifier.dispose();
sl<HomeNavigationService>()
.tabNotifier
.removeListener(_handleGlobalTabChange);
_pageController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) => ChangeNotifierProvider.value(
value: _tabNotifier,
value: sl<HomeNavigationService>().tabNotifier,
child: LinkLoader(
child: ODPLinkListener(
child: PendingILPListener(
child: TRLinkListener(
child: CoinflowLinkListener(
child: AmbassadorLinkListener(
child: ValueListenableBuilder(
valueListenable: _tabNotifier,
valueListenable: sl<HomeNavigationService>().tabNotifier,
builder: (context, value, _) => Scaffold(
backgroundColor: Colors.white,
extendBody: true,
Expand All @@ -85,7 +87,9 @@ class _HomeScreenState extends State<HomeScreen> {
icon: p.icon,
active: value == i,
onPressed: () {
_tabNotifier.value = i;
sl<HomeNavigationService>()
.tabNotifier
.value = i;
_pageController.jumpToPage(i);
},
),
Expand Down Expand Up @@ -122,10 +126,6 @@ class LinkLoader extends StatelessWidget {
);
}

class TabNotifier extends ValueNotifier<int> {
TabNotifier() : super(0);
}

// ignore: avoid-function-type-in-records, fix later
final List<({SvgGenImage icon, WidgetBuilder builder})> _pages = [
(
Expand All @@ -147,7 +147,7 @@ final List<({SvgGenImage icon, WidgetBuilder builder})> _pages = [
(
icon: Assets.icons.notifications,
builder: (context) => ActivitiesScreen(
initialTab: ActivitiesTab.pending,
initialTab: sl<HomeNavigationService>().activitiesTabNotifier.value,
onSendMoneyPressed: () => HomeScreen.openWalletTab(context),
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum ActivitiesTab { pending, transactions }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:injectable/injectable.dart';

import '../../accounts/auth_scope.dart';
import '../models/activities_tab.dart';

@Singleton(scope: authScope)
class HomeNavigationService {
final tabNotifier = ValueNotifier<int>(0);
final activitiesTabNotifier =
ValueNotifier<ActivitiesTab>(ActivitiesTab.pending);

void openWalletTab(BuildContext context) {
tabNotifier.value = 1;
Navigator.of(context).popUntil((route) => route.isFirst);
}

void openActivitiesTab(
BuildContext context, {
ActivitiesTab tab = ActivitiesTab.pending,
}) {
tabNotifier.value = 2;
activitiesTabNotifier.value = tab;
Navigator.of(context).popUntil((route) => route.isFirst);
}
}

0 comments on commit f83f824

Please sign in to comment.