Skip to content

Commit

Permalink
fix: listener called after tabcontroller got disposed
Browse files Browse the repository at this point in the history
  • Loading branch information
squidrye committed Sep 22, 2023
1 parent 72b03dd commit 58b0a06
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:math';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'pane_node_state.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
part of 'pane_node_cubit.dart';

@freezed
class PaneNodeState extends Equatable {
final List<double> flex;
const PaneNodeState({required this.flex});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:appflowy/workspace/application/tabs/tabs.dart';
import 'package:appflowy/workspace/application/tabs/tabs_controller.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

Expand All @@ -7,22 +7,22 @@ class PaneNode extends Equatable {
final PaneNode? parent;
final Axis? axis;
final String paneId;
final Tabs tabs;
final TabsController tabs;

PaneNode({
required this.paneId,
required this.children,
this.parent,
this.axis,
Tabs? tabs,
}) : tabs = tabs ?? Tabs();
TabsController? tabs,
}) : tabs = tabs ?? TabsController();

PaneNode copyWith({
PaneNode? parent,
List<PaneNode>? children,
Axis? axis,
String? paneId,
Tabs? tabs,
TabsController? tabs,
}) {
return PaneNode(
parent: parent ?? this.parent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:appflowy/startup/plugin/plugin.dart';
import 'package:appflowy/workspace/application/panes/panes_service.dart';
import 'package:appflowy/workspace/application/tabs/tabs.dart';
import 'package:appflowy/workspace/presentation/home/panes/draggable_pane_item.dart';
import 'package:appflowy/workspace/application/tabs/tabs_controller.dart';
import 'package:appflowy/workspace/presentation/home/panes/draggable_pane_target.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -48,7 +48,6 @@ class PanesCubit extends Cubit<PanesState> {
plugin: plugin,
direction: direction,
axis: axis,
setActivePaneCallback: setActivePane,
);

emit(
Expand All @@ -57,6 +56,7 @@ class PanesCubit extends Cubit<PanesState> {
count: state.count + 1,
),
);
setActivePane(state.root.children.last);
}

void closePane({required String paneId}) {
Expand All @@ -65,11 +65,13 @@ class PanesCubit extends Cubit<PanesState> {
root: panesService.closePaneHandler(
node: state.root,
targetPaneId: paneId,
setActivePaneCallback: setActivePane,
),
count: state.count - 1,
),
);

final children = state.root.children;
setActivePane(children.isEmpty ? state.root : children.last);
}

