Java Code Examples for android.databinding.ObservableArrayList

The following are top voted examples for showing how to use android.databinding.ObservableArrayList. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 7 votes vote down vote up
@Test
public void skipWhileSkipsWhileTrue() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    String gooderString = "GOODER STRING";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(goodString);
    CinderComputable computing = Cinder.observable(observed).takeWhile(()->observed.get(0).startsWith("GOOD"));
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.get(0);
    }, computing).immediate();

    observed.add(0, goodString);
    assertEquals(goodString, observing.get());

    observed.add(0, badString);
    assertEquals(goodString, observing.get());

    observed.add(0, gooderString);
    assertEquals(goodString, observing.get());
}
 
Example 2
Project: MVVMDemo   File: ObservableModel.java   Source Code and License 7 votes vote down vote up
public static void main(String[] args) {
    ObservableModel user1 = new ObservableModel();
    user1.firstName.set("Google");
    int age = user1.age.get();

    ObservableArrayMap<String, Object> user2 = new ObservableArrayMap<>();
    user2.put("firstName", "Google");
    user2.put("lastName", "Inc.");
    user2.put("age", 17);


    ObservableArrayList<Object> user3 = new ObservableArrayList<>();
    user3.add("Google");
    user3.add("Inc.");
    user3.add(17);

}
 
Example 3
Project: spline   File: LayerListAdapter.java   Source Code and License 6 votes vote down vote up
public ObservableList<Layer> getTwirledDownLayersForGroup(LayerGroup root) {
    ObservableList<Layer> twirledDownLayers = new ObservableArrayList<>();

    for (Layer l : root.getLayers()) {
        twirledDownLayers.add(l);
        if (l instanceof LayerGroup) {
            LayerGroup group = (LayerGroup) l;
            if (group.isTwirledDown()) {
                int i = twirledDownLayers.indexOf(l);
                List<Layer> childLayers = getTwirledDownLayersForGroup(group);
                twirledDownLayers.addAll(i + 1, childLayers);
            }
        }
    }

    return twirledDownLayers;
}
 
Example 4
Project: DereHelper   File: SongVM.java   Source Code and License 6 votes vote down vote up
public SongVM(BaseActivity mContext, Song song) {
    super(mContext);
    song.name = song.name.replace("\\n", "\n");
    songJacketUrl.set(String.format(SStaticR.SERVER_URL_RES + "/jacket/%s.png", song.music_id));
    switch (song.type){
        case 1:
            songIcon.set(R.drawable.ic_song_cute);
            break;
        case 2:
            songIcon.set(R.drawable.ic_song_cool);
            break;
        case 3:
            songIcon.set(R.drawable.ic_song_passion);
            break;
        case 4:
            songIcon.set(R.drawable.ic_song_all);
            break;
    }
    if (mContext instanceof SongDetailActivity){
        song.discription = song.discription.replace("\\n", "\n");
        itemViewModel = new ObservableArrayList<>();
        itemView = ItemView.of(com.lazyeraser.imas.derehelper.BR.viewModel, R.layout.item_list_live);
        loadLiveDetail(String.valueOf(song.music_id));
    }
    this.song.set(song);
}
 
Example 5
Project: Cinder   File: CinderUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void computableListElementsAreComputable() throws Exception {

    String string1 = "STRING 1";
    String string2 = "STRING 2";
    StringClass stringClass = new StringClass(string1);

    ObservableList<StringClass> strings = new ObservableArrayList<>();
    strings.add(0, stringClass);
    ObservableField<String> computedString = Cinder.computeField(()->strings.get(0).element.get(),
            Cinder.observable(strings, StringClass.class, "element")).immediate();
    assertEquals(computedString.get(), string1);

    strings.get(0).element.set(string2);
    assertEquals(computedString.get(), string2);
}
 
Example 6
Project: Cinder   File: CinderUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void observesObservableLists() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    String string3 = "STRING 3";
    StringClass stringClass1 = new StringClass(string1);
    StringClass stringClass2 = new StringClass(string2);
    ObservableArrayList<StringClass> observableList = new ObservableArrayList<>();

    ObservableField<String> observingString = new ObservableField<>("");
    Cinder.observe(
            ()->observingString.set(observableList.get(0).element.get()),
            observableList, StringClass.class, "element");

    observableList.add(0, stringClass1);
    assertEquals(observingString.get(), string1);

    observableList.add(0, stringClass2);
    assertEquals(observingString.get(), string2);

    stringClass2.element.set(string3);
    assertEquals(observingString.get(), string3);

    observableList.remove(0);
    assertEquals(observingString.get(), string1);
}
 
Example 7
Project: Cinder   File: CinderUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void simpleObservesObservableLists() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    StringClass stringClass1 = new StringClass(string1);
    StringClass stringClass2 = new StringClass(string2);
    ObservableArrayList<StringClass> observableList = new ObservableArrayList<>();

    ObservableField<String> observingString = new ObservableField<>("");
    Cinder.observe(
            ()->observingString.set(observableList.get(0).element.get()),
            observableList);

    observableList.add(0, stringClass1);
    assertEquals(observingString.get(), string1);

    observableList.add(0, stringClass2);
    assertEquals(observingString.get(), string2);

    observableList.remove(0);
    assertEquals(observingString.get(), string1);
}
 
