androidx.databinding.Observable Java Examples

The following examples show how to use androidx.databinding.Observable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FilterHolder.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void bind() {
    binding.setVariable(BR.currentFilter, openFilter);
    binding.setVariable(BR.filterType, filterType);
    binding.setVariable(BR.filterCount, FilterManager.getInstance().observeField(filterType));
    binding.executePendingBindings();

    itemView.setOnClickListener(view ->
            openFilter.set(openFilter.get() != filterType ? filterType : null)
    );

    openFilter.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            filterArrow.animate().scaleY(openFilter.get() != filterType ? 1 : -1).setDuration(200).start();
        }
    });
}
 
Example #2
Source File: ValiFieldBase.java    From valifi with Apache License 2.0 5 votes vote down vote up
/**
 * Core of all validations.
 * Called when value changed (from layout or by notifyPropertyChanged)
 * 1) notifies bound fields (e.g. when verifying fields)
 * 2) checks if value can be empty
 * 3) validates all attached blocking validators
 * 4) validates attached asynchronous validators
 *
 * @return property changed callback which should be set as field
 */
private OnPropertyChangedCallback setupOnPropertyChangedCallback() {
	return new OnPropertyChangedCallback() {
		@Override
		public void onPropertyChanged(Observable observable, int brId) {
			if (brId != com.mlykotom.valifi.BR.value || mIsResetting) return;

			// ad 1) notifying bound fields
			notifyBoundFieldsValueChanged();

			// ad 2) checking if value can be empty
			if (mIsEmptyAllowed && (mValue == null || whenThisFieldIsEmpty(mValue))) {
				setIsError(false, null);
				return;
			}

			cancelAndSetValidationTask(null);
			setInProgress(true);

			// ad 3) validating synchronous validators
			if (!checkBlockingValidators()) {
				setInProgress(false);
				cancelAndSetValidationTask(null);
				return;
			}

			// ad 4) validating asynchronous validators
			checkAsyncValidatorsIfAny();
		}
	};
}
 
Example #3
Source File: ValiFieldBaseTest.java    From valifi with Apache License 2.0 5 votes vote down vote up
@Test
public void checkDelayedError() throws InterruptedException {
	final long delay_time = 700;

	final ValiFieldBase<String> field = new ValiFieldText("invalid_length_12345");
	field.setErrorDelay(delay_time);
	assertThat(field.mErrorDelay, is(delay_time));

	final boolean[] testIsOk = {false};
	field.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
		@Override
		public void onPropertyChanged(Observable observable, int i) {
			if (i != com.mlykotom.valifi.BR.error) return;

			testIsOk[0] = true;
		}
	});

	field.addCustomValidator(ValiFiTest.FIELD_ERROR_MSG, new ValiFieldBase.PropertyValidator<String>() {
		@Override
		public boolean isValid(@Nullable String value) {
			return false;
		}
	});

	assertThat(field.isValid(), is(false));

	Thread.sleep(delay_time + 100);

	assertThat(field.getError(), is(ValiFiTest.FIELD_ERROR_MSG));
	assertThat(field.mDueTime, is(-1L));
	assertThat(testIsOk[0], is(true));
}
 
Example #4
Source File: AddEditTaskFragment.java    From simple-stack with Apache License 2.0 5 votes vote down vote up
@Override
public void bindViewModel(AddEditTaskViewModel viewModel) {
    checkNotNull(viewModel);
    if(this.viewModel == viewModel) {
        return;
    }
    this.viewModel = viewModel;
    snackbarCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable observable, int i) {
            SnackbarUtils.showSnackbar(getView(), viewModel.getSnackbarText());
        }
    };
    viewModel.snackbarText.addOnPropertyChangedCallback(snackbarCallback);
}
 
Example #5
Source File: TaskDetailFragment.java    From simple-stack with Apache License 2.0 5 votes vote down vote up
private void setupSnackbar() {
    snackbarCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable observable, int i) {
            SnackbarUtils.showSnackbar(getView(), viewModel.getSnackbarText());
        }
    };
    viewModel.snackbarText.addOnPropertyChangedCallback(snackbarCallback);
}
 
Example #6
Source File: BindingViewItem.java    From recyclerview-adapters with Apache License 2.0 5 votes vote down vote up
public void bindVisible(final ObservableField<Boolean> visibleChangeField) {
    if (visibleChangeField.get() != null) setVisible(visibleChangeField.get());

    releaseVisibleChangeBinding();

    visibleChangeCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            setVisible(visibleChangeField.get(), false);
        }
    };
    visibleChangeField.addOnPropertyChangedCallback(visibleChangeCallback);
    this.visibleChangeField = visibleChangeField;
}
 
Example #7
Source File: DataSourceBindingViewItem.java    From recyclerview-adapters with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    callback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            notifyDataSetChanged();
        }
    };
    source.addOnPropertyChangedCallback(callback);
}
 
Example #8
Source File: BindingItemAdapter.java    From recyclerview-adapters with Apache License 2.0 5 votes vote down vote up
public void bindVisible(final ObservableField<Boolean> visibleChangeField) {
    if (visibleChangeField.get() != null) setVisible(visibleChangeField.get());

    releaseVisibleChangeBinding();

    visibleChangeCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            setVisible(visibleChangeField.get(), false);
        }
    };
    visibleChangeField.addOnPropertyChangedCallback(visibleChangeCallback);
    this.visibleChangeField = visibleChangeField;
}
 
Example #9
Source File: TasksFragment.java    From simple-stack with Apache License 2.0 4 votes vote down vote up
@Override
public void onPropertyChanged(Observable observable, int i) {
    if(!Strings.isNullOrEmpty(tasksViewModel.getSnackbarText())) {
        SnackbarUtils.showSnackbar(getView(), tasksViewModel.getSnackbarText());
    }
}
 
Example #10
Source File: TasksFragment.java    From simple-stack with Apache License 2.0 4 votes vote down vote up
@Override
public void onPropertyChanged(Observable sender, int propertyId) {
    View fab = getActivity().findViewById(R.id.fab);
    boolean visible = tasksViewModel.tasksAddViewVisible.get();
    fab.setVisibility(visible ? View.VISIBLE : View.GONE); // TODO: why doesn't this work?
}