android.databinding.ObservableField Java Examples

The following examples show how to use android.databinding.ObservableField. 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: BindingAdapters.java    From Flubber with Apache License 2.0 6 votes vote down vote up
@BindingAdapter({"checked", "model"})
public static <T> void setChecked(RadioButton radioButton, final ObservableField<T> checked, final T model) {

    if (checked == null) {
        return;
    }

    radioButton.setOnCheckedChangeListener(
            (buttonView, isChecked) -> {
                if ((checked.get() == null || !checked.get().equals(model))
                        && isChecked) {

                    checked.set(model);
                }
            });

    final T checkedModel = checked.get();
    final boolean shouldBeChecked = checkedModel != null && checkedModel.equals(model);

    if (shouldBeChecked != radioButton.isChecked()) {
        radioButton.setChecked(shouldBeChecked);
    }
}
 
Example #2
Source File: RepositoryViewModel.java    From archi with Apache License 2.0 6 votes vote down vote up
public RepositoryViewModel(Context context, final Repository repository) {
    this.repository = repository;
    this.context = context;
    this.ownerName = new ObservableField<>();
    this.ownerEmail = new ObservableField<>();
    this.ownerLocation = new ObservableField<>();
    this.ownerLayoutVisibility = new ObservableInt(View.INVISIBLE);
    this.ownerEmailVisibility = new ObservableInt(View.VISIBLE);
    this.ownerLocationVisibility = new ObservableInt(View.VISIBLE);
    // Trigger loading the rest of the user data as soon as the view model is created.
    // It's odd having to trigger this from here. Cases where accessing to the data model
    // needs to happen because of a change in the Activity/Fragment lifecycle
    // (i.e. an activity created) don't work very well with this MVVM pattern.
    // It also makes this class more difficult to test. Hopefully a better solution will be found
    loadFullUser(repository.owner.url);
}
 
Example #3
Source File: RxUtils.java    From android-data-binding-rxjava with MIT License 6 votes vote down vote up
public static <T> Observable<T> toObservable(@NonNull final ObservableField<T> observableField) {
    return Observable.fromEmitter(asyncEmitter -> {

        final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) {
                if (dataBindingObservable == observableField) {
                    asyncEmitter.onNext(observableField.get());
                }
            }
        };

        observableField.addOnPropertyChangedCallback(callback);

        asyncEmitter.setCancellation(() -> observableField.removeOnPropertyChangedCallback(callback));

    }, AsyncEmitter.BackpressureMode.LATEST);
}
 
Example #4
Source File: RxObservableField.java    From okuki with Apache License 2.0 6 votes vote down vote up
public static <T> Observable<T> toObservable(@NonNull final ObservableField<T> field) {

        return Observable.create(e -> {
            T initialValue = field.get();
            if (initialValue != null) {
                e.onNext(initialValue);
            }
            final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() {
                @Override
                public void onPropertyChanged(android.databinding.Observable observable, int i) {
                    e.onNext(field.get());
                }
            };
            field.addOnPropertyChangedCallback(callback);
            e.setCancellable(() -> field.removeOnPropertyChangedCallback(callback));
        });
    }
 
Example #5
Source File: SignInActivityViewModel.java    From Anago with Apache License 2.0 6 votes vote down vote up
@Inject
public SignInActivityViewModel(BaseActivity activity, SignInUseCase signInUseCase,
                               CheckSessionUseCase checkSessionUseCase) {
    super(activity);

    this.signInUseCase = signInUseCase;
    this.checkSessionUseCase = checkSessionUseCase;

    name = new ObservableField<>();
    password = new ObservableField<>();
    buttonEnabled = new ObservableBoolean(false);

    checkSessionUseCase.run()
            .compose(bindToLifecycle().forSingle())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(authToken -> {
                Timber.v("Check session: " + authToken.token);
                Toast.makeText(getContext(), "Already signed in", Toast.LENGTH_SHORT).show();
                goToNext();
            }, Timber::e);
}
 
Example #6
Source File: ListServerViewModel.java    From SimpleFTP with MIT License 5 votes vote down vote up
/**
 * Default constructor.
 * @param context The context of the current activity.
 */
public ListServerViewModel(Activity context) {
    this.context = context;
    this.servers = new ObservableArrayList<FtpServerViewModel>();
    this.selectedServer = new ObservableField<FtpServerViewModel>();
    this.selectedServerVisible = new ObservableBoolean();
}
 
