container_coordinator.dart 8.8 KB
Newer Older
Jidong Chen's avatar
init  
Jidong Chen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2019 Alibaba Group
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
import 'dart:async';
Yacumima's avatar
dev  
Yacumima committed
25
import 'dart:io';
Jidong Chen's avatar
init  
Jidong Chen committed
26 27

import 'package:flutter/material.dart';
Yacumima's avatar
dev  
Yacumima committed
28
import 'package:flutter/semantics.dart';
Yacumima's avatar
Yacumima committed
29
import 'package:flutter/services.dart';
Yacumima's avatar
dev  
Yacumima committed
30 31 32 33
import '../channel/boost_channel.dart';
import 'boost_container.dart';
import '../flutter_boost.dart';
import '../logger.dart';
Jidong Chen's avatar
init  
Jidong Chen committed
34

Yacumima's avatar
Yacumima committed
35 36 37 38
class ContainerCoordinator {
  static ContainerCoordinator get singleton => _instance;

  static ContainerCoordinator _instance;
Jidong Chen's avatar
init  
Jidong Chen committed
39 40 41 42

  final Map<String, PageBuilder> _pageBuilders = <String, PageBuilder>{};
  PageBuilder _defaultPageBuilder;

Yacumima's avatar
Yacumima committed
43 44 45 46 47 48
  ContainerCoordinator(BoostChannel channel) {
    assert(_instance == null);

    _instance = this;

    channel.addEventListener("lifecycle",
Yacumima's avatar
dev  
Yacumima committed
49
        (String name, Map arguments) => _onChannelEvent(arguments));
Yacumima's avatar
Yacumima committed
50

Yacumima's avatar
dev  
Yacumima committed
51
    channel.addMethodHandler((MethodCall call) => _onMethodCall(call));
Jidong Chen's avatar
init  
Jidong Chen committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  }

  BoostContainerSettings _createContainerSettings(
      String name, Map params, String pageId) {
    Widget page;

    final BoostContainerSettings routeSettings = BoostContainerSettings(
        uniqueId: pageId,
        name: name,
        params: params,
        builder: (BuildContext ctx) {
          //Try to build a page using keyed builder.
          if (_pageBuilders[name] != null) {
            page = _pageBuilders[name](name, params, pageId);
          }

          //Build a page using default builder.
          if (page == null && _defaultPageBuilder != null) {
            page = _defaultPageBuilder(name, params, pageId);
          }

          assert(page != null);
          Logger.log('build widget:$page for page:$name($pageId)');

          return page;
        });

    return routeSettings;
  }

  //Register a default page builder.
  void registerDefaultPageBuilder(PageBuilder builder) {
    _defaultPageBuilder = builder;
  }

  //Register page builder for a key.
  void registerPageBuilder(String pageName, PageBuilder builder) {
    if (pageName != null && builder != null) {
      _pageBuilders[pageName] = builder;
    }
  }

  void registerPageBuilders(Map<String, PageBuilder> builders) {
    if (builders?.isNotEmpty == true) {
      _pageBuilders.addAll(builders);
    }
  }

Yacumima's avatar
Yacumima committed
100
  Future<dynamic> _onChannelEvent(dynamic event) {
Jidong Chen's avatar
init  
Jidong Chen committed
101
    if (event is Map) {
102
      Map map = event;
Jidong Chen's avatar
init  
Jidong Chen committed
103 104
      final String type = map['type'];

Yacumima's avatar
Yacumima committed
105 106
      Logger.log("onEvent $type");

Jidong Chen's avatar
init  
Jidong Chen committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      switch (type) {
        //Handler back key pressed event.
        case 'backPressedCallback':
          {
            final String id = map['uniqueId'];
            FlutterBoost.containerManager
                ?.containerStateOf(id)
                ?.performBackPressed();
          }
          break;
        //Enter foregroud
        case 'foreground':
          {
            FlutterBoost.containerManager?.setForeground();
          }
          break;
        //Enter background
        case 'background':
          {
            FlutterBoost.containerManager?.setBackground();
          }
          break;
        //Schedule a frame.
        case 'scheduleFrame':
          {
            WidgetsBinding.instance.scheduleForcedFrame();
            Future<dynamic>.delayed(Duration(milliseconds: 250),
                () => WidgetsBinding.instance.scheduleFrame());
          }
          break;
      }
    }
Yacumima's avatar
Yacumima committed
139 140 141 142 143

    return Future<dynamic>(() {});
  }

