onBootstrap static method

Widget onBootstrap(
  1. BuildContext context,
  2. dynamic complete(
    1. bool
    )
)

Implementation

static Widget onBootstrap(
  BuildContext context,
  Function(bool) complete,
) {
  final organizationsService = context.read<OrganizationService>();
  final feature = OrganizationFeature.instance();

  return FutureBuilder(
    future: organizationsService.loadOrgs(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        final organizations = snapshot.data;
        if (organizations == null) {
          // if no organization list is returned then
          // an error occurred and the user should be
          // be logged out and an error message
          // displayed
          UserIdentityFeature.logout(context);
        } else if (feature.config.membershipRequired) {
          // if membership is required then ensure
          // the user is associated with an organization
          if (organizations.isEmpty) {
            // if the user is not associated with any
            // organizations then show the create
            // organization dialog.
            return OrganizationProfile(
              buildContainer: (child, width, height) {
                return SizedBox(
                  width: width + 40, // card margins (4*2) + padding (16*2)
                  height: height + 40, // card margins (4*2) + padding (16*2)
                  child: Card(
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: child,
                    ),
                  ),
                );
              },
              dismissDialog: () {
                UserIdentityFeature.logout(context);
                Future.microtask(() => complete(false));
              },
              createOrgOnly: true,
            );
          } else {
            // if membership is required then the user must
            // be associated with at least one organization
            // and a default must be set and verified.
            final defaultOrg = UserIdentityFeature.getDefaultOrg(context);
            bool isVerified = organizations.any(
              (org) => org.orgId == defaultOrg && org.isVerified,
            );
            late Organization organization;
            if (!isVerified) {
              // pick the first verified organization or the
              // first organization if none are verified
              organization = organizations.firstWhere(
                (org) => org.isVerified,
                orElse: () => organizations.first,
              );
              isVerified = organization.isVerified;

              if (!isVerified) {
                // if the organization selected is not verified
                // then show the unverified organization alert
                // dialog and allow the user to logout
                return SizedBox(
                  width: OrganizationUnverified.formViewWidth + 32,
                  height: OrganizationUnverified.formViewHeight + 32,
                  child: Card(
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: OrganizationUnverified(
                        orgName: organization.name,
                        logout: () {
                          UserIdentityFeature.logout(context);
                          Future.microtask(() => complete(false));
                        },
                      ),
                    ),
                  ),
                );
              } else {
                // if the organization is verified then set
                // the default organization to the verified
                // organization
                UserIdentityFeature.setDefaultOrg(
                  context,
                  organization.orgId,
                );
              }
            } else {
              // if the default organization is verified
              // then retrieve it from user's list of
              // organizations
              organization = organizations.firstWhere(
                (org) => org.orgId == defaultOrg,
              );
            }
            // load the organization details and complete
            // the bootstrapping process once the details
            // are loaded
            organizationsService.readOrgData(organization).then((_) {
              Future.microtask(() => complete(true));
            });
            return const SizedBox();
          }
        }
        // bootstrapping is complete and
        // the user can proceed to the app
        Future.microtask(() => complete(true));
        return const SizedBox();
      } else {
        // show loading indicator while
        // organizations user is associated
        // with are being loaded
        return const CircularProgressIndicator();
      }
    },
  );
}