Example #7
Source File: HomeActivity.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void onPropertyChanged(@NonNull final Observable sender, final int propertyId) {
    //noinspection unchecked
    final ObservableField<NavigationItem> o = (ObservableField<NavigationItem>) sender;
    final NavigationItem value = o.get();
    if (value != null) {
        navigateTo(value);
    }
}
 
Example #8
Source File: NavigationItemObservableFieldConverter.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public ObservableField<NavigationItem> fromParcel(@NonNull final Parcel parcel) {
    final int value = parcel.readInt();
    switch (value) {
        case VALUE_NULL:
            return null;

        case VALUE_NULL_CONTENTS:
            return new ObservableField<>();

        default:
            return new ObservableField<>(NavigationItem.values()[value]);
    }
}
 
Example #9
Source File: NavigationItemObservableFieldConverter.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void toParcel(
        @Nullable final ObservableField<NavigationItem> input,
        @NonNull final Parcel parcel) {
    final int value;
    if (input == null) {
        value = VALUE_NULL;
    } else if (input.get() == null) {
        value = VALUE_NULL_CONTENTS;
    } else {
        value = input.get().ordinal();
    }
    parcel.writeInt(value);
}
 
Example #10
Source File: RepoListItemViewModel.java    From Anago with Apache License 2.0 5 votes vote down vote up
@Inject
public RepoListItemViewModel(BaseFragment fragment,
                             StarUseCase starUseCase,
                             UnstarUseCase unstarUseCase,
                             EventBus eventBus) {
    super(fragment);
    this.starUseCase = starUseCase;
    this.unstarUseCase = unstarUseCase;
    this.eventBus = eventBus;

    this.repo = new ObservableField<>();
    this.starred = new ObservableBoolean(false);
}
 
Example #11
Source File: RepoInfoFragmentViewModel.java    From Anago with Apache License 2.0 5 votes vote down vote up
@Inject
public RepoInfoFragmentViewModel(BaseFragment fragment, GetRepoUseCase getRepoUseCase,
                                 CheckStarUseCase checkStarUseCase, StarUseCase starUseCase,
                                 UnstarUseCase unstarUseCase, EventBus eventBus) {
    super(fragment);
    this.getRepoUseCase = getRepoUseCase;
    this.checkStarUseCase = checkStarUseCase;
    this.starUseCase = starUseCase;
    this.unstarUseCase = unstarUseCase;
    this.eventBus = eventBus;

    this.repo = new ObservableField<>();
    this.isConnecting = new ObservableBoolean(true);
    this.starred = new ObservableBoolean(false);
}
 
Example #12
Source File: TextWatcherAdapter.java    From Learning-Resources with MIT License 5 votes vote down vote up
public TextWatcherAdapter(ObservableField<String> f) {
    this.field = f;

    field.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback(){
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            if (isInEditMode){
                return;
            }
            value.set(field.get());
        }
    });
}
 
Example #13
Source File: UserActivityViewModel.java    From Anago with Apache License 2.0 5 votes vote down vote up
@Inject
public UserActivityViewModel(BaseActivity activity, GetUserUseCase getUserUseCase, EventBus eventBus) {
    super(activity);
    this.getUserUseCase = getUserUseCase;
    this.eventBus = eventBus;

    this.user = new ObservableField<>();
    this.isConnecting = new ObservableBoolean(true);
}
 
Example #14
Source File: FieldUtilsTest.java    From android-mvvm with Apache License 2.0 5 votes vote down vote up
@Test
public void doesNotEmitInitialNull() throws Exception {
    testObserver = toObservable(new ObservableField<>((Integer) null)).test();

    testObserver.assertNoValues()
            .assertNoErrors();
}
 
Example #15
Source File: CampusCardViewModel.java    From Sunshine with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeInit() {
    //不知道为什么不可以在类中初始化
    month = new ObservableField<>();
    expenses = new ObservableField<>();
    balance = new ObservableField<>();

    month.set(DateUtil.getCurrentMonth());
}
 
Example #16
Source File: ObservableFieldTest.java    From parceler with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservable() {
    ObservableTestTarget target = new ObservableTestTarget();

    target.test = new ObservableField<TestParcel>(new TestParcel());
    target.test.get().value = "test";

    ObservableTestTarget output = Parcels.unwrap(ParcelsTestUtil.wrap(target));

    assertEquals("test", output.test.get().value);
}
 