Example 8
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void takeTakesCorrectTimes() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    String string3 = "STRING 3";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(string1);
    CinderComputable computing = Cinder.observable(observed).take(2).immediate();
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.get(0);
    }, computing).immediate();
    assertEquals(string1, observing.get());

    observed.add(0, string2);
    assertEquals(string2, observing.get());

    observed.add(0, string3);
    assertEquals(string2, observing.get());
}
 
Example 9
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void skipSkipsCorrectTimes() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(string1);
    CinderComputable computing = Cinder.observable(observed).skip(1);
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.size() > 0 ?  observed.get(0) : "";
    }, computing);

    observed.add(0, string1);
    assertEquals(null, observing.get());

    observed.add(0, string2);
    assertEquals(string2, observing.get());
}
 
Example 10
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void filterFilters() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(goodString);
    CinderComputable computing = Cinder.observable(observed).filter(()->observed.get(0).startsWith("GOOD"));
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.get(0);
    }, computing).immediate();
    assertEquals(goodString, observing.get());

    observed.add(0, badString);
    assertEquals(goodString, observing.get());

}
 
Example 11
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void takeWhileTakesWhileTrue() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    String gooderString = "GOODER STRING";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(goodString);
    CinderComputable computing = Cinder.observable(observed).takeWhile(()->observed.get(0).startsWith("GOOD"));
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.get(0);
    }, computing).immediate();

    observed.add(0, goodString);
    assertEquals(goodString, observing.get());

    observed.add(0, badString);
    assertEquals(goodString, observing.get());

    observed.add(0, gooderString);
    assertEquals(goodString, observing.get());
}
 
Example 12
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void stopArrayListStops() throws Exception {
    int TEST = 43;
    ObservableArrayList<TestClass> observed = new ObservableArrayList<>();
    TestClass testClass = new TestClass();
    testClass.number.set(TEST);
    observed.add(testClass);
    CinderComputable computing = Cinder.observable(observed, TestClass.class, "number").immediate();
    CinderInt observing = Cinder.computeInt(()->
            observed.get(0).number.get(), computing).immediate();
    assertEquals(TEST, observing.get());

    computing.stop();

    observed.get(0).number.set(23);
    assertEquals(TEST, observing.get());

    observed.add(new TestClass());
    assertEquals(TEST, observing.get());
}
 
Example 13
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void takeTakesCorrectTimes() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    String string3 = "STRING 3";
    ObservableField<String> observed = new ObservableField<>(string1);
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
        list.add(0, observed.get());
    }, observed).take(2).immediate();
    assertEquals(string1, observing.get(0));

    observed.set(string2);
    assertEquals(string2, observing.get(0));

    observed.set(string3);
    assertEquals(string2, observing.get(0));
}
 
Example 14
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void takeWhileTakesWhileTrue() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    String gooderString = "GOODER STRING";
    ObservableField<String> observed = new ObservableField<>("");
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
        list.add(0, observed.get());
    }, observed).takeWhile(()->observed.get().startsWith("GOOD"));

    observed.set(goodString);
    assertEquals(goodString, observing.get(0));

    observed.set(badString);
    assertEquals(goodString, observing.get(0));

    observed.set(gooderString);
    assertEquals(goodString, observing.get(0));
}
 
Example 15
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 6 votes vote down vote up
@Test
public void skipWhileSkipsWhileTrue() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    String gooderString = "GOODER STRING";
    ObservableField<String> observed = new ObservableField<>("");
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
        list.add(0, observed.get());
    }, observed).takeWhile(()->observed.get().startsWith("GOOD"));

    observed.set(goodString);
    assertEquals(goodString, observing.get(0));

    observed.set(badString);
    assertEquals(goodString, observing.get(0));

    observed.set(gooderString);
    assertEquals(goodString, observing.get(0));
}
 
Example 16
Project: Cook-E   File: RecipeList.java   Source Code and License 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        mRecipes = new ObservableArrayList<>();
        mRecipes.addAll(App.getAccessor().loadAllRecipes(App.getDisplayLimit()));

    } catch (SQLException e) {
        new AlertDialog.Builder(getActivity())
                .setTitle(R.string.message_failed_to_load)
                .setMessage(e.getLocalizedMessage())
                .show();
        Log.e(TAG, "Failed to load recipes", e);
    }
}
 
Example 17
Project: Cook-E   File: MealList.java   Source Code and License 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMeals = new ObservableArrayList<>();
    mVisibleMeals = new ObservableArrayList<>();
    try {
        mMeals.addAll(App.getAccessor().loadAllBunches(App.getDisplayLimit()));
        mVisibleMeals.addAll(mMeals);
    } catch (SQLException e) {
        new AlertDialog.Builder(getActivity())
                .setTitle("Failed to load Meals")
                .setMessage(e.getLocalizedMessage())
                .show();
        Log.e(TAG, "Failed to load Meals", e);
    }
}
 
Example 18
Project: Cook-E   File: ObservableArrayListTest.java   Source Code and License 6 votes vote down vote up
/**
     * Checks whether the ObservableArrayList notifies its callbacks when an iterator is used
     * to remove items
     *
     * This test is temporarily disabled because of an Android bug.
     * A bug report has been filed: https://code.google.com/p/android/issues/detail?id=201481
     */