void openTab({required Plugin plugin}) {
Expand Down Expand Up @@ -117,7 +119,6 @@ class PanesCubit extends Cubit<PanesState> {
targetPaneId: to.paneId,
direction: direction,
axis: axis,
setActivePaneCallback: setActivePane,
fromNode: from,
),
count: state.count + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PanesState extends Equatable {

factory PanesState.initial() {
final pane = PaneNode(
tabs: Tabs(),
tabs: TabsController(),
children: const [],
paneId: nanoid(),
axis: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:appflowy/startup/plugin/plugin.dart';
import 'package:appflowy/workspace/application/panes/panes.dart';
import 'package:appflowy/workspace/application/tabs/tabs.dart';
import 'package:appflowy/workspace/application/tabs/tabs_controller.dart';
import 'package:appflowy/workspace/presentation/home/home_stack.dart';
import 'package:flutter/material.dart';
import 'package:nanoid/nanoid.dart';
Expand All @@ -13,7 +13,6 @@ class PanesService {
required String targetPaneId,
required Direction direction,
required Axis axis,
required void Function(PaneNode) setActivePaneCallback,
PaneNode? fromNode,
Plugin? plugin,
}) {
Expand All @@ -32,8 +31,7 @@ class PanesService {
paneId: nanoid(),
parent: newHolderNode,
axis: null,
tabs: Tabs(
currentIndex: node.tabs.currentIndex,
tabs: TabsController(
pageManagers: node.tabs.pageManagers,
),
);
Expand All @@ -42,8 +40,7 @@ class PanesService {
paneId: nanoid(),
children: const [],
axis: null,
tabs: Tabs(
currentIndex: fromNode.tabs.currentIndex,
tabs: TabsController(
pageManagers: fromNode.tabs.pageManagers,
),
) ??
Expand All @@ -52,9 +49,8 @@ class PanesService {
children: const [],
parent: newHolderNode,
axis: null,
tabs: Tabs(
pageManagers: [PageManager()..setPlugin(plugin!)],
),
tabs: TabsController(pageManagers: [PageManager()])
..openPlugin(plugin: plugin!),
);
final ret = newHolderNode.copyWith(
children: direction == Direction.front
Expand All @@ -67,7 +63,6 @@ class PanesService {
oldChildNode,
],
);
setActivePaneCallback(ret.children[0]);
return ret;
}

Expand All @@ -78,18 +73,16 @@ class PanesService {
if (node.children[i].paneId == targetPaneId) {
final newNode = fromNode?.copyWith(
paneId: nanoid(),
tabs: Tabs(
currentIndex: fromNode.tabs.currentIndex,
tabs: TabsController(
pageManagers: fromNode.tabs.pageManagers,
),
) ??
PaneNode(
paneId: nanoid(),
children: const [],
parent: node.parent,
tabs: Tabs(
pageManagers: [PageManager()..setPlugin(plugin!)],
),
tabs: TabsController(pageManagers: [PageManager()])
..openPlugin(plugin: plugin!),
);
if (direction == Direction.front) {
if (i == node.children.length) {
Expand All @@ -102,19 +95,17 @@ class PanesService {
}
final ret = node.copyWith(
paneId: nanoid(),
tabs: Tabs(),
tabs: TabsController(),
children: node.children
.map(
(e) => e.copyWith(
tabs: Tabs(
currentIndex: e.tabs.currentIndex,
tabs: TabsController(
pageManagers: e.tabs.pageManagers,
),
),
)
.toList(),
);
setActivePaneCallback(ret.children[i]);
return ret;
}
}
Expand All @@ -131,7 +122,6 @@ class PanesService {
plugin: plugin,
direction: direction,
axis: axis,
setActivePaneCallback: setActivePaneCallback,
fromNode: fromNode,
),
)
Expand All @@ -142,43 +132,46 @@ class PanesService {
PaneNode closePaneHandler({
required PaneNode node,
required String targetPaneId,
required Function(PaneNode) setActivePaneCallback,
}) {
if (node.paneId == targetPaneId) {
return node;
}
for (final element in node.children) {
for (var i = 0; i < node.children.length; i++) {
final element = node.children[i];
if (element.paneId == targetPaneId) {
node.children.remove(element);
setActivePaneCallback(node);

node.children.remove(element..tabs.closeAllViews);
if (node.children.length == 1) {
setActivePaneCallback(node.children.first);
return node.children.first.copyWith(
final ret = node.children.first.copyWith(
paneId: nanoid(),
parent: node.parent,
tabs: Tabs(
currentIndex: node.children.first.tabs.currentIndex,
tabs: TabsController(
pageManagers: node.children.first.tabs.pageManagers,
),
);
return ret;
}

return node.copyWith(
final ret = node.copyWith(
children: node.children
.map(
(e) => e.copyWith(
tabs: TabsController(
pageManagers: e.tabs.pageManagers,
),
),
)
.toList(),
paneId: nanoid(),
tabs: Tabs(
currentIndex: node.tabs.currentIndex,
pageManagers: node.tabs.pageManagers,
),
);

return ret;
}
}

final newChildren = node.children.map((childNode) {
return closePaneHandler(
node: childNode,
targetPaneId: targetPaneId,
setActivePaneCallback: setActivePaneCallback,
);
}).toList();

Expand Down

0 comments on commit 58b0a06

Please sign in to comment.