Skip to content

Commit 87c2f33

Browse files
committed
Merge pull request #36 from tvbarthel/develop
Develop
2 parents b12c87b + 7084921 commit 87c2f33

23 files changed

+534
-121
lines changed

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This project is a light open-source library that improves the sharing experience
2323
* [Picasso](#picasso)
2424
* [Glide](#glide)
2525
* [Custom icon loader](#custom-icon-loader)
26+
* [Comparator Provider](#comparator-provider)
2627
* [What's next](#whats-next)
2728
* [Contributing](#contributing)
2829
* [License](#license)
@@ -45,7 +46,7 @@ Find more about our motivations [here](http://tvbarthel.fr/IntentShare/).
4546
# Gradle dependency
4647
available on jcenter.
4748
```groovy
48-
compile 'fr.tvbarthel.intentshare:library:0.0.1'
49+
compile 'fr.tvbarthel.intentshare:library:0.0.2'
4950
```
5051

5152
dependencies
@@ -213,7 +214,7 @@ Default icon loader used to load target activities icons is based on AsyncTask.
213214
## Picasso
214215
If your are already using Picasso, you may want to consider using PicassoIconLoader:
215216
```groovy
216-
compile 'fr.tvbarthel.intentshare:picasso-loader:0.0.1'
217+
compile 'fr.tvbarthel.intentshare:picasso-loader:0.0.2'
217218
```
218219

219220
```java
@@ -227,7 +228,7 @@ IntentShare.with(context)
227228
## Glide
228229
If your are already using Glide, you may want to consider using GlideIconLoader:
229230
```groovy
230-
compile 'fr.tvbarthel.intentshare:glide-loader:0.0.1'
231+
compile 'fr.tvbarthel.intentshare:glide-loader:0.0.2'
231232
```
232233

233234
```java
@@ -257,12 +258,52 @@ IntentShare.with(context)
257258
})
258259
.deliver();
259260
```
261+
# Comparator provider
262+
By default, target activities are sorted based on the recency of their selection from your app.
263+
```java
264+
/**
265+
* Comparator used to sort {@link TargetActivity} based on the recency of their previous
266+
* selection and their default order as fallback when they have never been selected.
267+
* <p/>
268+
* The ordering imposed by this comparator on a set of {@link TargetActivity}
269+
* is not consistent with equals since c.compare(e1, e2)==0 has not the same boolean
270+
* value as e1.equals(e2).
271+
*/
272+
public RecencyComparatorProvider() {
273+
274+
}
275+
```
276+
Instead of using the default comparator, you can implement your own comparator provider in order to customize the target activities order display to the user:
277+
```java
278+
/**
279+
* ˙Interface which allow to define which comparator will be provided for sorting the
280+
* target activity inside the {@link TargetChooserActivity}.
281+
*/
282+
public interface TargetActivityComparatorProvider extends Parcelable {
283+
284+
/**
285+
* Provide the comparator used to sort {@link TargetActivity} displayed to the user.
286+
*
287+
* @return comparator used to sort {@link TargetActivity} displayed to the user.
288+
*/
289+
Comparator<TargetActivity> provideComparator();
290+
}
291+
```
292+
```java
293+
IntentShare.with(context)
294+
.chooserTitle("Select a sharing target : ")
295+
.text("Default text you would like to share.")
296+
.comparatorProvider(customComparatorProvider)
297+
.deliver();
298+
```
299+
An example from the sample can be found here : [SocialTargetActivityComparatorProvider.java](https://github.com/tvbarthel/IntentShare/blob/develop/sample/src/main/java/fr/tvbarthel/intentsharesample/SocialTargetActivityComparatorProvider.java)
300+
301+
260302
# What's next
261-
* Providing custom sorting for target activities.
262303
* Providing easier way to share images.
263304
* Removing dependencies on support libraries.
264305
* Sample : implementing image selection for extra provider.
265-
306+
266307
# Contributing
267308
Contributions are very welcome (: You can contribute through GitHub by forking the repository and sending a pull request.
268309

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.5.0'
8+
classpath 'com.android.tools.build:gradle:2.1.2'
99
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1'
1010
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
1111
// NOTE: Do not place your application dependencies here; they belong
@@ -29,6 +29,6 @@ ext {
2929
compileSdkVersion = 23
3030
targetSdkVersion = 23
3131
minSdkVersion = 16
32-
versionCode = 1
33-
versionName = "0.0.1"
32+
versionCode = 2
33+
versionName = "0.0.2"
3434
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip

library/src/main/java/fr/tvbarthel/intentshare/IntentShare.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public IntentShare[] newArray(int size) {
7676
*/
7777
IconLoader iconLoader;
7878

79+
/**
80+
* Provide the comparator used to sort the target activities.
81+
*/
82+
TargetActivityComparatorProvider comparatorProvider;
83+
7984
/**
8085
* Title that will be displayed in the chooser.
8186
*/
@@ -99,6 +104,7 @@ private IntentShare(Context context) {
99104
packageWithExtraProvider = new ArrayList<>();
100105
this.listener = null;
101106
this.iconLoader = new AsyncIconLoader();
107+
this.comparatorProvider = new TargetActivity.RecencyComparatorProvider();
102108
this.chooserTitle = context.getString(R.string.isl_default_sharing_label);
103109
}
104110

@@ -115,6 +121,7 @@ protected IntentShare(Parcel in) {
115121
this.mailSubject = in.readString();
116122
this.extraProviders = in.createTypedArrayList(ExtraProvider.CREATOR);
117123
this.iconLoader = in.readParcelable(IconLoader.class.getClassLoader());
124+
this.comparatorProvider = in.readParcelable(TargetActivityComparatorProvider.class.getClassLoader());
118125
this.chooserTitle = in.readString();
119126
}
120127

@@ -131,6 +138,7 @@ public void writeToParcel(Parcel dest, int flags) {
131138
dest.writeString(this.mailSubject);
132139
dest.writeTypedList(this.extraProviders);
133140
dest.writeParcelable(this.iconLoader, flags);
141+
dest.writeParcelable(this.comparatorProvider, flags);
134142
dest.writeString(this.chooserTitle);
135143
}
136144

@@ -170,6 +178,26 @@ public IntentShare iconLoader(@NonNull IconLoader iconLoader) {
170178
return this;
171179
}
172180

181+
/**
182+
* Provide a custom {@link java.util.Comparator} in order to sort the {@link TargetActivity}
183+
* displayed to the user.
184+
* <p/>
185+
* By default, TargetActivities are sorted by the recentness of their selection. If it's the
186+
* behaviour that you are looking for, don't use this method and let the default comparator
187+
* bring the magic.
188+
*
189+
* @param comparatorProvider comparator used to sort the TargetActivities displayed to the user.
190+
* Will override the default sorting by recentness.
191+
* @return current {@link IntentShare} for method chaining.
192+
*/
193+
public IntentShare comparatorProvider(@NonNull TargetActivityComparatorProvider comparatorProvider) {
194+
if (comparatorProvider == null) {
195+
throw new NullPointerException("Custom comparator provider can't be null.");
196+
}
197+
this.comparatorProvider = comparatorProvider;
198+
return this;
199+
}
200+
173201
/**
174202
* Text which will be shared.
175203
* <p/>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package fr.tvbarthel.intentshare;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
import android.support.v7.widget.GridLayoutManager;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
9+
/**
10+
* Factory used to handle layout manager according to the device android version seamlessly
11+
*/
12+
final class LayoutManagerFactory {
13+
14+
private static final int MARSHMALLOW_SPAN_COUNT = 4;
15+
16+
/**
17+
* Non instantiable class.
18+
*/
19+
private LayoutManagerFactory() {
20+
21+
}
22+
23+
/**
24+
* Build the layout manager for the {@link TargetActivity} list displayed to the user
25+
* during the target activity selection.
26+
*
27+
* @param context context used to instantiate layout manager.
28+
* @return layout manager matching the native look and feel linked to the device SDK version.
29+
*/
30+
public static RecyclerView.LayoutManager buildLayoutManager(Context context) {
31+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
32+
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, MARSHMALLOW_SPAN_COUNT);
33+
gridLayoutManager.setSpanSizeLookup(new MarshmallowSpanSizeLookup());
34+
return gridLayoutManager;
35+
} else {
36+
return new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
37+
}
38+
}
39+
40+
/**
41+
* SpanSizeLookup used to fit the native look and feel provided by
42+
* {@link android.content.Intent#createChooser(android.content.Intent, CharSequence)}
43+
*/
44+
private static final class MarshmallowSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
45+
46+
@Override
47+
public int getSpanSize(int position) {
48+
if (position == 0) {
49+
return MARSHMALLOW_SPAN_COUNT; // header taking the full width;
50+
} else {
51+
return 1; // target activity taking one unit;
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)