Beispiel #1
0
        protected async override void OnAppearing()
        {
            base.OnAppearing();

            if (string.IsNullOrEmpty(RetrievedItemDataStore.Instance().id))
            {
                id.Text           = "Retrieve records first";
                timeStamp.Text    = "Retrieve records first";
                userName.Text     = "Retrieve records first";
                userEmail.Text    = "Retrieve records first";
                userPassword.Text = "Retrieve records first";
            }
            else
            {
                id.Text           = RetrievedItemDataStore.Instance().id;
                timeStamp.Text    = RetrievedItemDataStore.Instance().savedTimeStamp;
                userName.Text     = RetrievedItemDataStore.Instance().name;
                userEmail.Text    = RetrievedItemDataStore.Instance().email;
                userPassword.Text = RetrievedItemDataStore.Instance().password;
            }
        }
        //******

        public RetrieveViewModel()
        {
            ////RegisterPersonCommand = new Command<string>(async (itemName) =>
            //RegisterPersonCommand = new Command(async () =>
            //{
            //    //if (string.IsNullOrEmpty(_userNameText))
            //    //    _userNameText = "";
            //    //if (string.IsNullOrEmpty(_userEmailText))
            //    //    _userEmailText = "";
            //    //if (string.IsNullOrEmpty(_userPasswordText))
            //    //_userPasswordText = "";

            //    Item itemToSave = new Item
            //    {
            //        Name = _userNameText,
            //        Email = _userEmailText,
            //        Password = _userPasswordText
            //    };

            //    //await Helpers.AwsDynamoDbHelper.Instance().SaveItemAsync(itemName);
            //    //await Helpers.AwsDynamoDbHelper.Instance().SaveItemAsync(_userNameText);

            //    IsBusy = true;
            //    await Helpers.AwsDynamoDbHelper.Instance().SaveItemAsync(itemToSave);
            //    IsBusy = !IsBusy;
            //});

            //RetrievePersonCommand = new Command(async () =>
            //{
            //    IsBusy = true;
            //    await Helpers.AwsDynamoDbHelper.Instance().ReadItemAsync("20180401162245+08:00#d28a0288-18ab-49ce-964d-ccdca8738fc9");
            //    IsBusy = !IsBusy;
            //});

            ReadPersonEqualCommand = new Command(async() =>
            {
                IsBusy         = true;
                var readResult = await Helpers.AwsDynamoDbHelper.Instance().ReadItemEqualAsync("Name", UserNameToRetrieveText);
                IsBusy         = !IsBusy;

                if (readResult.Count >= 1)
                {
                    readResult.ForEach((Item itemResult) =>
                    {
                        _idText           = itemResult.Id;
                        _timeStampText    = itemResult.SavedTimeStamp;
                        _userNameText     = itemResult.Name;
                        _userEmailText    = itemResult.Email;
                        _userPasswordText = itemResult.Password;
                    });

                    //****** Adapted from https://forums.xamarin.com/discussion/comment/280634/#Comment_280634 (taken from NMackay's June 2017 answer in https://forums.xamarin.com/discussion/97734/how-to-update-label-in-asynctask-from-the-viewmodel).
                    IdText           = _idText;
                    TimeStampText    = _timeStampText;
                    UserNameText     = _userNameText;
                    UserEmailText    = _userEmailText;
                    UserPasswordText = _userPasswordText;
                    //******
                }
                else
                {
                    IdText           = "No result found";
                    TimeStampText    = "No result found";
                    UserNameText     = "No result found";
                    UserEmailText    = "No result found";
                    UserPasswordText = "No result found";
                }

                RetrievedItemDataStore.Instance().retrievedName  = UserNameToRetrieveText;                //Is it necessary to store the UserNameToRetrieveText?
                RetrievedItemDataStore.Instance().id             = IdText;
                RetrievedItemDataStore.Instance().savedTimeStamp = TimeStampText;
                RetrievedItemDataStore.Instance().name           = UserNameText;
                RetrievedItemDataStore.Instance().email          = UserEmailText;
                RetrievedItemDataStore.Instance().password       = UserPasswordText;

                //System.Diagnostics.Debug.WriteLine("Name = " + _userNameText + ", Email = " + _userEmailText + ", Password = "******"Name = " + UserNameText + ", Email = " + UserEmailText + ", Password = " + UserPasswordText);
            });
        }
