Пример #1
0
        public SelectAreaPageViewModel(INavigationService navigationService,
                                       ITerminalService terminalService,
                                       IDialogService dialogService
                                       ) : base(navigationService, dialogService)
        {
            Title = "选择片区";

            _terminalService = terminalService;

            this.Load = ReactiveCommand.CreateFromTask(async() =>
            {
                var result = await _terminalService.GetDistrictsAsync(true, new System.Threading.CancellationToken());
                if (result != null)
                {
                    var series = result;
                    Districts  = new ObservableCollection <DistrictModel>(series);
                }
            });

            this.CancelCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                await _navigationService.GoBackAsync();
            });

            this.CheckCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var item = Districts.Where(s => s.Selected).ToList().FirstOrDefault();
                if (item == null)
                {
                    this.Alert("请选择项目!");
                    return;
                }

                await _navigationService.GoBackAsync(("District", item));
            });

            this.DeleteCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var item = Districts.Where(s => s.Selected).ToList().FirstOrDefault();
                await _navigationService.GoBackAsync(("ClearDistrict", item));
            });


            this.WhenAnyValue(x => x.Selecter).Throttle(TimeSpan.FromMilliseconds(500))
            .Skip(1)
            .Where(x => x != null)
            .SubOnMainThread(item =>
            {
                item.Selected = !item.Selected;
                this.Selecter = null;
            }).DisposeWith(DeactivateWith);

            this.BindBusyCommand(Load);
        }
Пример #2
0
        public async Task <IEnumerable <string> > SearchDistricts(string value)
        {
            var cancellationToken = new CancellationTokenSource().Token;

            DistrictsDTO = await coWINMetadataService.
                           GetDistricts(StatesDTO.Where(x => x.StateName == State).FirstOrDefault().StateId.ToString(), cancellationToken);

            Districts = DistrictsDTO.Select(x => x.DistrictName).ToList();

            if (string.IsNullOrEmpty(value))
            {
                return(Districts);
            }
            return(Districts.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)));
        }
Пример #3
0
    private void BalanceDistrictCounts()
    {
        var redDistricts  = Districts.Where((c) => { return(c.CurrentMajority == Constituent.Party.Red); });
        var blueDistricts = Districts.Where((c) => { return(c.CurrentMajority == Constituent.Party.Blue); });

        int redCount  = redDistricts.Count();
        int blueCount = blueDistricts.Count();

        while (redCount != blueCount)
        {
            //find out which one we want to switch
            District          districtToFlip;
            Constituent.Party originalParty, targetParty;

            //how many votes we need to sway in the other party's favor
            int amountToFlip;

            if (redCount > blueCount)
            {
                originalParty  = Constituent.Party.Red;
                targetParty    = Constituent.Party.Blue;
                districtToFlip = Utils.ChooseRandom(redDistricts.ToList());

                //enough votes to make it even, plus enough votes to give blue a small margin
                amountToFlip = (districtToFlip.VotesRed - districtToFlip.VotesBlue) / 2 + Random.Range(1, 4);
            }
            else
            {
                originalParty  = Constituent.Party.Blue;
                targetParty    = Constituent.Party.Red;
                districtToFlip = Utils.ChooseRandom(blueDistricts.ToList());

                //enough votes to make it even, plus enough votes to give red  a small margin
                amountToFlip = (districtToFlip.VotesBlue - districtToFlip.VotesRed) / 2 + Random.Range(1, 4);
            }

            //swap random consitutents in this district until it flips
            int numFlipped = 0;
            while (numFlipped < amountToFlip)
            {
                Constituent districtConstituent = districtToFlip.ConstituentsQuery.First((c) => c.party == originalParty);

                //select a random district from ALL other districts, and choose a random constituent in the target party
                District otherDistrict = Utils.ChooseRandom(Districts);
                if (otherDistrict == districtToFlip)
                {
                    continue;
                }
                Constituent otherConstituent = otherDistrict.ConstituentsQuery.FirstOrDefault((c) => c.party == targetParty);
                if (otherConstituent == null)
                {
                    continue;
                }

                //swap their parties
                districtConstituent.party = targetParty;
                otherConstituent.party    = originalParty;

                //update the other district's member data
                otherDistrict.UpdateMemberData();

                numFlipped++;
            }

            //update district member data
            districtToFlip.UpdateMemberData();

            //update the counts after the change
            redCount  = redDistricts.Count();
            blueCount = blueDistricts.Count();
        }
    }