Exemplo n.º 1
0
        private void DoWriteQueToView_func(List <KeyValuePair <ViewLink, object> > que)
        {
            xLog.Debug("start write " + que.Count + " values to view");
            int iDone = 0;

            IsWritingToView = true;
            foreach (var kv in que)
            {
                try
                {
                    var    view   = kv.Key.View;
                    var    prop   = kv.Key.Property;
                    object newVal = kv.Value;

                    if (prop.Name == nameof(View.Visibility))
                    {
                        if (newVal is bool)
                        {
                            newVal = (bool)newVal == true ? ViewStates.Visible : ViewStates.Gone;
                        }
                        else if (newVal is int)
                        {
                            newVal = (int)newVal > 0 ? ViewStates.Visible : ViewStates.Gone;
                        }
                    }

                    if (newVal is DateTime)
                    {
                        newVal = ((DateTime)newVal).ToShortDateString();
                    }
                    else if (newVal is TimeSpan)
                    {
                        newVal = ((TimeSpan)newVal).ToString(@"hh\:mm");
                    }

                    if (!Equals(prop.GetValue(view), newVal))
                    {
                        if (prop.CanWrite)
                        {
                            if (view is EditText && ViewToModelLinks.ContainsKey(view))
                            {
                                var cLinkId = ViewToModelLinks[view].ID;
                                if (LastViewValues.ContainsKey(cLinkId) && !Equals(prop.GetValue(view), LastViewValues[cLinkId]))
                                {
                                    if (sys.Debugmode && Activity != null)
                                    {
                                        Toast.MakeText(Activity, DateTime.Now.ToString("HH:mm:ss.fff") + " ignore value: " + newVal.ToString(), ToastLength.Short).Show();
                                    }
                                    continue;
                                }
                            }

                            int iPos = -1;
                            if (view is EditText)
                            {
                                iPos = (view as EditText).SelectionStart;
                            }

                            if (view is AutoCompleteTextView && nameof(AutoCompleteTextView.Text).Equals(prop.Name))
                            {
                                (view as AutoCompleteTextView).SetText((string)newVal, false);
                            }
                            else
                            {
                                prop.SetValue(view, newVal);
                            }

                            iDone++;
                            if (view is EditText)
                            {
                                if (iPos > 0)// && (view as EditText).Selected)
                                {
                                    (view as EditText).SetSelection(Math.Min(iPos, newVal.ToString().Length));
                                }

                                /*else if (!(view as EditText).Selected)
                                 *  Toast.MakeText(Activity, "not selected", ToastLength.Short).Show();
                                 * else
                                 *  Toast.MakeText(Activity, "other reason", ToastLength.Short).Show();*/
                            }
                        }
                        else if (view is Spinner)
                        {
                            switch (prop.Name)
                            {
                            case nameof(Spinner.SelectedItemPosition):
                                (view as Spinner).SetSelection((int)newVal);
                                iDone++;
                                break;

                            default:
                                throw new NotImplementedException("only SelectedItemPosition can be bound with a Spinner");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    sys.LogException(ex);
                }
            }
            IsWritingToView = false;
            xLog.Debug("writing finished, {iDone} values changed");
        }
Exemplo n.º 2
0
        private void StartViewChangeListener()
        {
            try
            {
                ViewToModelLinks.Clear();
                List <object> done = new List <object>();
                foreach (var vl in ViewLinks)
                {
                    try
                    {
                        if (done.Contains(vl.Value.View))
                        {
                            continue;
                        }
                        if (BindModes[vl.Value.ID] != BindMode.TwoWay)
                        {
                            continue;
                        }

                        var link = vl.Value;
                        if (link.View is CheckBox)
                        {
                            if (link.Property.Name != nameof(CheckBox.Checked))
                            {
                                continue;
                            }
                            (link.View as CheckBox).CheckedChange += CheckBox_CheckedChange;
                        }
                        if (link.View is Switch)
                        {
                            if (link.Property.Name != nameof(Switch.Checked))
                            {
                                continue;
                            }
                            (link.View as Switch).CheckedChange += Switch_CheckedChange;
                        }
                        if (link.View is EditText)
                        {
                            if (link.Property.Name != nameof(EditText.Text))
                            {
                                continue;
                            }
                            (link.View as EditText).AfterTextChanged += EditText_AfterTextChanged;
                            (link.View as EditText).SetOnEditorActionListener(this);
                            (link.View as EditText).FocusChange += EditText_FocusChange;
                        }
                        if (link.View is Spinner)
                        {
                            if (link.Property.Name != nameof(Spinner.SelectedItem) && link.Property.Name != nameof(Spinner.SelectedItemId) && link.Property.Name != nameof(Spinner.SelectedItemPosition))
                            {
                                continue;
                            }
                            (link.View as Spinner).ItemSelected += Spinner_ItemSelected;
                        }

                        ViewToModelLinks.Add(link.View, ObjectLinks[link.ID]);
                        done.Add(link.View);
                    }
                    catch (Exception ex)
                    {
                        sys.LogException(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                sys.LogException(ex);
            }
        }