Android Databinding TextEdit Validation of two or more fields enable button - validation

I am working on my first Android application using MVVM and Databinding. Some areas I am grasping but this one I am struggling with. The scenario:
I have a Create Account wizard activity, which uses Android Navigation Architecture to page through several fragments asking for input from the user. The first fragment/step asks the user for the first and last name. I do not want the button to proceed to the next step to enable until something is entered in both fields. I have enabled buttons based on ONE fields validation before, but not two. I feel like I am missing something silly.
Here is the button I want to enable after both fields have data in them:
<Button
android:id="#+id/continueToSecondStepButton"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:background="#drawable/button_transparent_background"
android:onClick="#{() -> viewModel.proceedToNextStep()}"
android:text="#string/step_proceed"
android:enabled="#{safeUnbox(viewModel.firstNamesValid) && safeUnbox(viewModel.lastNamesValid)}"
android:textAllCaps="false"
android:textColor="#{safeUnbox(viewModel.firstNamesValid) && safeUnbox(viewModel.lastNamesValid) ? #colorStateList/white : #colorStateList/transparent_white}"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subtext" />
Primarily here the focus is:
android:enabled="#{safeUnbox(viewModel.firstNamesValid) && safeUnbox(viewModel.lastNamesValid)}"
android:textColor="#{safeUnbox(viewModel.firstNamesValid) && safeUnbox(viewModel.lastNamesValid) ? #colorStateList/white : #colorStateList/transparent_white}"
I have two Transformations to listen to key changes on the first and last name fields and execute the validation method:
firstNamesValid = Transformations.switchMap(firstName) { firstName -> isNamesValid() }
lastNamesValid = Transformations.switchMap(lastName) { lastName -> isNamesValid() }
and for now, just a simple method to check if both fields have data in them:
private fun isNamesValid(): LiveData<Boolean> {
var namesValid = false
if (!firstName.value.isNullOrEmpty() && !lastName.value.isNullOrEmpty()) {
namesValid = true
}
val mediatorLiveData: MediatorLiveData<Boolean> = MediatorLiveData()
mediatorLiveData.value = namesValid
return mediatorLiveData
}
It "kind of" works but not well. Can you enable a button based on validating two fields with Databinding? I have a feeling there is an easier way to do this. What happens with this setup is that, you fill out first and last name, and nothing happens, but if you then navigate back to the first name and enter another character it works and enables the button. I assume this is due to some logic error in my code. Thanks for looking.

I ended up figuring it out right after. Typing it up helped me find the issue and I will share what I did for others. I had a logic error in my code referencing the isNamesValid function.
I made the following changes:
isNamesValid now takes a string parameter and checks the specific string rather then hardcoded checking the first and last name values
private fun isNameValid(name: String?): LiveData<Boolean> {
var namesValid = false
if (!name.isNullOrEmpty()) {
namesValid = true
}
val mediatorLiveData: MediatorLiveData<Boolean> = MediatorLiveData()
mediatorLiveData.value = namesValid
return mediatorLiveData
}
Updated Transformations to call that method passing in the name to be checked.
firstNamesValid = Transformations.switchMap(firstName) { firstName -> isNameValid(firstName) }
lastNamesValid = Transformations.switchMap(lastName) { lastName -> isNameValid(lastName) }

Related

Is there a way to use ChoicePrompt without validating choices?

