23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | buildscript {
26 | repositories {
27 | jcenter()
28 | }
29 | dependencies {
30 | classpath 'com.android.tools.build:gradle:1.1.1'
31 | }
32 | }
33 | apply plugin: 'com.android.application'
34 |
35 | repositories {
36 | jcenter()
37 | }
38 |
39 | android {
40 | compileSdkVersion 22
41 | buildToolsVersion "21.1.2"
42 |
43 | defaultConfig {
44 | applicationId "com.exallium.rxrecyclerview.app"
45 | minSdkVersion 19
46 | targetSdkVersion 22
47 | versionCode 1
48 | versionName "1.0"
49 |
50 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
51 | }
52 |
53 | packagingOptions {
54 | exclude 'LICENSE.txt'
55 | }
56 |
57 | buildTypes {
58 | release {
59 | minifyEnabled false
60 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
61 | }
62 | }
63 | }
64 |
65 | dependencies {
66 | compile project(":lib")
67 | compile 'com.jakewharton:butterknife:6.1.0'
68 | compile 'com.android.support:appcompat-v7:22.2.0'
69 |
70 | androidTestCompile 'com.android.support.test:runner:0.3'
71 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
72 | androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2') {
73 | exclude group: 'com.android.support', module: 'appcompat'
74 | exclude group: 'com.android.support', module: 'support-v4'
75 | exclude module: 'recyclerview-v7'
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ahart/src/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/exallium/rxrecyclerview/app/MainEspressoTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app;
26 |
27 | import android.support.test.espresso.assertion.ViewAssertions;
28 | import android.support.test.espresso.contrib.RecyclerViewActions;
29 | import android.support.test.rule.ActivityTestRule;
30 | import android.support.test.runner.AndroidJUnit4;
31 | import android.test.suitebuilder.annotation.LargeTest;
32 | import org.junit.Rule;
33 | import org.junit.Test;
34 | import org.junit.runner.RunWith;
35 |
36 | import static android.support.test.espresso.Espresso.onView;
37 | import static android.support.test.espresso.action.ViewActions.click;
38 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
39 | import static android.support.test.espresso.matcher.ViewMatchers.withText;
40 | import static org.hamcrest.Matchers.notNullValue;
41 |
42 | /**
43 | * MainActivity Test.
44 | *
45 | * MainActivity contains an RxRecyclerView with Headers and Empty enabled.
46 | */
47 | @RunWith(AndroidJUnit4.class)
48 | @LargeTest
49 | public class MainEspressoTest {
50 |
51 | @Rule
52 | public ActivityTestRule mainActivityRule = new ActivityTestRule<>(MainActivity.class);
53 |
54 | private void addClick() {
55 | onView(withId(R.id.addButton)).perform(click());
56 | }
57 |
58 | private void itemClick(int position) {
59 | onView(withId(R.id.recyclerView))
60 | .perform(RecyclerViewActions.actionOnItemAtPosition(position, click()));
61 | }
62 |
63 | private void itemCount(int size) {
64 | onView(withId(R.id.recyclerView)).check(RecyclerViewAssertions.count(size));
65 | }
66 |
67 | @Test
68 | public void emptyItemTest() {
69 | itemCount(1);
70 | }
71 |
72 | @Test
73 | public void addItemsTest() {
74 | for (int i = 0; i < 10; i++)
75 | addClick();
76 |
77 | // 1. and 10. will be in the same group.
78 | itemCount(19);
79 | }
80 |
81 | @Test
82 | public void removeItemsTest() {
83 | addClick();
84 | itemCount(2);
85 | itemClick(1);
86 | itemCount(1);
87 | }
88 |
89 | @Test
90 | public void changeItemTest() {
91 | addClick();
92 | itemCount(2);
93 | onView(withText("1 - Item")).check(ViewAssertions.matches(notNullValue()));
94 | onView(withId(R.id.refreshButton)).perform(click());
95 | itemCount(2);
96 | onView(withText("1- Item")).check(ViewAssertions.doesNotExist());
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/exallium/rxrecyclerview/app/RecyclerViewAssertions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app;
26 |
27 | import android.support.test.espresso.NoMatchingViewException;
28 | import android.support.test.espresso.ViewAssertion;
29 | import android.support.test.espresso.matcher.ViewMatchers;
30 | import android.support.v7.widget.RecyclerView;
31 | import android.util.Log;
32 | import android.view.View;
33 | import org.hamcrest.Matchers;
34 | import org.hamcrest.StringDescription;
35 |
36 | import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
37 |
38 | public class RecyclerViewAssertions {
39 |
40 | private static final String TAG = RecyclerViewAssertions.class.getSimpleName();
41 |
42 | public static ViewAssertion count(int n) {
43 | return new RecyclerViewCount(n);
44 | }
45 |
46 | static class RecyclerViewCount implements ViewAssertion {
47 |
48 | private final int n;
49 |
50 | private RecyclerViewCount(int n) {
51 | this.n = n;
52 | }
53 |
54 | @Override
55 | public void check(View view, NoMatchingViewException noViewException) {
56 | StringDescription description = new StringDescription();
57 | description.appendText("\'RecyclerView Count");
58 | if(noViewException != null) {
59 | description.appendText(String.format("\' check could not be performed because view \'%s\' was not found.\n", new Object[]{noViewException.getViewMatcherDescription()}));
60 | Log.e(RecyclerViewAssertions.TAG, description.toString());
61 | throw noViewException;
62 | } else if (!isAssignableFrom(RecyclerView.class).matches(view)) {
63 | description.appendText(String.format("\' check could not be performed because view \'%s\' is not a RecyclerView", view));
64 | Log.e(RecyclerViewAssertions.TAG, description.toString());
65 | } else if (((RecyclerView) view).getAdapter() == null) {
66 | description.appendText(String.format("\' check could not be performed because view \'%s\' has no adapter", view));
67 | Log.e(RecyclerViewAssertions.TAG, description.toString());
68 | } else {
69 | description.appendText(String.format("\' doesn\'t match the selected count.", new Object[0]));
70 | ViewMatchers.assertThat(description.toString(), n, Matchers.equalTo(((RecyclerView) view).getAdapter().getItemCount()));
71 | }
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
24 |
25 |
27 |
28 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/exallium/rxrecyclerview/app/Adapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app;
26 |
27 | import android.support.v7.widget.RecyclerView;
28 | import android.util.Log;
29 | import android.view.View;
30 | import android.view.ViewGroup;
31 | import android.widget.TextView;
32 | import com.exallium.rxrecyclerview.app.model.ObjectModel;
33 | import com.exallium.rxrecyclerview.lib.element.EventElement;
34 | import com.exallium.rxrecyclerview.lib.event.Event;
35 | import com.exallium.rxrecyclerview.lib.RxRecyclerViewAdapter;
36 | import rx.Observable;
37 |
38 | public class Adapter extends RxRecyclerViewAdapter {
39 | private static final String TAG = Adapter.class.getSimpleName();
40 |
41 | public Adapter(Observable> observable) {
42 | super(observable);
43 | }
44 |
45 | @Override
46 | public void onBindViewHolder(ViewHolder holder, EventElement element) {
47 | final String dataString;
48 | switch (element.getViewType() >> EventElement.MASK_SHIFT) {
49 | case EventElement.EMPTY_MASK:
50 | dataString = "EMPTY";
51 | break;
52 | case EventElement.HEADER_MASK:
53 | dataString = "HEADER";
54 | break;
55 | case EventElement.FOOTER_MASK:
56 | dataString = "FOOTER";
57 | break;
58 | default:
59 | dataString = element.getData().getValue();
60 | break;
61 | }
62 | holder.onBind(element.getData().getKey(), dataString, element.getViewType() >> EventElement.MASK_SHIFT);
63 | }
64 |
65 | @Override
66 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
67 | return new ViewHolder(new TextView(parent.getContext()));
68 | }
69 |
70 | @Override
71 | protected void postProcessElement(EventElement element) {
72 | Log.d(TAG, String.format("POST PROCESS ELEMENT %d - %s",
73 | element.getData().getKey(), element.getData().getValue()));
74 | }
75 |
76 | @Override
77 | protected void preProcessElement(EventElement element) {
78 | Log.d(TAG, String.format("PRE PROCESS ELEMENT %d - %s",
79 | element.getData().getKey(), element.getData().getValue()));
80 | final int indexOf = getIndexOf(element);
81 | if (indexOf != -1) {
82 | switch (element.getData().getType()) {
83 | case ADD:
84 | final EventElement oldElement = getItemAt(indexOf);
85 | Log.d(TAG, String.format("REPLACE ELEMENT %d - %s",
86 | oldElement.getData().getKey(), oldElement.getData().getValue()));
87 | break;
88 | case REMOVE:
89 | Log.d(TAG, String.format("REMOVE ELEMENT %d - %s",
90 | element.getData().getKey(), element.getData().getValue()));
91 | break;
92 | default:
93 | }
94 | }
95 | }
96 |
97 | public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
98 |
99 | private Long key;
100 | private String value;
101 |
102 | public ViewHolder(View itemView) {
103 | super(itemView);
104 | }
105 |
106 | public void onBind(Long key, String value, int viewMask) {
107 | ((TextView)itemView).setText(String.format("%d - %s", key, value));
108 | this.key = key;
109 | this.value = value;
110 | itemView.setOnClickListener(viewMask == EventElement.DATA_MASK ? this : null);
111 | }
112 |
113 | @Override
114 | public void onClick(View v) {
115 | ObjectModel.getInstance().getEventObserver().onNext(new Event<>(Event.TYPE.REMOVE, key, value));
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/com/exallium/rxrecyclerview/app/AnotherActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app;
26 |
27 | import android.app.Activity;
28 | import android.os.Bundle;
29 | import com.exallium.rxrecyclerview.app.model.ObjectModel;
30 | import com.exallium.rxrecyclerview.lib.event.Event;
31 |
32 | public class AnotherActivity extends Activity {
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(R.layout.activity_another);
37 | ObjectModel.getInstance().getEventObserver().onNext(new Event<>(Event.TYPE.ADD, 100L, "Another Item"));
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/exallium/rxrecyclerview/app/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app;
26 |
27 | import android.app.Activity;
28 | import android.content.Intent;
29 | import android.os.Bundle;
30 | import android.support.v7.widget.LinearLayoutManager;
31 | import android.support.v7.widget.RecyclerView;
32 | import android.widget.Button;
33 | import butterknife.ButterKnife;
34 | import butterknife.InjectView;
35 | import com.exallium.rxrecyclerview.app.model.ObjectModel;
36 | import com.exallium.rxrecyclerview.app.rx.transformers.IdAggregator;
37 | import com.exallium.rxrecyclerview.lib.GroupComparator;
38 | import com.exallium.rxrecyclerview.lib.event.Event;
39 | import com.exallium.rxrecyclerview.lib.operators.ElementGenerationOperator;
40 | import rx.Observable;
41 | import rx.android.view.OnClickEvent;
42 | import rx.android.view.ViewObservable;
43 | import rx.functions.Action1;
44 | import rx.functions.Func1;
45 | import rx.functions.Func2;
46 |
47 | import java.util.Comparator;
48 | import java.util.Random;
49 |
50 | public class MainActivity extends Activity {
51 |
52 | @InjectView(R.id.refreshButton)
53 | Button refreshButton;
54 |
55 | @InjectView(R.id.recyclerView)
56 | RecyclerView recyclerView;
57 |
58 | @InjectView(R.id.addButton)
59 | Button addButton;
60 |
61 | @InjectView(R.id.anotherActivityButton)
62 | Button anotherActivityButton;
63 |
64 | private final GroupComparator adapterComparator = new GroupComparator() {
65 | @Override
66 | public String getGroupKey(Event longStringEvent) {
67 | if (longStringEvent.getKey() != null)
68 | return longStringEvent.getKey().toString().substring(0,1);
69 | return "EMPTY";
70 | }
71 |
72 | @Override
73 | public Event getEmptyEvent(Event.TYPE eventType) {
74 | return new Event<>(eventType, 0L, "EMPTY");
75 | }
76 |
77 | @Override
78 | public int compare(Event lhs, Event rhs) {
79 | return lhs.getKey().compareTo(rhs.getKey());
80 | }
81 | };
82 |
83 | @Override
84 | protected void onCreate(Bundle savedInstanceState) {
85 | super.onCreate(savedInstanceState);
86 | setContentView(R.layout.activity_main);
87 | ButterKnife.inject(this);
88 |
89 | recyclerView.setLayoutManager(new LinearLayoutManager(this));
90 | final Random random = new Random(System.currentTimeMillis());
91 |
92 | Observable addClicks = ViewObservable.clicks(addButton);
93 | Observable idAggregator = addClicks.compose(new IdAggregator());
94 |
95 | Observable> createEvents = Observable.zip(addClicks, idAggregator, new Func2>() {
96 | @Override
97 | public Event call(OnClickEvent onClickEvent, Long key) {
98 | return new Event<>(Event.TYPE.ADD, key, "Item");
99 | }
100 | });
101 |
102 | Observable> updateEvents = ViewObservable.clicks(refreshButton).map(new Func1>() {
103 | @Override
104 | public Event call(OnClickEvent onClickEvent) {
105 | // send a new event saying position 0 string becomes random number
106 | return new Event<>(Event.TYPE.ADD, 1L, Integer.toString(random.nextInt()));
107 | }
108 | });
109 |
110 | Observable.merge(createEvents, updateEvents).subscribe(ObjectModel.getInstance().getEventObserver());
111 |
112 | Adapter adapter = new Adapter(ObjectModel
113 | .getInstance()
114 | .getEventObservable()
115 | .lift(new ElementGenerationOperator.Builder<>(adapterComparator)
116 | .hasHeader(true).hasEmpty(true).build()));
117 | recyclerView.setAdapter(adapter);
118 |
119 | ViewObservable.clicks(anotherActivityButton).forEach(new Action1() {
120 | @Override
121 | public void call(OnClickEvent onClickEvent) {
122 | Intent i = new Intent();
123 | i.setClass(MainActivity.this, AnotherActivity.class);
124 | startActivity(i);
125 | }
126 | });
127 | }
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/app/src/main/java/com/exallium/rxrecyclerview/app/model/ObjectModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app.model;
26 |
27 | import com.exallium.rxrecyclerview.lib.event.Event;
28 | import rx.Observable;
29 | import rx.Observer;
30 | import rx.functions.Action1;
31 | import rx.functions.Func1;
32 | import rx.subjects.PublishSubject;
33 |
34 | import java.util.Collections;
35 | import java.util.LinkedHashMap;
36 | import java.util.Map;
37 |
38 | /**
39 | * Singleton to act as our Model layer.
40 | */
41 | public final class ObjectModel {
42 |
43 | private static ObjectModel instance;
44 |
45 | public static ObjectModel getInstance() {
46 | if (instance == null)
47 | instance = new ObjectModel();
48 | return instance;
49 | }
50 |
51 | // My... "database"...
52 | public Map itemMap = Collections.synchronizedMap(new LinkedHashMap());
53 |
54 | // When I get an item, I want to add it to my itemMap, and then transmit that item down the road.
55 | private Observable> getEventCacheObservable() {
56 | return Observable.from(itemMap.entrySet()).map(new Func1, Event>() {
57 | @Override
58 | public Event call(Map.Entry longStringEntry) {
59 | return new Event<>(Event.TYPE.ADD, longStringEntry.getKey(), longStringEntry.getValue());
60 | }
61 | });
62 | }
63 |
64 | private final PublishSubject> eventPublishSubject = PublishSubject.create();
65 |
66 | public final Observable> getEventObservable() {
67 | return getEventCacheObservable().mergeWith(eventPublishSubject.doOnNext(new Action1>() {
68 | @Override
69 | public void call(Event longStringEvent) {
70 | switch (longStringEvent.getType()) {
71 | case ADD:
72 | itemMap.put(longStringEvent.getKey(), longStringEvent.getValue());
73 | break;
74 | case REMOVE:
75 | itemMap.remove(longStringEvent.getKey());
76 | break;
77 | }
78 | }
79 | }));
80 | }
81 |
82 | public final Observer> getEventObserver() {
83 | return eventPublishSubject;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/exallium/rxrecyclerview/app/rx/transformers/IdAggregator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.app.rx.transformers;
26 |
27 | import rx.Observable;
28 | import rx.functions.Func1;
29 | import rx.functions.Func2;
30 |
31 | public class IdAggregator implements Observable.Transformer {
32 |
33 | @Override
34 | public Observable call(Observable tObservable) {
35 | return tObservable.map(new Func1() {
36 | @Override
37 | public Long call(T t) {
38 | return 1L;
39 | }
40 | }).scan(new Func2() {
41 | @Override
42 | public Long call(Long accumulator, Long t2) {
43 | return accumulator + t2;
44 | }
45 | });
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_another.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
30 |
35 |
40 |
45 |
50 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/exallium/RxRecyclerView/32719cddf37dfcd4907ef4d25f26f8664d7303f0/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/exallium/RxRecyclerView/32719cddf37dfcd4907ef4d25f26f8664d7303f0/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/exallium/RxRecyclerView/32719cddf37dfcd4907ef4d25f26f8664d7303f0/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/exallium/RxRecyclerView/32719cddf37dfcd4907ef4d25f26f8664d7303f0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 | RxRecyclerView
27 | Refresh
28 | Add
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
26 |
27 | buildscript {
28 | repositories {
29 | jcenter()
30 | }
31 | dependencies {
32 | classpath 'com.android.tools.build:gradle:1.1.1'
33 | classpath 'com.github.dcendents:android-maven-plugin:1.2'
34 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
35 |
36 | // NOTE: Do not place your application dependencies here; they belong
37 | // in the individual module build.gradle files
38 | }
39 | }
40 |
41 | allprojects {
42 | repositories {
43 | jcenter()
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | #
2 | # The MIT License (MIT)
3 | #
4 | # Copyright (c) 2015 Alex Hart
5 | #
6 | # Permission is hereby granted, free of charge, to any person obtaining a copy
7 | # of this software and associated documentation files (the "Software"), to deal
8 | # in the Software without restriction, including without limitation the rights
9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | # copies of the Software, and to permit persons to whom the Software is
11 | # furnished to do so, subject to the following conditions:
12 | #
13 | # The above copyright notice and this permission notice shall be included in
14 | # all copies or substantial portions of the Software.
15 | #
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | # THE SOFTWARE.
23 | #
24 |
25 | # Project-wide Gradle settings.
26 |
27 | # IDE (e.g. Android Studio) users:
28 | # Gradle settings configured through the IDE *will override*
29 | # any settings specified in this file.
30 |
31 | # For more details on how to configure your build environment visit
32 | # http://www.gradle.org/docs/current/userguide/build_environment.html
33 |
34 | # Specifies the JVM arguments used for the daemon process.
35 | # The setting is particularly useful for tweaking memory settings.
36 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
37 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
38 |
39 | # When configured, Gradle will run in incubating parallel mode.
40 | # This option should only be used with decoupled projects. More details, visit
41 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
42 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/exallium/RxRecyclerView/32719cddf37dfcd4907ef4d25f26f8664d7303f0/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #
2 | # The MIT License (MIT)
3 | #
4 | # Copyright (c) 2015 Alex Hart
5 | #
6 | # Permission is hereby granted, free of charge, to any person obtaining a copy
7 | # of this software and associated documentation files (the "Software"), to deal
8 | # in the Software without restriction, including without limitation the rights
9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | # copies of the Software, and to permit persons to whom the Software is
11 | # furnished to do so, subject to the following conditions:
12 | #
13 | # The above copyright notice and this permission notice shall be included in
14 | # all copies or substantial portions of the Software.
15 | #
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | # THE SOFTWARE.
23 | #
24 |
25 | #Wed Apr 10 15:27:10 PDT 2013
26 | distributionBase=GRADLE_USER_HOME
27 | distributionPath=wrapper/dists
28 | zipStoreBase=GRADLE_USER_HOME
29 | zipStorePath=wrapper/dists
30 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
31 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | buildscript {
26 | repositories {
27 | jcenter()
28 | mavenCentral()
29 | }
30 | dependencies {
31 | classpath 'com.android.tools.build:gradle:1.1.1'
32 | }
33 | }
34 | apply plugin: 'com.android.library'
35 | apply plugin: 'com.github.dcendents.android-maven'
36 | apply plugin: 'com.jfrog.bintray'
37 |
38 | version "2.1.2"
39 |
40 | repositories {
41 | jcenter()
42 | mavenCentral()
43 | }
44 |
45 | android {
46 | compileSdkVersion 22
47 | buildToolsVersion "21.1.2"
48 | resourcePrefix "rxrecyclerview__"
49 |
50 | defaultConfig {
51 | minSdkVersion 14
52 | targetSdkVersion 22
53 | versionCode 1
54 | versionName version
55 | }
56 | buildTypes {
57 | }
58 | }
59 |
60 | def siteUrl = 'https://github.com/Exallium/RxRecyclerView'
61 | def gitUrl = 'https://github.com/Exallium/RxRecyclerView.git'
62 | group = 'com.exallium.rxrecyclerview'
63 |
64 | install {
65 | repositories.mavenInstaller {
66 | // Generate POM.xml
67 | pom {
68 | project {
69 | packaging 'aar'
70 | name 'RxRecyclerView'
71 | url siteUrl
72 |
73 | licenses {
74 | license {
75 | name 'The MIT License'
76 | url 'http://opensource.org/licenses/MIT'
77 | }
78 | }
79 |
80 | scm {
81 | connection gitUrl
82 | developerConnection gitUrl
83 | url siteUrl
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | dependencies {
91 | compile 'com.android.support:appcompat-v7:22.2.0'
92 | compile 'com.android.support:recyclerview-v7:22.2.0'
93 | compile 'io.reactivex:rxandroid:0.24.0'
94 |
95 | testCompile 'junit:junit:4.12'
96 | }
97 |
98 | task sourcesJar(type: Jar) {
99 | from android.sourceSets.main.java.srcDirs
100 | classifier = "sources"
101 | }
102 |
103 | task javadoc(type: Javadoc) {
104 | source = android.sourceSets.main.java.srcDirs
105 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
106 | options {
107 | links("http://docs.oracle.com/javase/7/docs/api/");
108 | linksOffline("http://d.android.com/reference", "${android.sdkDirectory}/docs/reference");
109 | }
110 | }
111 |
112 | task javadocJar(type: Jar, dependsOn: javadoc) {
113 | classifier = 'javadoc'
114 | from javadoc.destinationDir
115 | }
116 |
117 | artifacts {
118 | archives javadocJar
119 | archives sourcesJar
120 | }
121 |
122 | bintray {
123 | user = mavenUser
124 | key = binTrayApiKey
125 | configurations = ['archives']
126 | pkg {
127 | repo = 'maven'
128 | name = "com.exallium.rxrecyclerview"
129 | websiteUrl = siteUrl
130 | vcsUrl = gitUrl
131 | licenses = ["MIT"]
132 | publish = true
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/lib/lib.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ahart/src/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/lib/src/androidTest/java/com/exallium/rxrecyclerview/lib/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib;
26 |
27 | import android.app.Application;
28 | import android.test.ApplicationTestCase;
29 |
30 | /**
31 | * Testing Fundamentals
32 | */
33 | public class ApplicationTest extends ApplicationTestCase {
34 | public ApplicationTest() {
35 | super(Application.class);
36 | }
37 | }
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
24 |
25 |
27 |
28 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/GroupComparator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib;
26 |
27 | import com.exallium.rxrecyclerview.lib.event.Event;
28 |
29 | import java.util.Comparator;
30 |
31 | /**
32 | * Allows for easy Grouping and Sorting of Events and their Internal Data
33 | * @param The Event Key
34 | * @param The Event Value
35 | */
36 | public interface GroupComparator extends Comparator> {
37 | /**
38 | * Given an Event in a group, get the group key associated with it.
39 | * @param event The event we are analyzing
40 | * @return A string representing the group key.
41 | */
42 | String getGroupKey(Event event);
43 |
44 | /**
45 | * Generate an empty event. Can return null if you don't plan to support an empty view.
46 | * @param eventType The type of event to generate
47 | * @return a new instance of event or null
48 | */
49 | Event getEmptyEvent(Event.TYPE eventType);
50 | }
51 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/InsertionOrderComparator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib;
26 |
27 | import com.exallium.rxrecyclerview.lib.event.Event;
28 |
29 | /**
30 | * Group Comparator that allows for easy "default" insertion order.
31 | * I recommend using System.currentTimeMillis as your Key value.
32 | * This will guarentee insertion order.
33 | *
34 | * Headers and footers will not and are not designed to work with this. This is
35 | * only for those times when you don't want sorted data to be displayed.
36 | *
37 | * @param The Value of data we are inserting
38 | */
39 | public class InsertionOrderComparator implements GroupComparator {
40 | @Override
41 | public final String getGroupKey(Event event) {
42 | return event.getKey().toString();
43 | }
44 |
45 | /**
46 | * Returns an event with -1L as Key and null as value.
47 | * @param eventType ADD or REMOVE
48 | * @return an event to throw into an EmptyElement
49 | */
50 | @Override
51 | public Event getEmptyEvent(Event.TYPE eventType) {
52 | return new Event<>(eventType, -1L, null);
53 | }
54 |
55 | /**
56 | * Default compare. Override for reverse or what have you.
57 | * @param lhs Event we have
58 | * @param rhs Event we compare to
59 | * @return -1, 0, 1 based off comparison.
60 | */
61 | @Override
62 | public int compare(Event lhs, Event rhs) {
63 | return lhs.getKey().compareTo(rhs.getKey());
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/RxRecyclerViewAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib;
26 |
27 | import android.support.v7.widget.RecyclerView;
28 | import android.util.Log;
29 | import com.exallium.rxrecyclerview.lib.element.EventElement;
30 | import com.exallium.rxrecyclerview.lib.event.Event;
31 | import rx.Observable;
32 | import rx.Subscriber;
33 | import rx.android.schedulers.AndroidSchedulers;
34 |
35 | import java.util.TreeSet;
36 | import java.util.HashMap;
37 | import java.util.Map;
38 |
39 | /**
40 | * Reactive View Adapter for RecyclerView
41 | * Currently Supported operations are:
42 | * * Modify an existing item or Add a new Item (ADD)
43 | * * Remove an item (REMOVE)
44 | *
45 | * When the Controller (Activity or Fragment) is finished, it is good to send an OnComplete signal down through the
46 | * Observer chain. This should be done in your onDestroy method. Note that this is done automatically by ViewObservable
47 | *
48 | * @param The type of Keys used for the items.
49 | * @param The kind of items we are adapting
50 | * @param Custom View Holder
51 | */
52 | public abstract class RxRecyclerViewAdapter extends RecyclerView.Adapter {
53 |
54 | private static final String TAG = RxRecyclerViewAdapter.class.getSimpleName();
55 |
56 | private final Map> items = new HashMap<>();
57 |
58 | private final TreeSet> treeSet;
59 |
60 | /**
61 | * Takes an observable of RxAdapterEvents. See example in MainActivity in sample app.
62 | * It then splits this observable into one for each supported operation. UNKNOWN is of course not supported but
63 | * can be listened for separately by the Programmer
64 | * @param observable The Stream of Events to observe and react to
65 | */
66 | public RxRecyclerViewAdapter(Observable> observable) {
67 | treeSet = new TreeSet<>();
68 | Observable> androidThreadObservable = observable.observeOn(AndroidSchedulers.mainThread());
69 | androidThreadObservable.subscribe(new RxSubscriber());
70 | }
71 |
72 | private void onError(Class> clazz, Throwable e) {
73 | Log.d(TAG, "An error happened in " + clazz.getSimpleName(), e);
74 | }
75 |
76 | @Override
77 | public final void onBindViewHolder(VH holder, int position) {
78 | EventElement element = getItemAt(position);
79 | onBindViewHolder(holder, element);
80 | }
81 |
82 | /**
83 | * Binds a ViewHolder to the given EventElement
84 | * @param holder The ViewHolder to bind to
85 | */
86 | public abstract void onBindViewHolder(VH holder, EventElement element);
87 |
88 | @Override
89 | public final int getItemCount() {
90 | return treeSet.size();
91 | }
92 |
93 | @Override
94 | public final int getItemViewType(int position) {
95 | return getItemAt(position).getViewType();
96 | }
97 |
98 | // TODO: Performance
99 | protected final EventElement getItemAt(int position) {
100 | return (EventElement) treeSet.toArray()[position];
101 | }
102 |
103 | /**
104 | * @param element The element to find
105 | * @return -1 if the item does not exist in the set, otherwise the element's index
106 | */
107 | protected final int getIndexOf(EventElement element) {
108 | return treeSet.contains(element) ? treeSet.headSet(element).size() : -1;
109 | }
110 |
111 | /**
112 | * Happens before an item is Added or Removed from the list. At this point, you can
113 | * get the original item via use of getItemAt and getIndexOf.
114 | * @param element The element we are adding or removing
115 | */
116 | protected void preProcessElement(EventElement element) { }
117 |
118 | /**
119 | * Happens after an item is Added or Removed from the list. At this point the original
120 | * item is no longer available.
121 | * @param element The element we added or removed
122 | */
123 | protected void postProcessElement(EventElement element) { }
124 |
125 | private class RxSubscriber extends Subscriber> {
126 |
127 | @Override
128 | public void onCompleted() {
129 | unsubscribe();
130 | }
131 |
132 | @Override
133 | public void onError(Throwable e) {
134 | RxRecyclerViewAdapter.this.onError(this.getClass(), e);
135 | }
136 |
137 | @Override
138 | public void onNext(EventElement rxEvent) {
139 | preProcessElement(rxEvent);
140 |
141 | int eventType = rxEvent.getViewType() >> EventElement.MASK_SHIFT;
142 | if (eventType == EventElement.HEADER_MASK || eventType == EventElement.FOOTER_MASK) {
143 | switch (rxEvent.getData().getType()) {
144 | case ADD:
145 | treeSet.add(rxEvent);
146 | notifyItemInserted(getIndexOf(rxEvent));
147 | break;
148 | case REMOVE:
149 | int pos = getIndexOf(rxEvent);
150 | if (pos != -1) {
151 | treeSet.remove(rxEvent);
152 | notifyItemRemoved(pos);
153 | }
154 | break;
155 | }
156 | } else {
157 | EventElement currentRxEvent;
158 | switch (rxEvent.getData().getType()) {
159 | case ADD:
160 | if ((currentRxEvent = items.put(rxEvent.getData().getKey(), rxEvent)) != null) {
161 | final int orgPos = getIndexOf(currentRxEvent);
162 | treeSet.remove(currentRxEvent);
163 | treeSet.add(rxEvent);
164 | final int newPos = getIndexOf(rxEvent);
165 | if (orgPos != newPos) {
166 | notifyItemMoved(orgPos, newPos);
167 | }
168 | notifyItemChanged(newPos);
169 | }
170 | else {
171 | treeSet.add(rxEvent);
172 | notifyItemInserted(getIndexOf(rxEvent));
173 | }
174 | break;
175 | case REMOVE:
176 | currentRxEvent = items.remove(rxEvent.getData().getKey());
177 | if (currentRxEvent != null) {
178 | int index = getIndexOf(currentRxEvent);
179 | if (treeSet.remove(currentRxEvent)) {
180 | notifyItemRemoved(index);
181 | }
182 | }
183 | break;
184 | }
185 | }
186 | postProcessElement(rxEvent);
187 | }
188 | }
189 | }
190 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/element/EmptyElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.element;
26 |
27 | import com.exallium.rxrecyclerview.lib.GroupComparator;
28 | import com.exallium.rxrecyclerview.lib.event.Event;
29 |
30 | public class EmptyElement extends EventElement {
31 |
32 | public EmptyElement(Event event, GroupComparator groupComparator) {
33 | super(event, groupComparator);
34 | }
35 |
36 | @Override
37 | public int getViewType() {
38 | return EMPTY_MASK << MASK_SHIFT;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/element/EventElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.element;
26 |
27 | import com.exallium.rxrecyclerview.lib.GroupComparator;
28 | import com.exallium.rxrecyclerview.lib.event.Event;
29 |
30 | /**
31 | * An Event Element contains an Event and a View Type for on screen rendering. It is comparable to other
32 | * elements for sorting.
33 | *
34 | * There are 4 types of Elements. Data elements, Headers, Footers, and Empty. Each has a mask, so that
35 | * you can subclass and create new Elements.
36 | *
37 | * @param The Event Key
38 | * @param The Event Value
39 | */
40 | public class EventElement implements Comparable> {
41 |
42 | // Max supported viewtypes per mask is 1024, including the default
43 | public static final int MASK_SHIFT = 10;
44 |
45 | public static final int DATA_MASK = 0; // 00
46 | public static final int HEADER_MASK = 1; // 01
47 | public static final int FOOTER_MASK = 2; // 10
48 | public static final int EMPTY_MASK = 3; // 11
49 |
50 | private final Event event;
51 | private final GroupComparator eventGroupComparator;
52 |
53 | public EventElement(Event event, GroupComparator groupComparator) {
54 | this.event = event;
55 | this.eventGroupComparator = groupComparator;
56 | }
57 |
58 | /**
59 | * @return The Group Key for the Wrapped Event
60 | */
61 | public final String getGroup() {
62 | return eventGroupComparator.getGroupKey(event);
63 | }
64 |
65 | /**
66 | * @return The Datatype for this Element. See Class Documentation
67 | */
68 | public int getViewType() {
69 | return DATA_MASK;
70 | }
71 |
72 | /**
73 | * @return The wrapped Event
74 | */
75 | public final Event getData() {
76 | return event;
77 | }
78 |
79 | /**
80 | * Compare us to another Element
81 | * @param another The other element
82 | * @return Comparator result (-1, 0, 1)
83 | */
84 | public int compareTo(EventElement another) {
85 | int groupComparison = getGroup().compareTo(another.getGroup());
86 | if (groupComparison != 0) {
87 | return groupComparison;
88 | }
89 |
90 | // We are always after header elements and before footer elements
91 | if (another.getViewType() >> MASK_SHIFT == HEADER_MASK)
92 | return 1;
93 | if (another.getViewType() >> MASK_SHIFT == FOOTER_MASK)
94 | return -1;
95 |
96 | return eventGroupComparator.compare(getData(), another.getData());
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/element/FooterElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.element;
26 |
27 | import com.exallium.rxrecyclerview.lib.GroupComparator;
28 | import com.exallium.rxrecyclerview.lib.event.Event;
29 |
30 | public class FooterElement extends EventElement {
31 |
32 | public FooterElement(Event event, GroupComparator groupComparator) {
33 | super(event, groupComparator);
34 | }
35 |
36 | @Override
37 | public int getViewType() {
38 | return FOOTER_MASK << MASK_SHIFT;
39 | }
40 |
41 | @Override
42 | public int compareTo(EventElement another) {
43 | int groupComparison = getGroup().compareTo(another.getGroup());
44 | if (groupComparison != 0) {
45 | return groupComparison;
46 | }
47 |
48 | if (another.getViewType() == getViewType())
49 | return 0;
50 |
51 | return 1;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/element/HeaderElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.element;
26 |
27 | import com.exallium.rxrecyclerview.lib.GroupComparator;
28 | import com.exallium.rxrecyclerview.lib.event.Event;
29 |
30 | public class HeaderElement extends EventElement {
31 | public HeaderElement(Event event, GroupComparator groupComparator) {
32 | super(event, groupComparator);
33 | }
34 |
35 | @Override
36 | public int getViewType() {
37 | return HEADER_MASK << MASK_SHIFT;
38 | }
39 |
40 | @Override
41 | public int compareTo(EventElement another) {
42 | int groupComparison = getGroup().compareTo(another.getGroup());
43 | if (groupComparison != 0) {
44 | return groupComparison;
45 | }
46 |
47 | if (another.getViewType() == getViewType())
48 | return 0;
49 |
50 | return -1;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/event/Event.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.event;
26 |
27 | /**
28 | * Immutable Event for adapter change.
29 | *
30 | * @param The Key for this object (such as an ID number)
31 | * @param The Value for this object (The object itself)
32 | */
33 | public class Event {
34 |
35 | public enum TYPE {
36 | ADD, // Used when Adding content to an RxRecyclerView
37 | REMOVE, // Used when Removing content from an RxRecyclerView
38 | }
39 |
40 | private final TYPE type;
41 | private final K key;
42 | private final V value;
43 |
44 | public Event(TYPE type, K key, V value) {
45 | this.type = type;
46 | this.key = key;
47 | this.value = value;
48 | }
49 |
50 | /**
51 | * Returns the type of this event. Finalized as this method is intended to be used
52 | * outside of the bounds of the type parameters.
53 | * @return The TYPE of this Event
54 | */
55 | public final TYPE getType() {
56 | return type;
57 | }
58 |
59 | /**
60 | * @return The Event's Key
61 | */
62 | public K getKey() {
63 | return key;
64 | }
65 |
66 | /**
67 | * @return The Event's Value
68 | */
69 | public V getValue() {
70 | return value;
71 | }
72 |
73 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/exallium/rxrecyclerview/lib/operators/ElementGenerationOperator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.exallium.rxrecyclerview.lib.operators;
26 |
27 | import com.exallium.rxrecyclerview.lib.GroupComparator;
28 | import com.exallium.rxrecyclerview.lib.element.*;
29 | import com.exallium.rxrecyclerview.lib.event.Event;
30 | import rx.Observable;
31 | import rx.Subscriber;
32 |
33 | import java.util.HashMap;
34 | import java.util.Map;
35 | import java.util.TreeMap;
36 |
37 | public class ElementGenerationOperator implements Observable.Operator, Event> {
38 |
39 | Map groupMap = new HashMap<>();
40 | private final GroupComparator groupComparator;
41 | private final boolean hasHeader;
42 | private final boolean hasFooter;
43 | private final boolean hasEmpty;
44 |
45 | public ElementGenerationOperator(Builder builder) {
46 | this.groupComparator = builder.groupComparator;
47 | this.hasFooter = builder.hasFooter;
48 | this.hasHeader = builder.hasHeader;
49 | this.hasEmpty = builder.hasEmpty;
50 | }
51 |
52 | public static class Builder {
53 | private final GroupComparator groupComparator;
54 | private boolean hasHeader = false;
55 | private boolean hasFooter = false;
56 | private boolean hasEmpty = false;
57 |
58 | public Builder(GroupComparator groupComparator) {
59 | this.groupComparator = groupComparator;
60 | }
61 |
62 | public Builder hasHeader(boolean hasHeader) {
63 | this.hasHeader = hasHeader;
64 | return this;
65 | }
66 |
67 | public Builder hasFooter(boolean hasFooter) {
68 | this.hasFooter = hasFooter;
69 | return this;
70 | }
71 |
72 | public Builder hasEmpty(boolean hasEmpty) {
73 | this.hasEmpty = hasEmpty;
74 | return this;
75 | }
76 |
77 | public ElementGenerationOperator build() {
78 | return new ElementGenerationOperator<>(this);
79 | }
80 | }
81 |
82 |
83 | @Override
84 | public Subscriber super Event> call(final Subscriber super EventElement> subscriber) {
85 | return new Subscriber>() {
86 |
87 | @Override
88 | public void onStart() {
89 | if (!subscriber.isUnsubscribed() && hasEmpty)
90 | subscriber.onNext(new EmptyElement<>(groupComparator.getEmptyEvent(Event.TYPE.ADD), groupComparator));
91 | }
92 |
93 | @Override
94 | public void onCompleted() {
95 | if (!subscriber.isUnsubscribed())
96 | subscriber.onCompleted();
97 | }
98 |
99 | @Override
100 | public void onError(Throwable e) {
101 | if (!subscriber.isUnsubscribed())
102 | subscriber.onError(e);
103 | }
104 |
105 | @Override
106 | public void onNext(Event event) {
107 | if (!subscriber.isUnsubscribed()) {
108 | final EventElement element = new EventElement<>(event, groupComparator);
109 | subscriber.onNext(element);
110 | final String groupKey = element.getGroup();
111 | final int groupSize = groupMap.containsKey(groupKey) ? groupMap.get(groupKey) : 0;
112 |
113 | switch (event.getType()) {
114 |
115 | case ADD:
116 | int startSize = groupMap.size();
117 | groupMap.put(groupKey, groupSize + 1);
118 | if (groupSize == 0) {
119 | if (hasHeader) subscriber.onNext(new HeaderElement<>(event, groupComparator));
120 | if (hasFooter) subscriber.onNext(new FooterElement<>(event, groupComparator));
121 | }
122 | if (hasEmpty && startSize == 0)
123 | subscriber.onNext(new EmptyElement<>(groupComparator.getEmptyEvent(Event.TYPE.REMOVE), groupComparator));
124 | break;
125 | case REMOVE:
126 | groupMap.put(groupKey, Math.max(0, groupSize - 1));
127 | if (groupMap.get(groupKey) == 0) {
128 | groupMap.remove(groupKey);
129 | if (hasHeader) subscriber.onNext(new HeaderElement<>(event, groupComparator));
130 | if (hasFooter) subscriber.onNext(new FooterElement<>(event, groupComparator));
131 | }
132 | if (hasEmpty && groupMap.size() == 0)
133 | subscriber.onNext(new EmptyElement<>(groupComparator.getEmptyEvent(Event.TYPE.ADD), groupComparator));
134 | break;
135 | }
136 | }
137 | }
138 | };
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/lib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 | RxRecyclerView
27 |
28 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 Alex Hart
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | include ':lib', ':app'
26 |
--------------------------------------------------------------------------------