//    @Test
    public void testRemoveWithIterator() {
        final ObservableList<String> items = new ObservableArrayList<>();

        final ListChangeMock<String> mock = new ListChangeMock<>();
        items.addOnListChangedCallback(mock);

        // Add items to list
        items.addAll(Arrays.asList("Cake", "Pie", "Galette", "Pudding"));
        // Change listener reflects the added items
        assertEquals(items, mock.getCurrentList());

        // Remove all items from the list using an iterator
        for (Iterator<String> iter = items.iterator(); iter.hasNext(); ) {
            iter.next();
            iter.remove();
        }
        // List is now empty
        assertTrue(items.isEmpty());
        // Change listener should reflect this
        assertEquals(items, mock.getCurrentList());
    }
 
Example 19
Project: devfest-2016-realm   File: PasswordReadViewModel.java   Source Code and License 6 votes vote down vote up
public PasswordReadViewModel(Navigator navigator, PasswordInteractor interactor, int id) {
  this.navigator = navigator;
  this.interactor = interactor;
  this.id = id;

  this.title = new ObservableField<>();
  this.icon = new ObservableField<>();
  this.username = new ObservableField<>();
  this.password = new ObservableField<>();
  this.displayPassword = new ObservableBoolean(false);
  this.customFields = new ObservableArrayList<>();

  realmPassword = interactor.readPassword(id);
  realmPassword.addChangeListener(this::updateView);

  updateView(realmPassword);
}
 
Example 20
Project: ExpandableRecyclerview-Databinding   File: BaseExpandableAdapter.java   Source Code and License 6 votes vote down vote up
/**
 * expand item
 *
 * @param parentIndex                       will be expand item index
 * @param baseExpandableObservable          the item Observable
 * @param expansionTriggeredByListItemClick
 */
public void expandItem(int parentIndex, BaseExpandableObservable baseExpandableObservable, boolean expansionTriggeredByListItemClick) {
    if (parentIndex >= 0 && parentIndex < mDataList.size() && !baseExpandableObservable.isExpand.get()) {

        ObservableArrayList<Object> childList = baseExpandableObservable.getChildList();
        final int childSize = childList.size();
        for (int i = 0; i < childSize; i++) {
            Object o = childList.get(i);
            int newIndex = parentIndex + i + 1;
            mDataList.add(newIndex, o);
        }
        notifyItemRangeInserted(parentIndex + 1, childSize);
        int positionStart = parentIndex + childSize;
        if (parentIndex != mDataList.size() - 1)
            notifyItemRangeChanged(positionStart, mDataList.size() - positionStart);
        baseExpandableObservable.setIsExpand(true);
        if (!mRecyclerViews.isEmpty()) {
            BindingViewHolder viewHolderForAdapterPosition = (BindingViewHolder) mRecyclerViews.get(0).findViewHolderForAdapterPosition(parentIndex);
            baseExpandableObservable.onExpansionToggled(viewHolderForAdapterPosition, parentIndex, true);
        }
        if (expansionTriggeredByListItemClick && mExpandCollapseListener != null) {
            mExpandCollapseListener.onListItemExpanded(parentIndex);
        }
    }
}
 
Example 21
Project: kickmaterial   File: DataManager.java   Source Code and License 6 votes vote down vote up
public static ObservableArrayList<Category> getCategoriesList() {
    ObservableArrayList<Category> categories = new ObservableArrayList<>();

    categories.add(getCategoryAll());
    categories.add(new Category(1, R.string.art, R.color.art, R.drawable.art));
    categories.add(new Category(3, R.string.comics, R.color.comics, R.drawable.comics));
    categories.add(new Category(26, R.string.crafts, R.color.crafts, R.drawable.crafts));
    categories.add(new Category(6, R.string.dance, R.color.dance, R.drawable.dance));
    categories.add(new Category(7, R.string.design, R.color.design, R.drawable.design));
    categories.add(new Category(9, R.string.fashion, R.color.fashion, R.drawable.fashion));
    categories.add(new Category(11, R.string.film_video, R.color.film_video, R.drawable.film_video));
    categories.add(new Category(10, R.string.food, R.color.food, R.drawable.food));
    categories.add(new Category(12, R.string.games, R.color.games, R.drawable.games));
    categories.add(new Category(13, R.string.journalism, R.color.journalism, R.drawable.journalism));
    categories.add(new Category(14, R.string.music, R.color.music, R.drawable.music));
    categories.add(new Category(15, R.string.photography, R.color.photography, R.drawable.photography));
    categories.add(new Category(18, R.string.publishing, R.color.publishing, R.drawable.publishing));
    categories.add(new Category(16, R.string.technology, R.color.technology, R.drawable.technology));
    categories.add(new Category(17, R.string.theater, R.color.theater, R.drawable.theater));

    return categories;
}
 