  Future<dynamic> _onMethodCall(MethodCall call) {
Yacumima's avatar
Yacumima committed
144 145
    Logger.log("onMetohdCall ${call.method}");

Yacumima's avatar
Yacumima committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    switch (call.method) {
      case "didInitPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          _nativeContainerDidInit(pageName, params, uniqueId);
        }
        break;
      case "willShowPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          _nativeContainerWillShow(pageName, params, uniqueId);
        }
        break;
      case "didShowPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          nativeContainerDidShow(pageName, params, uniqueId);
        }
        break;
      case "willDisappearPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          _nativeContainerWillDisappear(pageName, params, uniqueId);
        }
        break;
      case "didDisappearPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          _nativeContainerDidDisappear(pageName, params, uniqueId);
        }
        break;
      case "willDeallocPageContainer":
        {
          String pageName = call.arguments["pageName"];
          Map params = call.arguments["params"];
          String uniqueId = call.arguments["uniqueId"];
          _nativeContainerWillDealloc(pageName, params, uniqueId);
        }
        break;
      case "onNativePageResult":
        {}
        break;
    }

    return Future<dynamic>(() {});
Jidong Chen's avatar
init  
Jidong Chen committed
201 202
  }

Yacumima's avatar
Yacumima committed
203
  bool _nativeContainerWillShow(String name, Map params, String pageId) {
Jidong Chen's avatar
init  
Jidong Chen committed
204 205 206 207 208 209 210 211 212 213 214 215
    if (FlutterBoost.containerManager?.containsContainer(pageId) != true) {
      FlutterBoost.containerManager
          ?.pushContainer(_createContainerSettings(name, params, pageId));
    }

    return true;
  }

  bool nativeContainerDidShow(String name, Map params, String pageId) {
    FlutterBoost.containerManager
        ?.showContainer(_createContainerSettings(name, params, pageId));

Yacumima's avatar
dev  
Yacumima committed
216 217 218 219
    //在Android上对无障碍辅助模式的兼容
    if (Platform.isAndroid) {
      try {
        final SemanticsOwner owner =
Yacumima's avatar
dev  
Yacumima committed
220 221
            WidgetsBinding.instance.pipelineOwner?.semanticsOwner;
        final SemanticsNode root = owner?.rootSemanticsNode;
Yacumima's avatar
dev  
Yacumima committed
222 223 224 225 226 227 228
        root?.detach();
        root?.attach(owner);
      } catch (e) {
        assert(false, e.toString());
      }
    }

Jidong Chen's avatar
init  
Jidong Chen committed
229 230 231 232 233 234 235 236 237
    performContainerLifeCycle(_createContainerSettings(name, params, pageId),
        ContainerLifeCycle.Appear);

    Logger.log(
        'native containner did show,\nmanager dump:\n${FlutterBoost.containerManager?.dump()}');

    return true;
  }

Yacumima's avatar
Yacumima committed
238
  bool _nativeContainerWillDisappear(String name, Map params, String pageId) {
239 240
    performContainerLifeCycle(_createContainerSettings(name, params, pageId),
        ContainerLifeCycle.WillDisappear);
Jidong Chen's avatar
init  
Jidong Chen committed
241 242 243
    return true;
  }

Yacumima's avatar
Yacumima committed
244
  bool _nativeContainerDidDisappear(String name, Map params, String pageId) {
Jidong Chen's avatar
init  
Jidong Chen committed
245 246 247 248 249
    performContainerLifeCycle(_createContainerSettings(name, params, pageId),
        ContainerLifeCycle.Disappear);
    return true;
  }

Yacumima's avatar
Yacumima committed
250
  bool _nativeContainerDidInit(String name, Map params, String pageId) {
Jidong Chen's avatar
init  
Jidong Chen committed
251 252 253 254 255
    performContainerLifeCycle(_createContainerSettings(name, params, pageId),
        ContainerLifeCycle.Init);
    return true;
  }

Yacumima's avatar
Yacumima committed
256
  bool _nativeContainerWillDealloc(String name, Map params, String pageId) {
Jidong Chen's avatar
init  
Jidong Chen committed
257 258 259 260 261 262
    performContainerLifeCycle(_createContainerSettings(name, params, pageId),
        ContainerLifeCycle.Destroy);

    FlutterBoost.containerManager?.remove(pageId);

    Logger.log(
263
        'native containner dealloc, \n manager dump:\n${FlutterBoost.containerManager?.dump()}');
Jidong Chen's avatar
init  
Jidong Chen committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    return true;
  }

  static void performContainerLifeCycle(
      BoostContainerSettings settings, ContainerLifeCycle lifeCycle) {
    for (BoostContainerLifeCycleObserver observer in FlutterBoost
        .singleton.observersHolder
        .observersOf<BoostContainerLifeCycleObserver>()) {
      observer(lifeCycle, settings);
    }

    Logger.log(
        'BoostContainerLifeCycleObserver container:${settings.name} lifeCycle:$lifeCycle');
  }
}