I would like to present the user with a series of choices, but also allow them to type in freeform text. The Choice prompt will automatically reprompt until a choice or synonym is chosen.
The RecognizerOptions NoValue and/or NoAction appear to be related to this, but I haven't been able to find good documentation on them. Setting them to true doesn't work.
AddDialog(new ChoicePrompt(promptForChoice) { RecognizerOptions = new FindChoicesOptions() { NoValue = true, NoAction = true } });
I've also tried creating an "anything" validator that always returns true.
AddDialog(new ChoicePrompt(promptForChoice, validator: AnythingValidator.AnythingValidatorAsync) { RecognizerOptions = new FindChoicesOptions() { NoValue = true, NoAction = true } });
public static Task<bool> AnythingValidatorAsync(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
This allows the prompt to exit, but the result is null. I can go dig out what the user entered from the Context.Activity.Text but that doesn't seem like a very robust solution.
There seems to be something obvious I'm missing with PromptChoice
Choices work by hardcoding in the options the user needs to choose from. We can't implement freeform text in choices. What you can do is, add another choice "others" in the choice list and implement a waterfall to get the user input. Also, you can't use the RecognizerOptions as they are related to synonyms.

In MVVMCross 5, how can I navigate backward multiple pages?

In this example, a unique Person is defined by their FirstName and LastName. PageA is a form that selects a unique Person. PageB is a list of unique FirstNames, and PageC is a list of all the LastNames that exist for a given FirstName.
I'm having a hard time solving a particular UX pattern using MvxNavigationService. Here's what I'm attempting to do, (psuedocode):
PageA.SelectedItem = NavigateTo(PageB) [list of Person, grouped
by Person.FirstName];
PageB.SelectedItem = NavigateTo(PageC) [for
Person.FirstName, list of Person.LastName, ];
PageC.Close(SelectedItem);
PageB.Close(SelectedItem);
When I actually try and implement this and run it on Android, the viewmodel logic executes, but the UI doesn't show PageA.
Update: Calling PageB.Close() navigates back to PageC, since PageC was the previous page. Perhaps the problem could be solved by ensuring that PageC is removed from the stack upon closing it. How might this be accomplished?
There's many ways of doing this, using a Custom ViewPresenter on iOS or using an Activity on Android.
One way I achieved this for a small pair (2 view models) was by adding an Instance static variable to the first ViewModel that opens the second ViewModel, like this:
public class FirstViewModel
{
public static FirstViewModel Instance;
public void FirstViewModel()
{
Instance = this;
...
}
}
And then in the second ViewModel's save/close command, I just closed both ViewModels like this and it worked:
public new MvvmCross.Commands.IMvxCommand SaveClickCommand
{
get
{
return new MvvmCross.Commands.MvxAsyncCommand(
async () =>
{
await Navigator.Close(this);
await Navigator.Close(FirstViewModel.Instance);
}
);
}
}

Make changes to buttons and labels in an XML Ribbon during run-time

I know there are several topics already on stackoverflow, but nothing that actually solves the problem. Here it is:
Because of some inherent problems with Ribbon Designer I decided to build my next Excel AddIn using XML Ribbon.
However, occasionally I need to make changes to the controls in the ribbon based on user selections. For example I need to change the text of a label, and also make some of the controls disabled in some cases. And here's where I hit a brick wall. It looks like there's no way to do it. I tried to put the logic in the onAction callback as follows:
public void LabelAction(IRibbonControl control)
{
LabelControl label = (LabelControl)control;
label.Label = "changed text";
}
But this cast doesn't work because apparently IRibbonControl interface has nothing to do with the RibbonControl class that LabelConrol inherits from.
I was also not able to find any other way to access any of the XML ribbon controls. Is there even a solution to this? Or should I stick to Ribbon Designer?
You need to do this in a routine that sets the item label.
The xml would look like this:
<button id="SkLabelTest1" getLabel="GetLabelTest" onAction="SkLabelTest1"/>
<button id="SkLabelTest2" getLabel="GetLabelTest" onAction="SkLabelTest2"/>
The routine you are interested in is getLabel
I've done a noddy routine to demonstrate this.
First I added a property to ThisAddin.cs for it to read:
public string _labelTest = string.Empty;
public string LabelTest { get { return _labelTest; } set { _labelTest = value; } }
Then in my ribbon handling code I added the getLabel routine:
public string GetLabelTest(Office.IRibbonControl control)
{
switch (control.Id.ToLower())
{
case "sklabeltest2":
if (Globals.ThisAddIn.LabelTest != string.Empty)
return Globals.ThisAddIn.LabelTest;
else
return "Label Test 2";
default:
return "Label Test 1";
}
}
This works by the SkLabelTest1 button changing the text of SkLabelTest2 and then invalidating the control to force the ribbon to reload it:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
Globals.ThisAddIn._ribbon.InvalidateControl("SkLabelTest2");
}
I've tested just in case and it changes the text OK. Hope this helps
I couldn't make a comment because of my reputation. As a comment to Charlie's post, it is a perfect solution but on my side, I had to change one part.
I changed public void SklabelTest1 function to this one below:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
this.ribbon.InvalidateControl("SkLabelTest2");
}
And also added this in the beginning of my ribbon class.
private Office.IRibbonUI ribbon;
I hope it helps.

JavaFX: Prevent selection of a different tab if the data validation of the selected tab fails