Example 22
Project: onyxbeacon-android-sdk   File: DataManager.java   Source Code and License 6 votes vote down vote up
public static ObservableArrayList<Category> getCategoriesList() {
    ObservableArrayList<Category> categories = new ObservableArrayList<>();

    categories.add(getCategoryAll());
    categories.add(new Category(1, R.string.art, R.color.art, R.drawable.art));
    categories.add(new Category(3, R.string.comics, R.color.comics, R.drawable.comics));
    categories.add(new Category(26, R.string.crafts, R.color.crafts, R.drawable.crafts));
    categories.add(new Category(6, R.string.dance, R.color.dance, R.drawable.dance));
    categories.add(new Category(7, R.string.design, R.color.design, R.drawable.design));
    categories.add(new Category(9, R.string.fashion, R.color.fashion, R.drawable.fashion));
    categories.add(new Category(11, R.string.film_video, R.color.film_video, R.drawable.film_video));
    categories.add(new Category(10, R.string.food, R.color.food, R.drawable.food));
    categories.add(new Category(12, R.string.games, R.color.games, R.drawable.games));
    categories.add(new Category(13, R.string.journalism, R.color.journalism, R.drawable.journalism));
    categories.add(new Category(14, R.string.music, R.color.music, R.drawable.music));
    categories.add(new Category(15, R.string.photography, R.color.photography, R.drawable.photography));
    categories.add(new Category(18, R.string.publishing, R.color.publishing, R.drawable.publishing));
    categories.add(new Category(16, R.string.technology, R.color.technology, R.drawable.technology));
    categories.add(new Category(17, R.string.theater, R.color.theater, R.drawable.theater));

    return categories;
}
 
Example 23
Project: binding-collection-adapter   File: DynamicItemViewTest.java   Source Code and License 6 votes vote down vote up
@Test
@UiThreadTest
public void adapterDoesntChangeForSameItemBindingInListView() {
    ObservableList<String> items = new ObservableArrayList<>();
    items.addAll(Arrays.asList("one", "two", "three"));
    TestHelpers.ViewModel viewModel = new TestHelpers.ViewModel.Builder(items, ItemBinding.<String>of(BR.item, item)).build();

    ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.list_view, null, false);
    binding.setVariable(BR.viewModel, viewModel);
    binding.executePendingBindings();

    ListView listView1 = (ListView) binding.getRoot();
    BindingCollectionAdapter<String> adapter1 = (BindingCollectionAdapter<String>) listView1.getAdapter();

    items.add("four");

    ListView listView2 = (ListView) binding.getRoot();
    @SuppressWarnings("unchecked")
    BindingCollectionAdapter<String> adapter2 = (BindingCollectionAdapter<String>) listView2.getAdapter();

    assertThat(adapter1).isSameAs(adapter2);
}
 
Example 24
Project: binding-collection-adapter   File: MergeObservableTest.java   Source Code and License 6 votes vote down vote up
@Test
public void insertingItemAndListContainsItemThenList() {
    MergeObservableList<String> list = new MergeObservableList<>();
    ObservableList.OnListChangedCallback callback = mock(ObservableList.OnListChangedCallback.class);
    list.addOnListChangedCallback(callback);
    list.insertItem("test1");
    ObservableList<String> items = new ObservableArrayList<>();
    items.add("test2");
    items.add("test3");
    list.insertList(items);

    assertThat(list)
            .hasSize(3)
            .containsExactly("test1", "test2", "test3");
    verify(callback).onItemRangeInserted(list, 0, 1);
    verify(callback).onItemRangeInserted(list, 1, 2);
}
 
Example 25
Project: binding-collection-adapter   File: MergeObservableTest.java   Source Code and License 6 votes vote down vote up
@Test
public void addingItemToBackingListAddsItemToList() {
    MergeObservableList<String> list = new MergeObservableList<>();
    ObservableList.OnListChangedCallback callback = mock(ObservableList.OnListChangedCallback.class);
    list.addOnListChangedCallback(callback);
    list.insertItem("test1");
    ObservableList<String> items = new ObservableArrayList<>();
    items.add("test2");
    list.insertList(items);
    list.insertItem("test4");
    items.add("test3");

    assertThat(list)
            .hasSize(4)
            .containsExactly("test1", "test2", "test3", "test4");
    verify(callback).onItemRangeInserted(list, 0, 1);
    verify(callback).onItemRangeInserted(list, 1, 1);
    verify(callback, times(2)).onItemRangeInserted(list, 2, 1);
}
 
Example 26
Project: binding-collection-adapter   File: MergeObservableTest.java   Source Code and License 6 votes vote down vote up
@Test
public void removingItemFromBackingListRemovesItemFromList() {
    MergeObservableList<String> list = new MergeObservableList<>();
    ObservableList.OnListChangedCallback callback = mock(ObservableList.OnListChangedCallback.class);
    list.addOnListChangedCallback(callback);
    list.insertItem("test1");
    ObservableList<String> items = new ObservableArrayList<>();
    items.add("test2");
    list.insertList(items);
    list.insertItem("test3");
    items.clear();

    assertThat(list)
            .hasSize(2)
            .containsExactly("test1", "test3");
    verify(callback).onItemRangeInserted(list, 0, 1);
    verify(callback).onItemRangeInserted(list, 1, 1);
    verify(callback).onItemRangeInserted(list, 2, 1);
    verify(callback).onItemRangeRemoved(list, 1, 1);
}
 
