public CreateChannelStep2ViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            AdminedPublicChannels = new ObservableCollection <TLChatBase>();

            InviteLink = AppResources.Loading;

            _newChannel             = StateService.NewChannel;
            StateService.NewChannel = null;

            MTProtoService.CheckUsernameAsync(new TLInputChannelEmpty(), new TLString("username"),
                                              result => BeginOnUIThread(() =>
            {
                TooMuchUsernames = false;
            }),
                                              error => BeginOnUIThread(() =>
            {
                if (TLRPCError.TypeEquals(error, ErrorType.CHANNELS_ADMIN_PUBLIC_TOO_MUCH))
                {
                    HasError         = true;
                    Error            = AppResources.ChannelsAdminPublicTooMuchShort;
                    TooMuchUsernames = true;
                    GetAdminedPublichChannelsAsync();
                }
            }));

            PropertyChanged += (sender, args) =>
            {
                if (Property.NameEquals(args.PropertyName, () => IsPublic))
                {
                    if (!IsPublic && Invite == null)
                    {
                        ExportInvite();
                    }
                }
                else if (Property.NameEquals(args.PropertyName, () => UserName))
                {
                    var userName = UserName;
                    BeginOnUIThread(TimeSpan.FromSeconds(0.3), () =>
                    {
                        if (string.Equals(userName, UserName))
                        {
                            Check();
                        }
                    });
                }
            };
        }
        public void Check()
        {
            var checkedUsername = UserName;

            var username = UserName;

            if (username != null &&
                username.StartsWith("@"))
            {
                username = username.Substring(1, username.Length - 1);
            }

            if (string.IsNullOrEmpty(username))
            {
                HasError            = false;
                IsUsernameAvailable = false;
                //Error = string.Empty;
                return;
            }

            var isValidSymbols = username.All(IsValidSymbol);

            if (!isValidSymbols)
            {
                HasError = true;
                Error    = AppResources.ChannelNameInvalid;
                return;
            }

            if (username[0] >= '0' && username[0] <= '9')
            {
                HasError = true;
                Error    = AppResources.ChannelNameStartsWithNumber;
                return;
            }

            if (username.Length < Constants.UsernameMinLength)
            {
                HasError = true;
                Error    = AppResources.ChannelNameShort;
                return;
            }

            HasError            = false;
            IsUsernameAvailable = false;

            MTProtoService.CheckUsernameAsync(_newChannel.ToInputChannel(), new TLString(username),
                                              result => BeginOnUIThread(() =>
            {
                HasError = !result.Value;
                if (HasError)
                {
                    Error = AppResources.ChannelNameOccupied;
                }

                if (string.Equals(checkedUsername, UserName) && result.Value)
                {
                    IsUsernameAvailable     = true;
                    UsernameAvailableString = string.Format(AppResources.NameIsAvailable, checkedUsername);
                }
            }),
                                              error => BeginOnUIThread(() =>
            {
                HasError = true;
                if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                {
                    Error = AppResources.FloodWaitString;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.INTERNAL))
                {
                    var messageBuilder = new StringBuilder();
                    messageBuilder.AppendLine(AppResources.ServerErrorMessage);
                    messageBuilder.AppendLine();
                    messageBuilder.AppendLine("Method: channels.checkUsername");
                    messageBuilder.AppendLine("Result: " + error);

                    Error = AppResources.ServerError;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST))
                {
                    if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_INVALID))
                    {
                        Error = AppResources.ChannelNameInvalid;
                    }
                    else if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_OCCUPIED))
                    {
                        Error = AppResources.ChannelNameOccupied;
                    }
                    else if (TLRPCError.TypeEquals(error, ErrorType.CHANNELS_ADMIN_PUBLIC_TOO_MUCH))
                    {
                        MessageBox.Show(AppResources.ChannelsAdminPublicTooMuch, AppResources.Error, MessageBoxButton.OK);

                        Error            = AppResources.ChannelsAdminPublicTooMuchShort;
                        TooMuchUsernames = true;
                        GetAdminedPublichChannelsAsync();
                    }
                    else
                    {
                        Error = error.ToString();
                    }
                }
                else
                {
                    Error = string.Empty;
                    Execute.ShowDebugMessage("channels.checkUsername error " + error);
                }
            }));
        }
        public void Check()
        {
            var local    = Username;
            var username = Username;

            if (username != null &&
                username.StartsWith("@"))
            {
                username = username.Substring(1, username.Length - 1);
            }

            if (string.IsNullOrEmpty(username))
            {
                HasError = false;
                //Error = string.Empty;
                return;
            }

            var isValidSymbols = username.All(IsValidSymbol);

            if (!isValidSymbols)
            {
                var message = IsMegaGroup ? AppResources.SupergroupNameInvalid : AppResources.ChannelNameInvalid;
                HasError = true;
                Error    = message;
                return;
            }

            if (username[0] >= '0' && username[0] <= '9')
            {
                var message = IsMegaGroup ? AppResources.SupergroupNameStartsWithNumber : AppResources.ChannelNameStartsWithNumber;
                HasError = true;
                Error    = message;
                return;
            }

            if (username.Length < Constants.UsernameMinLength)
            {
                var message = IsMegaGroup ? AppResources.SupergroupNameShort : AppResources.ChannelNameShort;
                HasError = true;
                Error    = message;
                return;
            }

            if (_currentItem == null)
            {
                return;
            }

            MTProtoService.CheckUsernameAsync(_currentItem.ToInputChannel(), new TLString(username),
                                              result => Execute.BeginOnUIThread(() =>
            {
                if (local != Username)
                {
                    return;
                }

                HasError = !result.Value;
                if (HasError)
                {
                    Error = AppResources.UsernameOccupied;
                }
            }),
                                              error => Execute.BeginOnUIThread(() =>
            {
                HasError = true;
                if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                {
                    Error = AppResources.FloodWaitString;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.INTERNAL))
                {
                    var messageBuilder = new StringBuilder();
                    messageBuilder.AppendLine(AppResources.ServerErrorMessage);
                    messageBuilder.AppendLine();
                    messageBuilder.AppendLine("Method: channel.checkUsername");
                    messageBuilder.AppendLine("Result: " + error);

                    Error = AppResources.ServerError;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST))
                {
                    if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_INVALID))
                    {
                        var message = IsMegaGroup
                                ? AppResources.SupergroupNameInvalid
                                : AppResources.ChannelNameInvalid;
                        Error = message;
                    }
                    else if (TLRPCError.TypeEquals(error, ErrorType.CHANNELS_ADMIN_PUBLIC_TOO_MUCH))
                    {
                        var message = AppResources.ChannelsAdminPublicTooMuchShort;
                        Error       = message;
                    }
                    else if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_OCCUPIED))
                    {
                        var message = IsMegaGroup
                                ? AppResources.SupergroupNameOccupied
                                : AppResources.ChannelNameOccupied;
                        Error = message;
                    }
                    else
                    {
                        Error = error.ToString();
                        //Execute.BeginOnUIThread(() => NavigationService.GoBack());
                    }
                }
                else
                {
                    Error = string.Empty;
                    Execute.ShowDebugMessage("channel.updateUsername error " + error);
                }
            }));
        }
