home_controller.dart 3.2 KB
Newer Older
1
import 'package:carousel_slider/carousel_slider.dart';
2
import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
3 4 5 6 7
import 'package:flutter/cupertino.dart';
import 'package:logair_application/ui/routes/home.dart';

/// This controller class is integrated to the [HomeView] to enable out of scope control of select Widgets therein.
class HomeController {
8 9 10 11 12 13 14 15 16
  /// The page that the controllers were previously pointing toward.
  int _previousPage;

  /// The page that the controllers are now pointing toward.
  int _currentPage;

  /// Whether the navbar triggered the action, to avoid livelock generated by [CircularBottomNavigationController] listening to [_navigationController.value].
  bool _triggerOriginNavbar;

17 18
  factory HomeController() => _singleton;

19
  /// [HomeController] initializer that sets the default initial values.
20 21 22 23 24
  HomeController._internal() {
    this._currentPage = 0;
    this._previousPage = 0;
    this._triggerOriginNavbar = true;
  }
25 26 27 28 29 30

  static final HomeController _singleton = new HomeController._internal();

  /// The controller for the [CarouselSlider].
  PageController _pageController;

31 32 33 34
  /// The controller for the [CircularBottomNavigation]
  CircularBottomNavigationController _navigationController;

  /// Sets the [PageController] for the [CarouselSlider]
35 36
  set carouselController(PageController carouselController) => this._pageController = carouselController;

37
  /// Sets the [CircularBottomNavigationController] for the [CircularBottomNavigation]
38 39
  set navigationController(CircularBottomNavigationController navController) { 
    this._navigationController = navController;
40 41 42
  }

  /// Forces the [CircularBottomNavigation] to the correct position.
43 44 45 46 47 48
  set navbar(int page) {
    this._currentPage = page;
    // Set to false to mitigate livelock.
    this._triggerOriginNavbar = false;

    // Guard
49
    if (this._navigationController != null)
50 51 52 53 54
      this._navigationController.value = (this._currentPage + 1) % 3;
    
    this._previousPage = this._currentPage;
    // Set to true once risk of livelock passed.
    this._triggerOriginNavbar = true;
55
  }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

  /// Forces the [PageController] to the correct position.
  set page(int page) {
    // Guard + Supplementary guard against livelock.
    if (this._pageController != null && this._triggerOriginNavbar) {
      // Navbar has pages in order [2, 0, 1] of the carousel.
      this._currentPage = (page - 1) % 3;

      // Make the carousel controller move forward (jump to +1 would limit UI because of lackluster cyclical handling).
      if ((this._currentPage == 0 && this._previousPage == 2) || (this._currentPage == 1 && this._previousPage == 0) || (this._currentPage == 2 && this._previousPage == 1))
        this._pageController.nextPage(duration: Duration(milliseconds: 200), curve: Curves.linear);
      // Make the carousel controller move backward (jump to +1 would limit UI because of lackluster cyclical handling).
      else if ((this._currentPage == 2 && this._previousPage == 0) || (this._currentPage == 0 && this._previousPage == 1) || (this._currentPage == 1 && this._previousPage == 2))
        this._pageController.previousPage(duration: Duration(milliseconds: 200), curve: Curves.linear);

      this._previousPage = this._currentPage;
    }
  }

75
}