Example 27
Project: binding-collection-adapter   File: MergeObservableTest.java   Source Code and License 6 votes vote down vote up
@Test
public void changingItemFromBackingListChangesItInList() {
    MergeObservableList<String> list = new MergeObservableList<>();
    ObservableList.OnListChangedCallback callback = mock(ObservableList.OnListChangedCallback.class);
    list.addOnListChangedCallback(callback);
    list.insertItem("test1");
    ObservableList<String> items = new ObservableArrayList<>();
    items.add("test2");
    list.insertList(items);
    items.set(0, "test3");
    
    assertThat(list)
            .hasSize(2)
            .containsExactly("test1", "test3");
    verify(callback).onItemRangeInserted(list, 0, 1);
    verify(callback).onItemRangeInserted(list, 1, 1);
    verify(callback).onItemRangeChanged(list, 1, 1);
}
 
Example 28
Project: binding-collection-adapter   File: DynamicItemViewTest.java   Source Code and License 6 votes vote down vote up
@Test
@UiThreadTest
public void adapterDoesntChangeForSameItemBindingInRecylerView() {
    ObservableList<String> items = new ObservableArrayList<>();
    items.addAll(Arrays.asList("one", "two", "three"));
    TestHelpers.ViewModel viewModel = new TestHelpers.ViewModel(items, ItemBinding.<String>of(BR.item, item));

    ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.recycler_view, null, false);
    binding.setVariable(BR.viewModel, viewModel);
    binding.executePendingBindings();

    RecyclerView recyclerView1 = (RecyclerView) binding.getRoot();
    BindingCollectionAdapter<String> adapter1 = (BindingCollectionAdapter<String>) recyclerView1.getAdapter();

    items.add("four");

    RecyclerView recyclerView2 = (RecyclerView) binding.getRoot();
    @SuppressWarnings("unchecked")
    BindingCollectionAdapter<String> adapter2 = (BindingCollectionAdapter<String>) recyclerView2.getAdapter();

    assertThat(adapter1).isSameAs(adapter2);
}
 
Example 29
Project: ForgetAdapters   File: MainActivityViewModel.java   Source Code and License 5 votes vote down vote up
public MainActivityViewModel(IMainActivityAccess access) {

        this.access = access;

        people = new ObservableArrayList<>();
        people.add(new PersonModel("Wojciech", "Sura", "PGS Software S.A."));
        people.add(new PersonModel("Bill", "Gates", "Microsoft"));
        people.add(new PersonModel("Steve", "Jobs", "Apple"));
    }
 
Example 30
Project: ForgetAdapters   File: BindingAdapters.java   Source Code and License 5 votes vote down vote up
@BindingAdapter(value = {"items", "viewProvider"}, requireAll = true)
public static <TModel, TView extends View & IDataViewModelProvider<TModel>> void setItems(RecyclerView recyclerView, ObservableArrayList<TModel> items, RecyclerViewProvider<TModel, TView> viewProvider) {

    if (recyclerView.getAdapter() != null &&
            recyclerView.getAdapter() instanceof RecyclerViewListAdapter &&
            ((RecyclerViewListAdapter)recyclerView.getAdapter()).getList() == items)
        return;

    RecyclerViewListAdapter<TModel, TView> adapter = new RecyclerViewListAdapter<>(items, viewProvider);
    recyclerView.setAdapter(adapter);
}
 
Example 31
Project: ForgetAdapters   File: BindingAdapters.java   Source Code and License 5 votes vote down vote up
@BindingAdapter(value = {"items", "viewProvider"}, requireAll = true)
public static <TModel extends IIdProvider, TView extends View & IDataViewModelProvider<TModel>> void setItems(ListView listView, ObservableArrayList<TModel> items, ListViewProvider<TModel, TView> viewProvider) {

    if (listView.getAdapter() != null &&
            listView.getAdapter() instanceof ListViewCollectionAdapter &&
            ((ListViewCollectionAdapter)listView.getAdapter()).getItems() == items)
        return;

    ListViewListAdapter<TModel, TView> adapter = new ListViewListAdapter<TModel, TView>(items, viewProvider, false);
    listView.setAdapter(adapter);
}
 
Example 32
Project: ForgetAdapters   File: RecyclerViewListAdapter.java   Source Code and License 5 votes vote down vote up
public RecyclerViewListAdapter(ObservableArrayList<TModel> observableList, RecyclerViewProvider<TModel, TView> viewProvider) {

        this.observableList = observableList;
        this.viewProvider = viewProvider;

        observableList.addOnListChangedCallback(listChangeListener);
        setHasStableIds(viewProvider.hasStableIds());
    }
 
Example 33
Project: ForgetAdapters   File: ListViewListAdapter.java   Source Code and License 5 votes vote down vote up
public ListViewListAdapter(ObservableArrayList<TModel> list, ListViewProvider<TModel, TView> provider, boolean stableIds) {
    this.list = list;
    this.provider = provider;
    this.stableIds = stableIds;

    this.list.addOnListChangedCallback(listChangedCallback);
}
 