Beispiel #3
0
        //******

        //****** Adapted from the Query and Scan section in https://docs.aws.amazon.com/mobile/sdkforxamarin/developerguide/dynamodb-integration-objectpersistencemodel.html
        /// <summary>
        /// Reads items where the contents of the field is equal to the query string. This method is async.
        /// </summary>
        /// <returns>A List of items.</returns>
        /// <param name="fieldToQuery">Field to query.</param>
        /// <param name="queryString">Query string.</param>
        public async Task <List <Item> > ReadItemEqualAsync(string fieldToQuery, string queryString)
        {
            if (string.IsNullOrEmpty(queryString))
            {
                List <Item> blankResponse = new List <Item>
                {
                    new Item
                    {
                        Id             = "Nothing to retrieve",
                        SavedTimeStamp = "Nothing to retrieve",
                        Name           = "Nothing to retrieve",
                        Email          = "Nothing to retrieve",
                        Password       = "******"
                    }
                };

                RetrievedItemDataStore.Instance().retrievedName  = "Nothing to retrieve";                //Is it necessary to store the UserNameToRetrieveText?
                RetrievedItemDataStore.Instance().id             = "Nothing to retrieve";
                RetrievedItemDataStore.Instance().savedTimeStamp = "Nothing to retrieve";
                RetrievedItemDataStore.Instance().name           = "Nothing to retrieve";
                RetrievedItemDataStore.Instance().email          = "Nothing to retrieve";
                RetrievedItemDataStore.Instance().password       = "******";

                return(blankResponse);                //An error of "value cannot be null" will be thrown in the viewmodel if the queryString is null.
                                                      //So FromQueryAsync() isn't executed if queryString is null.
            }

            //var client = new AmazonDynamoDBClient(_credentials, RegionEndpoint.USEast2);
            //DynamoDBContext context = new DynamoDBContext(client);

            //QueryFilter queryFilter = null;

            //switch (queryOperator)
            //{
            //	case "Equal":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.Equal, queryString);
            //		break;
            //	case "GreaterThan":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.GreaterThan, queryString);
            //		break;
            //	case "GreaterThanOrEqual":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.GreaterThanOrEqual, queryString);
            //		break;
            //	case "LessThan":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.LessThan, queryString);
            //		break;
            //	case "LessThanOrEqual":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.LessThanOrEqual, queryString);
            //		break;
            //	case "BeginsWith":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.BeginsWith, queryString);
            //		break;
            //	case "Between":
            //		queryFilter = new QueryFilter(fieldToQuery, QueryOperator.Between, queryString);
            //		break;
            //}

            var search = _context.FromQueryAsync <Item>(new QueryOperationConfig()
            {
                //IndexName = "Name-index", //Taken from the DynamoDB dashboard when creating the Global Secondary Index.
                //Filter = new QueryFilter("Name", QueryOperator.Equal, "AwsDynamoDbTest app started")

                IndexName = fieldToQuery + "-index", //Taken from the DynamoDB dashboard when creating the Global Secondary Index.
                Filter    = new QueryFilter(fieldToQuery, QueryOperator.Equal, queryString)
            });

#if DEBUG
            //System.Diagnostics.Debug.WriteLine("QueryAsync() items retrieved:");

            int count          = 0;
            var searchResponse = await search.GetRemainingAsync();

            string itemData = null;

            searchResponse.ForEach((item) =>
            {
                count++;
                //System.Diagnostics.Debug.WriteLine(count + ". " + "Name = " + item.Name + ", Password = "******", Id = " + item.Id + ", SavedTimeStamp = " + item.SavedTimeStamp);
                itemData += count + ". " + "Name = " + item.Name + ", Password = "******", Id = " + item.Id + ", SavedTimeStamp = " + item.SavedTimeStamp + "\n";
            });

            System.Diagnostics.Debug.WriteLine("ReadItemEqualAsync() retrieved " + count + " items:\n" + itemData);
            count = 0;
#endif
            return(searchResponse);
        }