Exemple #4
0
        public void Check()
        {
            var checkedUsername = Username;

            var username = Username;

            if (username != null &&
                username.StartsWith("@"))
            {
                username = username.Substring(1, username.Length - 1);
            }

            if (string.IsNullOrEmpty(username))
            {
                HasError            = false;
                IsUsernameAvailable = false;
                //Error = string.Empty;
                return;
            }

            var isValidSymbols = username.All(IsValidSymbol);

            if (!isValidSymbols)
            {
                HasError = true;
                Error    = AppResources.UsernameInvalid;
                return;
            }

            if (username[0] >= '0' && username[0] <= '9')
            {
                HasError = true;
                Error    = AppResources.UsernameStartsWithNumber;
                return;
            }

            if (username.Length < Constants.UsernameMinLength)
            {
                HasError = true;
                Error    = AppResources.UsernameShort;
                return;
            }

            HasError            = false;
            IsUsernameAvailable = false;

            MTProtoService.CheckUsernameAsync(new TLString(username),
                                              result =>
            {
                HasError = !result.Value;
                if (HasError)
                {
                    Error = AppResources.UsernameOccupied;
                }

                if (string.Equals(checkedUsername, Username) && result.Value)
                {
                    IsUsernameAvailable     = true;
                    UsernameAvailableString = string.Format(AppResources.NameIsAvailable, checkedUsername);
                }
            },
                                              error =>
            {
                HasError = true;
                if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                {
                    Error = AppResources.FloodWaitString;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.INTERNAL))
                {
                    var messageBuilder = new StringBuilder();
                    messageBuilder.AppendLine(AppResources.ServerErrorMessage);
                    messageBuilder.AppendLine();
                    messageBuilder.AppendLine("Method: account.checkUsername");
                    messageBuilder.AppendLine("Result: " + error);

                    Error = AppResources.ServerError;
                }
                else if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST))
                {
                    if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_INVALID))
                    {
                        Error = AppResources.UsernameInvalid;
                    }
                    else if (TLRPCError.TypeEquals(error, ErrorType.USERNAME_OCCUPIED))
                    {
                        Error = AppResources.UsernameOccupied;
                    }
                    else
                    {
                        Error = error.ToString();
                        //Execute.BeginOnUIThread(() => NavigationService.GoBack());
                    }
                }
                else
                {
                    Error = string.Empty;
                    Execute.ShowDebugMessage("account.checkUsername error " + error);
                }
            });
        }