Example 34
Project: mvvm-clean   File: HouseListViewModel.java   Source Code and License 5 votes vote down vote up
public void requestData() {
    getHouseListUseCase.execute(new DisposableObserver<List<HouseEntity>>() {

        @Override
        protected void onStart() {
            Toast.makeText(activity, "加载开始", Toast.LENGTH_SHORT);
        }

        @Override
        public void onNext(List<HouseEntity> value) {
            ObservableArrayList<HouseItemViewModel> houseItemViewModels = new ObservableArrayList<>();
            for (HouseEntity houseEntity : value) {
                HouseItemViewModel itemViewModel = new HouseItemViewModel(activity, houseEntity);
                houseItemViewModels.add(itemViewModel);
            }
            setHouses(houseItemViewModels);
        }

        @Override
        public void onError(Throwable e) {
            Toast.makeText(activity, "加载失败", Toast.LENGTH_SHORT);
        }

        @Override
        public void onComplete() {
            Toast.makeText(activity, "加载开始", Toast.LENGTH_SHORT);
        }
    }, GetHouseList.Params.setHouse(1, 1, 1, ""));
}
 
Example 35
Project: Mvvm   File: RecyclerViewModel.java   Source Code and License 5 votes vote down vote up
public RecyclerViewModel(int itemVariableId, @LayoutRes int itemLayoutResId, MvvmAdapter<T> adapter, OnItemClickListener<T> listener, int listenerVariableId) {
    this.items = new ObservableArrayList<>();
    this.adapter = null == adapter ? new MvvmAdapter<T>() : adapter;
    this.listener = listener;
    this.bindingItem = BindingItem.<T>of(itemVariableId, itemLayoutResId);
    if (null != listener) {
        this.bindingItem.bindExtra(listenerVariableId, listener);
    }
    this.itemDecoration = new ObservableField<>();
    this.layoutManager = new ObservableField<>();
}
 
Example 36
Project: RxDataBinding   File: ObservableArrayListItemRangeChangeEventObservable.java   Source Code and License 5 votes vote down vote up
Listener(final ObservableArrayList<T> observableArrayList,
    final Observer<? super ObservableArrayListItemRangeChangeEvent<T>> observer) {
  this.observableArrayList = observableArrayList;
  this.onListChangedCallback =
      new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() {
        @Override public void onChanged(ObservableArrayList<T> observableArrayList) {
        }

        @Override
        public void onItemRangeChanged(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
          observer.onNext(
              ObservableArrayListItemRangeChangeEvent.create(observableArrayList, positionStart,
                  itemCount));
        }

        @Override
        public void onItemRangeInserted(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }

        @Override
        public void onItemRangeMoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int positionEnd, int itemCount) {
        }

        @Override
        public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }
      };
}
 
Example 37
Project: RxDataBinding   File: RxObservableArrayList.java   Source Code and License 5 votes vote down vote up
/**
 * Create an observable of item range insertion events on {@code observableArrayList}.
 * <p>
 * <em>Warning:</em> The created observable keeps a strong reference to {@code
 * observableArrayList}.
 * Unsubscribe to free this reference.
 */
@CheckResult @NonNull
public static <T> Observable<ObservableArrayListItemRangeInsertionEvent<T>>
itemRangeInsertionEvents(
    @NonNull ObservableArrayList<T> observableArrayList) {
  checkNotNull(observableArrayList, "observableArrayList == null");
  return new ObservableArrayListItemRangeInsertionEventObservable<>(observableArrayList);
}
 
Example 38
Project: RxDataBinding   File: ObservableArrayListItemRangeInsertionEventObservable.java   Source Code and License 5 votes vote down vote up
Listener(final ObservableArrayList<T> observableArrayList,
    final Observer<? super ObservableArrayListItemRangeInsertionEvent<T>> observer) {
  this.observableArrayList = observableArrayList;
  this.onListChangedCallback =
      new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() {
        @Override public void onChanged(ObservableArrayList<T> observableArrayList) {
        }

        @Override
        public void onItemRangeChanged(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }

        @Override
        public void onItemRangeInserted(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
          observer.onNext(ObservableArrayListItemRangeInsertionEvent.create(observableArrayList,
              positionStart, itemCount));
        }

        @Override
        public void onItemRangeMoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int positionEnd, int itemCount) {
        }

        @Override
        public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }
      };
}
 
Example 39
Project: RxDataBinding   File: ObservableArrayListItemRangeRemovalEventObservable.java   Source Code and License 5 votes vote down vote up
Listener(final ObservableArrayList<T> observableArrayList,
    final Observer<? super ObservableArrayListItemRangeRemovalEvent<T>> observer) {
  this.observableArrayList = observableArrayList;
  this.onListChangedCallback =
      new ObservableList.OnListChangedCallback<ObservableArrayList<T>>() {
        @Override public void onChanged(ObservableArrayList<T> observableArrayList) {
        }

        @Override
        public void onItemRangeChanged(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }

        @Override
        public void onItemRangeInserted(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
        }

        @Override
        public void onItemRangeMoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int positionEnd, int itemCount) {
        }

        @Override
        public void onItemRangeRemoved(ObservableArrayList<T> observableArrayList,
            int positionStart, int itemCount) {
          observer.onNext(ObservableArrayListItemRangeRemovalEvent.create(observableArrayList,
              positionStart, itemCount));
        }
      };
}
 
