@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_two_way); FormModel formModel = new FormModel("123", "123"); mBinding.setModel(formModel); formModel.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int id/* Model 中 BR 属性的 id 值 */) { switch (id) { case BR.name: Toast.makeText(TwoWayActivity.this, "BR.name: " + id, Toast.LENGTH_SHORT).show(); break; case BR.password: Toast.makeText(TwoWayActivity.this, "BR.password: " + id, Toast.LENGTH_SHORT).show(); break; } } }); }
public AtomWatcher(BaseObservable... fields){ for(BaseObservable field: fields){ BaseObservable af = field; Observable.OnPropertyChangedCallback callback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { run(); } }; field.addOnPropertyChangedCallback(callback); closables.add(new ClosableAction(()->{ af.removeOnPropertyChangedCallback(callback); })); } }
public ItemViewHolder(ViewDataBinding binding) { super(binding); itemModel = new ItemModel(); ReflectionHelper.run(binding,"set",itemModel); itemModel.selected.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { if(!updateSelected) return; if(selectedItems == null) return; if(selectedItems.contains(model)){ if(allowMultipleSelection){ selectedItems.remove(model); } }else{ if(!allowMultipleSelection){ selectedItems.clear(); } selectedItems.add(model); } } }); }
public void setAnimationBody(final CustomAnimationBody animationBody) { this.models = AnimationBodyModelUtil.generateFor(animationBody); for (SeekBarModel model : models) { modelsMap.put(model.getValue(), model); model.getValue().addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { if (modelChangedCallback != null) { modelChangedCallback.onModelChanged(modelsMap.get(observable)); } } }); } notifyDataSetChanged(); }
public void setLayer(Layer layer) { if (this.layer != null && changedCallback != null) { this.layer.removeOnPropertyChangedCallback(changedCallback); } this.layer = layer; // Because other parts of the application are updating layer model objects directly, we // need to listen for changes to the model's properties and notify of those property // changes on this object (because they share property names we can get away with this). changedCallback = new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { notifyPropertyChanged(propertyId); } }; layer.addOnPropertyChangedCallback(changedCallback); }
public void setParent(Observable parent) { this.parent = parent; if (this.parent != null) { this.parent.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { if (i == BR.visible || i == BR.ancestorsVisible) { notifyPropertyChanged(BR.ancestorsVisible); } } }); } notifyPropertyChanged(BR.parent); notifyPropertyChanged(BR.parentDepth); }
public MovieViewModel(MoviesRepository moviesRepository) { this.moviesRepository = moviesRepository; mObservableMovie.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { Movie movie = mObservableMovie.get(); if (movie != null) { posterUrl.set(getPosterUrl(movie)); movieTitle.set(getTitle(movie)); backdropUrl.set(getBackdropUrl(movie)); voteAverage.set((float) movie.getVoteAverage()); voteCount.set(movie.getVoteCount()); movieOverview.set(movie.getOverview()); movieTagline.set(movie.getTagline()); movieReleaseDate.set(getReleaseDate(movie)); } } }); }
public PostActionVM() { sharedPreferences = ChaoliApplication.getAppContext().getSharedPreferences(TAG, MODE_PRIVATE); editor = sharedPreferences.edit(); title.set(sharedPreferences.getString(DRAFT_TITLE, "")); content.set(sharedPreferences.getString(DRAFT_CONTENT, "")); prevContent = content.get(); channelId.set(sharedPreferences.getInt(DRAFT_CHANNEL, Channel.caff.getChannelId())); curChannel = Channel.getChannel(channelId.get()); content.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { } }); }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); currentAudioCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { onCurrentAudioChanged(currentAudio.get()); } }; currentAlbumArtCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { getActivity().runOnUiThread(() -> { onCurrentAlbumArtChanged(currentAlbumArt.get()); }); } }; }
@Override public void onPropertyChanged(Observable observable, int i) { Paper.book().write("shuffle", shuffleSetting); } }; onRepeatChangedCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { Paper.book().write("repeat", repeatSetting); } }; shuffleSetting.addOnPropertyChangedCallback(onShuffleChangedCallback); repeatSetting.addOnPropertyChangedCallback(onRepeatChangedCallback); System.out.println("MusicService started"); }
private void logOnChange(OWDevice.DeviceCharacteristic deviceCharacteristic) { deviceCharacteristic.value.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { Timber.d(deviceCharacteristic.key.get() + " = " + deviceCharacteristic.value.get()); } }); }
/** * 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(); } }; }
@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)); }
private void init() { for (final ValidatedObservableField field : this.fields) { field.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { set(areAllFieldsValid()); } }); } set(areAllFieldsValid()); }
@Test public void notCallNotifyChangeForSetErrorMessageWithNullValue() { field.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { fail("Should not get here"); } }); field.setErrorMessage(null); }
@Test public void callNotifyChangeForSetErrorMessageWithNullValueIfPreviousErrorMessageWasNotNull() { final String newErrorMessage = "newErrorMessage"; field.setErrorMessage(newErrorMessage); field.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { assertNull(field.getErrorMessage()); } }); field.setErrorMessage(null); assertNull(field.getErrorMessage()); }
@Test public void notCallNotifyChangeForSetErrorMessageWithTheSameAsCurrentOne() { final String newErrorMessage = "newErrorMessage"; field.setErrorMessage(newErrorMessage); field.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { fail("Should not get here"); } }); field.setErrorMessage(newErrorMessage); }
public void setEpochCollector(EpochCollector collector) { this.collector = collector; this.collector.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { EpochSurfaceView.this.invalidate(); } } ); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize Muse first up. MuseManagerAndroid.getInstance().setContext(this); // Bind viewmodel to the view. ActivityRecordBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_record); binding.setRecordVM(viewModel); binding.setDeviceVM(deviceVM); // Bind action bar, seems like this can't be done in the layout :( deviceVM.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { RecordActivity.this.getSupportActionBar().setTitle( String.format("%s: %s", deviceVM.getName(), deviceVM.getConnectionState()) ); } }); // And attach the desired muse to the VM once connected. final String macAddress = getIntent().getExtras().getString("mac"); MuseManagerUtil.getByMacAddress(macAddress, new MuseManagerUtil.MuseCallback() { @Override public void museFound(Muse muse) { RecordActivity.this.deviceVM.setMuse(muse); } }); }
public RecordVM(Context context, StreamingDeviceViewModel device) { this.context = context; this.device = device; this.device.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { // TODO: Only propogate subset of properties? RecordVM.this.notifyChange(); } }); }
@BindingAdapter(value = {"clickCommand", "commandParameter"}, requireAll = false) public static void setAdapter(View button, AtomCommand command, Object parameter){ Observable.OnPropertyChangedCallback pc; AtomCommand oldCommand = (AtomCommand)button.getTag(R.id.atomCommand); if(oldCommand != command) { if(button instanceof Button) { if(oldCommand!=null) { pc = (Observable.OnPropertyChangedCallback) button.getTag(R.id.atomCommandEnabled); if (pc != null) { oldCommand.enabled.removeOnPropertyChangedCallback(pc); } } pc = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { ((Button)button).setEnabled(command.enabled.get()); } }; command.enabled.addOnPropertyChangedCallback(pc); } button.setTag(R.id.atomCommand, command); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { command.execute(parameter); } }); } command.parameter.set(parameter); }
public RadioRVAdapter(Context context) { this.context = context; checked.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { if (selectedListener != null) { final ObservableField observableField = (ObservableField) observable; selectedListener.onElementSelected((RadioElementModel) observableField.get()); } } }); }
/** * Exists as public method to allow Json deserializing to add color change listener after * deserializing */ public void addOnColorChangeListener() { this.color.addOnPropertyChangedCallback(new OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { ShapeLayer.this.notifyPropertyChanged(BR.color); } }); }
@Override public void bindViewModel(AddEditTaskViewModel viewModel) { checkNotNull(viewModel); this.viewModel = viewModel; snackbarCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { SnackbarUtils.showSnackbar(getView(), viewModel.getSnackbarText()); } }; viewModel.snackbarText.addOnPropertyChangedCallback(snackbarCallback); }
private void setupSnackbar() { snackbarCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int i) { SnackbarUtils.showSnackbar(getView(), viewModel.getSnackbarText()); } }; viewModel.snackbarText.addOnPropertyChangedCallback(snackbarCallback); }
@Override public final <T> void addSubscribe(io.reactivex.Observable<T> observable, Observer<T> observer) { if (null == observable || null == observer) { return; } observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); }
@SuppressWarnings("ConstantConditions") // @Test @UiThreadTest public void propertyChangeEvents() { boolean value = true; observableBoolean.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable observable, int propertyId) { // do something } }); RecordingObserver<Boolean> o = new RecordingObserver<>(); RxObservableBoolean.propertyChanges(observableBoolean).subscribe(o); o.assertNoMoreEvents(); observableBoolean.set(value); assertEquals(value, o.takeNext()); value = !value; observableBoolean.set(value); assertEquals(value, o.takeNext()); o.dispose(); value = !value; observableBoolean.set(value); o.assertNoMoreEvents(); }
public void bindVisible(final ObservableField<Boolean> visibleChangeField) { if (visibleChangeField.get() != null) setVisible(visibleChangeField.get()); visibleChangeCallback = new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { setVisible(visibleChangeField.get()); } }; visibleChangeField.addOnPropertyChangedCallback(visibleChangeCallback); this.visibleChangeField = visibleChangeField; }
@Override public void onPropertyChanged(Observable observable, int fieldId) { LegislatorSearchInputViewModel viewModel = (LegislatorSearchInputViewModel) observable; //Switch will not work on BR fields, c'est la vie. if (fieldId == BR.legislatorName) { //Don't show validation failure unless they've already failed validation by submitting. //Eagerly show validation acceptance if they have failed validation. if (!viewModel.isNameValid()) validateName(viewModel.getLegislatorName()); } else if (fieldId == BR.zipCode) { //Don't show validation failure unless they've already failed validation by submitting. //Eagerly show validation acceptance if they have failed validation. if (!viewModel.isZipValid()) validateZip(viewModel.getZipCode()); } }
@Test public void testOnPropertyChangedCallbacksAddedToViewModel() { final Observable.OnPropertyChangedCallback firstCallback = mock(Observable.OnPropertyChangedCallback.class); final Observable.OnPropertyChangedCallback secondCallback = mock(Observable.OnPropertyChangedCallback.class); activityController.create(); activity.setViewModel(activity.createViewModel()); activity.addOnPropertyChangedCallback(firstCallback); activity.addOnPropertyChangedCallback(secondCallback); verify(activity.getViewModel()).addOnPropertyChangedCallback(firstCallback); verify(activity.getViewModel()).addOnPropertyChangedCallback(secondCallback); }
@Test public void testRemoveOnPropertyChangedCallbacksOnActivityDestroy() { final Observable.OnPropertyChangedCallback firstCallback = mock(Observable.OnPropertyChangedCallback.class); final Observable.OnPropertyChangedCallback secondCallback = mock(Observable.OnPropertyChangedCallback.class); activityController.create(); activity.setViewModel(activity.createViewModel()); activity.addOnPropertyChangedCallback(firstCallback); activity.addOnPropertyChangedCallback(secondCallback); activity.onDestroy(); verify(activity.getViewModel()).removeOnPropertyChangedCallback(firstCallback); verify(activity.getViewModel()).removeOnPropertyChangedCallback(secondCallback); }
@Test public void testOnPropertyChangedCallbacksAddedToViewModel() { final Observable.OnPropertyChangedCallback firstCallback = mock(Observable.OnPropertyChangedCallback.class); final Observable.OnPropertyChangedCallback secondCallback = mock(Observable.OnPropertyChangedCallback.class); fragment.setViewModel(fragment.createViewModel()); fragment.addOnPropertyChangedCallback(firstCallback); fragment.addOnPropertyChangedCallback(secondCallback); verify(fragment.getViewModel()).addOnPropertyChangedCallback(firstCallback); verify(fragment.getViewModel()).addOnPropertyChangedCallback(secondCallback); }
@Test public void testRemoveOnPropertyChangedCallbacksOnActivityDestroy() { final Observable.OnPropertyChangedCallback firstCallback = mock(Observable.OnPropertyChangedCallback.class); final Observable.OnPropertyChangedCallback secondCallback = mock(Observable.OnPropertyChangedCallback.class); fragment.setViewModel(fragment.createViewModel()); fragment.addOnPropertyChangedCallback(firstCallback); fragment.addOnPropertyChangedCallback(secondCallback); SupportFragmentTestUtil.startFragment(fragment); fragment.onDestroy(); verify(fragment.getViewModel()).removeOnPropertyChangedCallback(firstCallback); verify(fragment.getViewModel()).removeOnPropertyChangedCallback(secondCallback); }
public static void addCallback(BaseActivity activity, Observable observable, Observable.OnPropertyChangedCallback callback) { ArrayMap<Observable, Observable.OnPropertyChangedCallback> localMap = mGlobalMap.get(activity); if (localMap == null) { localMap = new ArrayMap<>(); mGlobalMap.put(activity, localMap); } observable.addOnPropertyChangedCallback(callback); localMap.put(observable, callback); }
public static void removeCallbacks(BaseActivity activity) { ArrayMap<Observable, Observable.OnPropertyChangedCallback> localMap = mGlobalMap.get(activity); if (localMap != null) { for (Observable observable : localMap.keySet()) { observable.removeOnPropertyChangedCallback(localMap.get(observable)); } } }
private void onItemRangeInserted(int positionStart, int itemCount) { List<Integer> notifyIndices = new ArrayList<>(); for (int i = positionStart; i < positionStart + itemCount; ++i) { IndexWithRemover iwr = new IndexWithRemover(null); // register callback for receiving onPropertyChanged events of inserted elements T item = source.get(i); if (item instanceof Observable) { final Observable observable = (Observable) item; observable.addOnPropertyChangedCallback(itemChangedCallback); iwr.remover = new Remover() { @Override public void remove() { observable.removeOnPropertyChangedCallback(itemChangedCallback); } }; } indexList.add(i, iwr); if (getFilter().execute(item)) { int index = appearItem(i); if (index >= 0) { notifyIndices.add(index); } } } int notifyCount = notifyIndices.size(); if (registry != null && notifyCount > 0) { registry.notifyInserted(this, notifyIndices.get(0), notifyCount); } }
public CustomSwitcher(Context context, AttributeSet attrs) { super(context, attrs); this.binding = CustomSwitcherBinding.inflate(LayoutInflater.from(context), this, true); this.binding.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { @Override public void onPropertyChanged(Observable sender, int propertyId) { if (propertyId == BR.item) { setVm(binding.getItem()); } } }); }