Exemplo n.º 1
0
        public ConfigureAppViewModel(NewProjectViewModel screen = null) : base("newprj_app", screen)
        {
            Title                = "New Project";
            Description          = "Follow these steps to generate the Xamarin solution: specify your app name, organization and supported platforms.\nFurther settings in the \"Next\" section.";
            IsDescriptionVisible = true;

            BackVisible = false;

            this.WhenAnyValue(t => t.AppName, t => t.OrganizationId, t => t.TargetAndroid, t => t.TargetIos,
                              (app, org, target1, target2) => !string.IsNullOrWhiteSpace(app) && !string.IsNullOrWhiteSpace(org) && (target1 || target2))
            .BindTo(this, t => t.NextEnabled);

            this.WhenAnyValue(t => t.AppName, t => t.OrganizationId, BuildAndroidAppId)
            .ToProperty(this, t => t.AndroidAppId, out _androidAppId, initialValue: BuildAndroidAppId(AppName, OrganizationId));

            this.WhenAnyValue(t => t.AppName, t => t.OrganizationId, BuildIosAppId)
            .ToProperty(this, t => t.IosAppId, out _iosAppId, initialValue: BuildIosAppId(AppName, OrganizationId));

            // Command to check if app can target Android / iOS
            ReactiveCommand <Unit, Unit> checkPlatformRequirementsCommand = ReactiveCommand.CreateFromTask(() => Task.Run(() =>
            {
                // Check iOS support
                if (OperatingSystem.IsWindows())
                {
                    CanTargetIos = false;
                    SetPlatformMessage(Platform.iOS, PlatformMessageType.Normal, "Flutter iOS projects are supported on macOS only.");
                }

                // Check Android SDK installation
                bool canTargetAndroid = AndroidSdkLocator.TryLocate(out _);

                CanTargetAndroid = canTargetAndroid;
                if (!canTargetAndroid)
                {
                    SetPlatformMessage(Platform.Android, PlatformMessageType.Normal, "Android SDK not detected.");
                }

                TargetIos     = CanTargetIos;
                TargetAndroid = CanTargetAndroid;
            }));

            checkPlatformRequirementsCommand.Execute();
        }
        public CreateProjectProgressViewModel(FlutnetAppSettings appSettings, FlutnetProjectSettings projectSettings, NewProjectViewModel screen = null) : base("newprj_progress", screen)
        {
            _appSettings     = appSettings;
            _projectSettings = projectSettings;

            Title                = "New Project";
            Description          = "The project will take some time to generate.\nWait until the procedure finish.\n";
            IsDescriptionVisible = false;

            NextText     = "Finish";
            BackVisible  = false;
            IsFinishPage = true;

            OutputLines = new ObservableCollection <string>();
            // Observe any changes in the observable collection.
            // Note that the property has no public setters, so we
            // assume the collection is mutated by using the Add(),
            // Delete(), Clear() and other similar methods.
            OutputLines
            // Convert the collection to a stream of chunks,
            // so we have IObservable<IChangeSet<TKey, TValue>>
            // type also known as the DynamicData monad.
            .ToObservableChangeSet()
            // Each time the collection changes, we get
            // all updated items at once.
            .ToCollection()
            // Aggregate all the elements in the collection
            // into a multi-line string.
            .Select(lines => string.Join(Environment.NewLine, lines))
            // Then, we convert the multi-line string to the
            // property a multi-line TextBox can bind.
            .ToProperty(this, x => x.Output, out _output, scheduler: RxApp.MainThreadScheduler);

            // Create the command that calls Flutnet CLI
            CreateProject = ReactiveCommand.CreateFromTask(async ct =>
            {
                NewProjectInArg arguments = BuildCommandLineArg();

                CommandLineCallResult callResult = await CommandLineTools.Call <NewProjectOutArg>(arguments, ct, line =>
                {
                    OutputLines.Add(line);
                });

                // This is not the proper way to change property values and raise property change notifications:
                // we should return a public object and subscribe to the command observable
                // so that we can use ReactiveUI framework methods such as ToProperty, BindTo etc.
                if (callResult.Canceled)
                {
                    IsCanceled = true;
                }
                else if (callResult.Failed)
                {
                    IsFailed = true;
                }
                else
                {
                    OutArg result = callResult.CommandResult;
                    if (!result.Success)
                    {
                        IsFailed = true;
                    }
                    else
                    {
                        IsCompletedSuccessfully = true;
                    }
                }
            });

            CreateProject.IsExecuting.ToProperty(this, x => x.IsInProgress, out _isInProgress);
            CreateProject.IsExecuting.BindTo(this, x => x.IsBusy);

            BrowseProject = ReactiveCommand.Create(
                execute: () => Launcher.OpenFolder(Path.Combine(projectSettings.Location, projectSettings.SolutionName)),
                canExecute: this.WhenAnyValue(t => t.IsCompletedSuccessfully));

            // Execute the command when the View is activated
            Activator = new ViewModelActivator();
            this.WhenActivated(disposables =>
            {
                CreateProject.Execute(Unit.Default).Subscribe().DisposeWith(disposables);
            });

            this.WhenAnyValue(t => t.IsInProgress, t => !t).BindTo(this, t => t.NextEnabled);
        }