Example 40
Project: exchange-rates-mvvm   File: CurrencyListViewModel.java   Source Code and License 5 votes vote down vote up
public CurrencyListViewModel(Navigator navigator, GetCurrenciesUseCase currenciesUseCase, GetCurrenciesRatesDate getCurrenciesRatesDate) {
    mCurrencies = new ObservableArrayList<>();
    mCurrenciesUseCase = checkNotNull(currenciesUseCase);
    mGetCurrenciesRatesDate = checkNotNull(getCurrenciesRatesDate);
    mNavigator = checkNotNull(navigator);
    setReadDate(new Date());
}
 
Example 41
Project: Blockbuster   File: MovieGalleryAdapter.java   Source Code and License 5 votes vote down vote up
public MovieGalleryAdapter(Context context, TwoWayView recyclerView) {
    this.context = context;
    this.items = new ObservableArrayList<MovieItem>();
    this.items.addOnListChangedCallback(new OnItemsChangedCallback(this));
    this.recyclerView = recyclerView;
    Fresco.initialize(context);
}
 
Example 42
Project: NewsMe   File: MeiziFragment.java   Source Code and License 5 votes vote down vote up
private void initData() {
    sex = SexUtil.getSex(getActivity());
    data = new ObservableArrayList<>();
    if (sex == 0) {
        data.addAll(boys);
    } else if (sex == 1) {
        data.addAll(girls);
    }
}
 
Example 43
Project: android-ui-toolkit-demos   File: MainActivity.java   Source Code and License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    ObservableArrayList users = new ObservableArrayList();
    users.add(new User("George", "Mount"));
    binding.setUsers(users);
    binding.setHandler(new ButtonHandler(users));
}
 
Example 44
Project: SimpleFTP   File: ExplorerViewModel.java   Source Code and License 5 votes vote down vote up
/**
 * Default constructor.
 * @param context The context of the current activity.
 */
public ExplorerViewModel(Context context) {
    this.context = context;
    this.isLoading = new ObservableBoolean(false);
    this.isRefreshing = new ObservableBoolean(false);
    this.files = new ObservableArrayList<FileViewModel>();
    this.isSelectionMode = new ObservableBoolean(false);
    this.numberSelectedItems = new ObservableInt(0);
    this.changeDirectory("/");
}
 
Example 45
Project: SimpleFTP   File: ListServerViewModel.java   Source Code and 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 46
Project: SimpleFTP   File: BindingRecyclerViewAdapter.java   Source Code and License 5 votes vote down vote up
/**
 * Sets the list of items to display.
 * @param items The list of items.
 */
public void setItems(@Nullable Collection<T> items)
{
    if (this.items == items)
    {
        return;
    }

    if (this.items != null)
    {
        this.items.removeOnListChangedCallback(onListChangedCallback);
        notifyItemRangeRemoved(0, this.items.size());
    }

    if (items instanceof ObservableList)
    {
        this.items = (ObservableList<T>) items;
        notifyItemRangeInserted(0, this.items.size());
        this.items.addOnListChangedCallback(onListChangedCallback);
    }
    else if (items != null)
    {
        this.items = new ObservableArrayList<>();
        this.items.addOnListChangedCallback(onListChangedCallback);
        this.items.addAll(items);
    }
    else
    {
        this.items = null;
    }
}
 
Example 47
Project: AndroidDataBindingTutorial   File: MockRandomData.java   Source Code and License 5 votes vote down vote up
public static List<ObservableVehicleInfo> getObservableVehicleInfos(){
    ObservableArrayList<ObservableVehicleInfo> vehicleInfos = new ObservableArrayList<>();
    for (VehicleInfo vehicleInfo : sVehicleInfos) {
        vehicleInfos.add(new ObservableVehicleInfo(vehicleInfo));
    }
    return vehicleInfos;
}
 
Example 48
Project: chaoli-forum-for-android-2   File: BusinessQuestion.java   Source Code and License 5 votes vote down vote up
public BusinessQuestion(Question item) {
    for (int i = 0; i < 4; i++) isChecked.add(false);
    id = item._id.$id;
    question = item.question;
    choice = Boolean.valueOf(item.choice);
    multiAnswer = Boolean.valueOf(item.multi_answer);
    options = new ObservableArrayList<>();
    options.addAll(item.options);
    while (options.size() < 4) {
        options.add(ChaoliApplication.getAppContext().getString(R.string.useless_option));
    }
}
 
Example 49
Project: Architecture-Demo   File: PostListComponentViewModel.java   Source Code and License 5 votes vote down vote up
@Inject
public PostListComponentViewModel(Bus bus, IRepository repository, INavigator navigator, ILocalization localization) {
    super(bus);

    this.repository = repository;
    this.navigator = navigator;
    this.localization = localization;

    this.posts = new ObservableArrayList<>();

    this.isBusy = new ObservableBoolean();
    this.loadCommand = new Command(this::loadPosts);
}
 
