Skip to content

Commit

Permalink
Merge pull request #36 from tvbarthel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tbarthel-fr committed Jun 4, 2016
2 parents b12c87b + 7084921 commit 87c2f33
Show file tree
Hide file tree
Showing 23 changed files with 534 additions and 121 deletions.
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This project is a light open-source library that improves the sharing experience
* [Picasso](#picasso)
* [Glide](#glide)
* [Custom icon loader](#custom-icon-loader)
* [Comparator Provider](#comparator-provider)
* [What's next](#whats-next)
* [Contributing](#contributing)
* [License](#license)
Expand All @@ -45,7 +46,7 @@ Find more about our motivations [here](http://tvbarthel.fr/IntentShare/).
# Gradle dependency
available on jcenter.
```groovy
compile 'fr.tvbarthel.intentshare:library:0.0.1'
compile 'fr.tvbarthel.intentshare:library:0.0.2'
```

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

```java
Expand All @@ -227,7 +228,7 @@ IntentShare.with(context)
## Glide
If your are already using Glide, you may want to consider using GlideIconLoader:
```groovy
compile 'fr.tvbarthel.intentshare:glide-loader:0.0.1'
compile 'fr.tvbarthel.intentshare:glide-loader:0.0.2'
```

```java
Expand Down Expand Up @@ -257,12 +258,52 @@ IntentShare.with(context)
})
.deliver();
```
# Comparator provider
By default, target activities are sorted based on the recency of their selection from your app.
```java
/**
* Comparator used to sort {@link TargetActivity} based on the recency of their previous
* selection and their default order as fallback when they have never been selected.
* <p/>
* The ordering imposed by this comparator on a set of {@link TargetActivity}
* is not consistent with equals since c.compare(e1, e2)==0 has not the same boolean
* value as e1.equals(e2).
*/
public RecencyComparatorProvider() {

}
```
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:
```java
/**
* ˙Interface which allow to define which comparator will be provided for sorting the
* target activity inside the {@link TargetChooserActivity}.
*/
public interface TargetActivityComparatorProvider extends Parcelable {

/**
* Provide the comparator used to sort {@link TargetActivity} displayed to the user.
*
* @return comparator used to sort {@link TargetActivity} displayed to the user.
*/
Comparator<TargetActivity> provideComparator();
}
```
```java
IntentShare.with(context)
.chooserTitle("Select a sharing target : ")
.text("Default text you would like to share.")
.comparatorProvider(customComparatorProvider)
.deliver();
```
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)


# What's next
* Providing custom sorting for target activities.
* Providing easier way to share images.
* Removing dependencies on support libraries.
* Sample : implementing image selection for extra provider.

# Contributing
Contributions are very welcome (: You can contribute through GitHub by forking the repository and sending a pull request.

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -29,6 +29,6 @@ ext {
compileSdkVersion = 23
targetSdkVersion = 23
minSdkVersion = 16
versionCode = 1
versionName = "0.0.1"
versionCode = 2
versionName = "0.0.2"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
28 changes: 28 additions & 0 deletions library/src/main/java/fr/tvbarthel/intentshare/IntentShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public IntentShare[] newArray(int size) {
*/
IconLoader iconLoader;

/**
* Provide the comparator used to sort the target activities.
*/
TargetActivityComparatorProvider comparatorProvider;

/**
* Title that will be displayed in the chooser.
*/
Expand All @@ -99,6 +104,7 @@ private IntentShare(Context context) {
packageWithExtraProvider = new ArrayList<>();
this.listener = null;
this.iconLoader = new AsyncIconLoader();
this.comparatorProvider = new TargetActivity.RecencyComparatorProvider();
this.chooserTitle = context.getString(R.string.isl_default_sharing_label);
}

Expand All @@ -115,6 +121,7 @@ protected IntentShare(Parcel in) {
this.mailSubject = in.readString();
this.extraProviders = in.createTypedArrayList(ExtraProvider.CREATOR);
this.iconLoader = in.readParcelable(IconLoader.class.getClassLoader());
this.comparatorProvider = in.readParcelable(TargetActivityComparatorProvider.class.getClassLoader());
this.chooserTitle = in.readString();
}

Expand All @@ -131,6 +138,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mailSubject);
dest.writeTypedList(this.extraProviders);
dest.writeParcelable(this.iconLoader, flags);
dest.writeParcelable(this.comparatorProvider, flags);
dest.writeString(this.chooserTitle);
}

Expand Down Expand Up @@ -170,6 +178,26 @@ public IntentShare iconLoader(@NonNull IconLoader iconLoader) {
return this;
}

/**
* Provide a custom {@link java.util.Comparator} in order to sort the {@link TargetActivity}
* displayed to the user.
* <p/>
* By default, TargetActivities are sorted by the recentness of their selection. If it's the
* behaviour that you are looking for, don't use this method and let the default comparator
* bring the magic.
*
* @param comparatorProvider comparator used to sort the TargetActivities displayed to the user.
* Will override the default sorting by recentness.
* @return current {@link IntentShare} for method chaining.
*/
public IntentShare comparatorProvider(@NonNull TargetActivityComparatorProvider comparatorProvider) {
if (comparatorProvider == null) {
throw new NullPointerException("Custom comparator provider can't be null.");
}
this.comparatorProvider = comparatorProvider;
return this;
}

/**
* Text which will be shared.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fr.tvbarthel.intentshare;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
* Factory used to handle layout manager according to the device android version seamlessly
*/
final class LayoutManagerFactory {

private static final int MARSHMALLOW_SPAN_COUNT = 4;

/**
* Non instantiable class.
*/
private LayoutManagerFactory() {

}

/**
* Build the layout manager for the {@link TargetActivity} list displayed to the user
* during the target activity selection.
*
* @param context context used to instantiate layout manager.
* @return layout manager matching the native look and feel linked to the device SDK version.
*/
public static RecyclerView.LayoutManager buildLayoutManager(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, MARSHMALLOW_SPAN_COUNT);
gridLayoutManager.setSpanSizeLookup(new MarshmallowSpanSizeLookup());
return gridLayoutManager;
} else {
return new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
}
}

/**
* SpanSizeLookup used to fit the native look and feel provided by
* {@link android.content.Intent#createChooser(android.content.Intent, CharSequence)}
*/
private static final class MarshmallowSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {

@Override
public int getSpanSize(int position) {
if (position == 0) {
return MARSHMALLOW_SPAN_COUNT; // header taking the full width;
} else {
return 1; // target activity taking one unit;
}
}
}
}
Loading

0 comments on commit 87c2f33

Please sign in to comment.