I'm creating a CRUD application that store data in a local h2 DB. I'm pretty new to JavaFX. I've created a TabPane to with 3 Tab using an jfxml created with Scene Builder 2.0. Each Tab contains an AncorPane that wrap all the controls: Label, EditText, and more. Both the TabPane and the Tabs are managed using one controller. This function is used to create and to update the data. It's called from a grid that display all the data. A pretty basic CRUD app.
I'm stuck in the validation phase: when the user change the tab, by selecting another tab, it's called a validation method of the corresponding tab. If the validation of the Tab fails, I want that the selection remains on this tab.
To achieve this I've implemented the following ChangeListener on the SelectionModel of my TabPane:
boolean processingTabValidationOnChange = false;
tabPane.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
if (processingTabValidationOnChange == false) {
boolean success;
switch (t.intValue()) {
case 0: success = validationTab1Passed();
break;
case 1: success = validationTab2Passed();
break;
case 1: success = validationTab3Passed();
break;
default: success = false;
}
if (success == false) {
processingTabValidationOnChange = true;
// select the previous tab
tabPane.getSelectionModel().select(t.intValue());
processingTabValidationOnChange = false;
}
}
}
});
I'm not sure that this is the right approach because:
The event changed is fired two times, one for the user selection and one for the .select(t.intValue()). To avoid this I've used a global field boolean processingTabValidationOnChange... pretty dirty I know.
After the .select(t.intValue()) the TabPane displays the correctly Tab as selected but the content of the tab is empty as if the AnchorPane was hidden. I cannot select again the tab that contains the errors because it's already selected.
Any help would be appreciated.
Elvis
I would approach this very differently. Instead of waiting for the user to select a different tab, and reverting if the contents of the current tab are invalid, prevent the user from changing tabs in the first place.
The Tab class has a disableProperty. If it is set to true, the tab cannot be selected.
Define a BooleanProperty or BooleanBinding representing whether or not the data in the first tab is invalid. You can create such bindings based on the state of the controls in the tab. Then bind the second tab's disableProperty to it. That way the second tab automatically becomes disabled or enabled as the data in the first tab becomes valid or invalid.
You can extend this to as many tabs as you need, binding their properties as the logic dictates.
Here's a simple example.
Update: The example linked above is a bit less simple now. It will dynamically change the colors of the text fields depending on whether the field is valid or not, with validation rules defined by bindings in the controller. Additionally, there are titled panes at the top of each page, with a title showing the number of validation errors on the page, and a list of messages when the titled pane is expanded. All this is dynamically bound to the values in the controls, so it gives constant, clear, yet unobtrusive feedback to the user.
As I commented to the James's answer, I was looking for a clean solution to the approach that I've asked. In short, to prevent the user to change to a different tab when the validation of the current tab fails. I proposed a solution implementing the ChangeListener but, as I explained: it's not very "clean" and (small detail) it doesn't work!
Ok, the problem was that the code used to switch back the previous tab:
tabPane.getSelectionModel().select(t.intValue());
is called before the process of switching of the tab itself it's completed, so it ends up selected... but hidden.
To prevent this I've used Platform.runLater(). The code .select() is executed after the change of tab. The full code becomes:
//global field, to prevent validation on .select(t.intValue());
boolean skipValidationOnTabChange = false;
tabPane.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
if (skipValidationOnTabChange == false) {
boolean success;
switch (t.intValue()) {
case 0:
success = validationTab1Passed();
break;
case 1:
success = validationTab2Passed();
break;
case 1:
success = validationTab3Passed();
break;
default:
success = false;
}
if (success == false) {
Platform.runLater(new Runnable() {
#Override
public void run() {
skipValidationOnTabChange = true;
tabPane.getSelectionModel().select(t.intValue());
skipValidationOnTabChange = false;
}
});
}
}
}
});
Anyway, if anyone has a better solution to accomplish this, you're welcome. In example using a method like consume() to prevent the tab to be selected two times. This way I can eliminated the global field skipValidationOnTabChange.
Elvis
I needed to achieve the similar thing. I've done this by changing the com.sun.javafx.scene.control.behavior.TabPaneBehaviour class by overriding selectTab method:
class ValidatingTabPaneBehavior extends TabPaneBehavior {
//constructors etc...
#Override
public void selectTab(Tab tab) {
try {
Tab current = getControl().getSelectionModel().getSelectedItem();
if (current instanceof ValidatingTab) {
((ValidatingTab) current).validate();
}
//this is the method we want to prevent from running in case of error in validation
super.selectTab(tab);
}catch (ValidationException ex) {
//show alert or do nothing tab won't be changed
}
}
});
The ValidatingTab is my own extension to Tab:
public class ValidatingTab extends Tab {
public void validate() throws ValidationException {
//validation
}
}
This is the "clean part" of the trick. Now we need to place ValidatingTabPaneBehavior into TabPane.
First you need to copy (!) the whole com.sun.javafx.scene.control.skin.TabPaneSkin to the new class in order to change its constructor. It is quite long class, so here is only the part when I switch the Behavior class:
public class ValidationTabPaneSkin extends BehaviorSkinBase<TabPane, TabPaneBehavior> {
//copied private fields
public ValidationTabPaneSkin(TabPane tabPane) {
super(tabPane, new ValidationTabPaneBehavior(tabPane));
//the rest of the copied constructor
}
The last thing is to change the skin in your tabPane instance:
tabPane.setSkin(new ValidationTabPaneSkin(tabPane));

ViewModels and IsolatedStorageSettings

Im working on a MVVM Windows phone app that displays weather info.
When the app loads up it opens MainPage.xaml. It makes a call the the service to get weather info and binds that data to the UI. Both Fahrenheit and Celcius info are returned but only one is displayed.
On the setting page, the user can select to view the temp in either Fahrenheit or Celcius.
The user can change this setting at any time and its stored in IsolatedStorageSettings.
The issue Im having is this:
when the user navigates to the Settings page and changes their preference for either Fahrenheit or Celcius, this change is not reflected on the main page.
This issue started me thinking about this in a broader context. I can see this being an issue in ANY MVVM app where the display depends on some setting in IsolatedStorage. Any time any setting in the IsoStore is updated, how does the ViewModels know this? When I navigate back in the NavigationStack from the settings page back to MainPage how can I force a rebind of the page?
The data in my model hasnt changed, only the data that I want to display has changed.
Am I missing something simple here?
Thanks in advance.
Alex
Probably you have code like this:
public double DisplayTemperature
{
get { return (IsCelsium) ? Celsium : Fahrenheit; }
}
And IsCelsium is:
public double IsCelsium
{
get { return (bool)settings["IsCelsium"]; }
set { settings["IsCelsium"] = value; }
}
So you need to add NotifyPropertyChanged event to notify UI to get new values from DisplayTemperature property:
public double IsCelsium
{
get { return (bool)settings["IsCelsium"]; }
set
{
settings["IsCelsium"] = value;
NotifyPropertyChanged("DisplayTemperature");
}
}
Take a look at Caliburn Micro. You could implement something similar or use CM itself. When using CM I don't even think about this stuff, CM makes it so simple.
When your ViewModel inherits from Screen there are life-cycle events that fire that you can override. For example, OnInitialize fires the very first time the ViewModel is Activated and OnActivate fires every time the VM is activated. There's also OnViewAttached and OnViewLoaded.
These methods are the perfect place to put logic to populate or re-populate data.
CM also has some special built in features for allowing one to easily tombstone a single property or an entire object graph into Iso or phone state.
ok, so Ive come up with a solution. Before I get to it, let me provide some background. The app that Im working on uses both MVVM Light and WP7Contrib. That being the case, I am using Funq for DI and the MVVMLight Toolkit. After I posted my initial question, I gave the question a bit more thought. I remembered a video that I watched a while back from MIX2011 called Deep Dive MVVM with Laurent Bugnion
http://channel9.msdn.com/Events/MIX/MIX11/OPN03
In it, he talks about just this problem (view models not living at the same time) on Windows Phone. The part in question starts around the 19 minute mark.
Anyway, after I remembered that and realized that the ViewModel locator is exposed in App.xaml, this became a trivial problem to solve. When the user changes the Fahrenheit/Celcius option on the setting page, I simply get a reference to the MainViewModel via the ViewModelLocator and reset the collection that is bound to the UI thus causing the bindings to update.
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
public bool ImperialSetting
{
get
{
return GetValueOrDefault<bool>(ImperialSettingKeyName, ImperialSettingDefault);
}
set
{
if (AddOrUpdateValue(ImperialSettingKeyName, value))
{
Save();
RaisePropertyChanged("ImperialSettingText");
var vml = new ViewModelLocator();
vml.MainViewModel.Cities = (App.Current as App).Cities;
}
}
}
It was a mistake on my part not to realize that I could get access to the viewModel via the ViewModelLocator. Hopefully this post saves someone else the time I burned on this issue.

Resources