Example 50
Project: Cinder   File: CinderUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void computesArrayList() throws Exception {
    final int NUMBER_1 = 23;
    final int NUMBER_2 = 56;
    final ObservableInt number = new ObservableInt(NUMBER_1);
    final ObservableArrayList<Integer> ints =
            Cinder.<Integer>computeArrayList((list)->list.add(number.get()), number).immediate();
    assertEquals(ints.get(0).longValue(), NUMBER_1);

    number.set(NUMBER_2);
    assertEquals(ints.get(1).longValue(), NUMBER_2);
}
 
Example 51
Project: Cinder   File: CinderUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void computableListsAreComputable() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    ObservableList<String> strings = new ObservableArrayList<>();
    strings.add(0, string1);
    ObservableField<String> computedString = Cinder.computeField(()->strings.get(0),
            Cinder.observable(strings)).immediate();
    assertEquals(computedString.get(), string1);

    strings.add(0, string2);
    assertEquals(computedString.get(), string2);
}
 
Example 52
Project: Cinder   File: CinderComputableUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void onceTakesOnce() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    ObservableArrayList<String> observed = new ObservableArrayList<>();
    observed.add(string1);
    CinderComputable computing = Cinder.observable(observed).once().immediate();
    ObservableField<String> observing = Cinder.<String>computeField(()-> {
        return observed.get(0);
    }, computing).immediate();
    assertEquals(string1, observing.get());

    observed.add(0, string2);
    assertEquals(string1, observing.get());
}
 
Example 53
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void onceTakesOnce() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    ObservableField<String> observed = new ObservableField<>(string1);
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->
        list.add(0, observed.get()), observed).once().immediate();
    assertEquals(string1, observing.get(0));

    observed.set(string2);
    assertEquals(string1, observing.get(0));
}
 
Example 54
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void skipSkipsCorrectTimes() throws Exception {
    String string1 = "STRING 1";
    String string2 = "STRING 2";
    ObservableField<String> observed = new ObservableField<>();
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
        list.add(0, observed.get());
    }, observed).skip(1);

    observed.set(string1);
    assertEquals(0, observing.size());

    observed.set(string2);
    assertEquals(string2, observing.get(0));
}
 
Example 55
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void filterFilters() throws Exception {
    String badString = "BAD STRING";
    String goodString = "GOOD STRING";
    ObservableField<String> observed = new ObservableField<>("");
    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
        list.add(0, observed.get());
    }, observed).filter(()->observed.get().startsWith("GOOD"));

    observed.set(badString);
    assertEquals(0, observing.size());

    observed.set(goodString);
    assertEquals(goodString, observing.get(0));
}
 
Example 56
Project: Cinder   File: CinderArrayListUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void withDefaultAssignsDefault() throws Exception {
    String testString = "TEST";
    ArrayList<String> defaultValue = new ArrayList<>();
    defaultValue.add(testString);

    ObservableArrayList<String> observing = Cinder.<String>computeArrayList((list)->{
    }).withDefault(defaultValue);

    assertEquals(testString, observing.get(0));
}
 
Example 57
Project: Cinder   File: RxCinder.java   Source Code and License 5 votes vote down vote up
public static <T> ObservableArrayList<T> convertArrayList(Observable<List<T>> observable){
    ObservableArrayList<T> observableArrayList = new ObservableArrayList<>();
    observable.subscribe((a)->{
        observableArrayList.clear();
        observableArrayList.addAll(a);
    });
    return observableArrayList;
}
 
Example 58
Project: Cinder   File: RxCinderUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void convertsObservableArrayListToRx() throws Exception {
    ObservableArrayList<String> observableStrings = new ObservableArrayList<>();
    Observable<ArrayList<String>> rxStrings = RxCinder.convert(observableStrings);

    final ArrayList<String> testStrings = new ArrayList<>(Arrays.asList("Corndog"));
    rxStrings.subscribe((stringArray)->{
        Collections.copy(testStrings, stringArray);});

    final String TEST_STRING = "Banana";
    observableStrings.add(TEST_STRING);
    assertTrue(testStrings.get(0).equals(TEST_STRING));
}
 
Example 59
Project: Cinder   File: RxCinderUnitTest.java   Source Code and License 5 votes vote down vote up
@Test
public void convertsRxListToObservable() throws Exception {
    final String TEST_STRING = "Banana";
    Observable<List<String>> rxStrings = Observable.just(new ArrayList<>(Arrays.asList(TEST_STRING)));
    ObservableArrayList<String> observableStrings = RxCinder.convertArrayList(rxStrings);
    assertTrue(observableStrings.get(0).equals(TEST_STRING));
}
 
Example 60
Project: Cook-E   File: MealRecipeAddActivity.java   Source Code and License 5 votes vote down vote up
/**
 * Unpacks the recipes extra from the intent that started this activity and returns an
 * ObservableArrayList containing the same recipes.
 *
 * @return a list of recipes
 */
private ObservableArrayList<Recipe> unpackRecipes() {
    final Parcelable[] parcelables = getIntent().getParcelableArrayExtra(EXTRA_RECIPES);
    Objects.requireNonNull(parcelables,
            "MealRecipeAddActivity must be started with a recipes extra");
    final ObservableArrayList<Recipe> recipes = new ObservableArrayList<>();
    recipes.ensureCapacity(parcelables.length);
    for (Parcelable parcelable : parcelables) {
        recipes.add((Recipe) parcelable);
    }
    return recipes;
}