Exemplo n.º 3
0
        public MainPageViewModel(IScreen hostScreen) : base("main", hostScreen)
        {
            // Initialize all the page view models
            NewProjectViewModel newProjectPage = new NewProjectViewModel(this);
            AboutViewModel      aboutPage      = new AboutViewModel(this);

            // Initialize the commands for navigating to these pages
            OpenNewProjectPage = ReactiveCommand.CreateFromObservable(() => Router.Navigate.Execute(newProjectPage));
            OpenAboutPage      = ReactiveCommand.CreateFromObservable(() => Router.Navigate.Execute(aboutPage));

            // Create the command for displaying the setup wizard at startup
            ReactiveCommand <Unit, Unit> cmdOpenSetupWizard = ReactiveCommand.CreateFromObservable <Unit, Unit>(sender =>
            {
                return(ShowSetupWizard.Handle(Unit.Default));
            });

            // Check license status and optionally check for updates when the view is activated
            this.WhenActivated(disposables =>
            {
                AppSettings.Default.Preferences.Reload();
                if (AppSettings.Default.Preferences.CheckForUpdatesAtStartup)
                {
                    aboutPage.CheckForUpdates.Execute().Subscribe().DisposeWith(disposables);
                }

                // Set the default (initial) page to the new project page
                Router.Navigate.Execute(newProjectPage).Subscribe().DisposeWith(disposables);
            });

            newProjectPage.WhenActivated(disposables =>
            {
                if (_firstActivation && AppSettings.Default.Preferences.ShowSetupWizardAtStartup)
                {
                    cmdOpenSetupWizard.Execute().Subscribe().DisposeWith(disposables);
                    _firstActivation = false;
                }
            });

            //// Sample of a command executed periodically
            //TimeSpan interval = TimeSpan.FromHours(1);
            //Observable.Timer(interval, interval)
            //    .Select(time => Unit.Default)
            //    .InvokeCommand(aboutPage, p => p.CheckForUpdates);

            aboutPage.WhenAnyValue(p => p.IsNewVersionAvailable).BindTo(this, t => t.IsUpdateAvailable);

            Router.CurrentViewModel
            .Where(vm => vm is IPageViewModel)
            .Select(vm => (IPageViewModel)vm)
            .Subscribe(page =>
            {
                page.WhenAnyValue(p => p.Title).BindTo(this, t => t.Title);
                page.WhenAnyValue(p => p.IsBusy).BindTo(this, t => t.IsBusy);
            });

            Router.CurrentViewModel
            .Select(vm => vm is NewProjectViewModel)
            .ToProperty(this, t => t.IsNewProjectPageVisible, out _isNewProjectPageVisible);

            Router.CurrentViewModel
            .Select(vm => vm is AboutViewModel)
            .ToProperty(this, t => t.IsAboutPageVisible, out _isAboutPageVisible);
        }