Example #17
Source File: MainViewModel.java    From archi with Apache License 2.0 5 votes vote down vote up
public MainViewModel(Context context, DataListener dataListener) {
    this.context = context;
    this.dataListener = dataListener;
    infoMessageVisibility = new ObservableInt(View.VISIBLE);
    progressVisibility = new ObservableInt(View.INVISIBLE);
    recyclerViewVisibility = new ObservableInt(View.INVISIBLE);
    searchButtonVisibility = new ObservableInt(View.GONE);
    infoMessage = new ObservableField<>(context.getString(R.string.default_info_message));
}
 
Example #18
Source File: RadioRVAdapter.java    From Flubber with Apache License 2.0 5 votes vote down vote up
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());
            }
        }
    });
}
 
Example #19
Source File: FieldUtils.java    From android-mvvm with Apache License 2.0 5 votes vote down vote up
@NonNull
public static <T> rx.Observable<T> toObservable(@NonNull final ObservableField<T> field) {

    return RxJavaInterop.toV1Observable(
            com.manaschaudhari.android_mvvm.FieldUtils.toObservable(field),
            BackpressureStrategy.LATEST);
}
 
Example #20
Source File: FieldUtils.java    From android-mvvm with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an ObservableField to an Observable. Note that setting null value inside
 * ObservableField (except for initial value) throws a NullPointerException.
 * @return Observable that contains the latest value in the ObservableField
 */
@NonNull
public static <T> Observable<T> toObservable(@NonNull final ObservableField<T> field) {

    return Observable.create(new ObservableOnSubscribe<T>() {
        @Override
        public void subscribe(final ObservableEmitter<T> e) throws Exception {
            T initialValue = field.get();
            if (initialValue != null) {
                e.onNext(initialValue);
            }
            final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() {
                @Override
                public void onPropertyChanged(android.databinding.Observable observable, int i) {
                    e.onNext(field.get());
                }
            };
            field.addOnPropertyChangedCallback(callback);
            e.setCancellable(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    field.removeOnPropertyChangedCallback(callback);
                }
            });
        }
    });
}
 
Example #21
Source File: ContentListItemViewModel.java    From Anago with Apache License 2.0 4 votes vote down vote up
@Inject
public ContentListItemViewModel(BaseFragment fragment) {
    super(fragment);
    content = new ObservableField<>();
}
 
Example #22
Source File: AppModule.java    From vk_music_android with GNU General Public License v3.0 4 votes vote down vote up
@Provides
@Singleton
ObservableField<VKApiAudio> provideCurrentAudio() {
    return Paper.book().read("currentAudio", new ObservableField<>());
}
 
Example #23
Source File: StargazerListItemViewModel.java    From Anago with Apache License 2.0 4 votes vote down vote up
@Inject
public StargazerListItemViewModel(BaseActivity activity) {
    super(activity);

    this.user = new ObservableField<>();
}
 
Example #24
Source File: AppModule.java    From vk_music_android with GNU General Public License v3.0 4 votes vote down vote up
@Provides
@Singleton
ObservableField<Bitmap> provideCurrentAlbumArt() {
    return new ObservableField<>();
}
 
Example #25
Source File: IssueListItemViewModel.java    From Anago with Apache License 2.0 4 votes vote down vote up
@Inject
public IssueListItemViewModel(BaseFragment fragment) {
    super(fragment);
    issue = new ObservableField<>();
}
 
Example #26
Source File: PullRequestListItemViewModel.java    From Anago with Apache License 2.0 4 votes vote down vote up
@Inject
public PullRequestListItemViewModel(BaseFragment fragment) {
    super(fragment);
    pullRequest = new ObservableField<>();
}
 
Example #27
Source File: ConditionalAlbumListModel.java    From PainlessMusicPlayer with Apache License 2.0 4 votes vote down vote up
@NonNull
public ObservableField<RecyclerView.Adapter> getRecyclerAdapter() {
    return mRecyclerAdpter;
}
 
Example #28
Source File: QueueActivityModel.java    From PainlessMusicPlayer with Apache License 2.0 4 votes vote down vote up
@NonNull
public ObservableField<String> getImageUri() {
    return mImageUri;
}
 
Example #29
Source File: DepartmentViewModel.java    From ExpandableRecyclerview-Databinding with Apache License 2.0 4 votes vote down vote up
public DepartmentViewModel() {
    this.name = new ObservableField<>();
}
 
Example #30
Source File: BaseBindableViewHolder.java    From mv2m with Apache License 2.0 4 votes vote down vote up
public ObservableField<T> getItem() {
    return item;
}