VEINHORN / ScrollGalleryView

:bridge_at_night: Android image gallery with bottom scroll view
MIT License
531 stars 157 forks source link

Fluent API for gallery initialization #63

Closed VEINHORN closed 5 years ago

VEINHORN commented 6 years ago

It's time to provide a new fluent API for the ScrollGalleryView library which should significantly increase flexibility of gallery creation and initialization. Currently, the development of new API is going in separate fluent-api branch, so if you have some ideas how to make API more flexible - PR is welcome, or just leave a comment with your suggestions. Here I have tried to provide a simple sketch of API.

Key API features

  1. Get rid of ordered method invocation during gallery initialization.
  2. Support flexible step-by-step gallery initialization.
  3. Provide a new way to add media to your gallery (using static imports or interface).

Unclear points

  1. The place where we should check parameters which would be passed into GalleryBuilder (for now it's .build() method).

Gallery initialization

Pass view id to .from()

GalleryBuilder.from(R.id.scroll_gallery_view)
 .media(File image)
 .media(String image)
 .media(URI image)
 .media(Bitmap image)
 .build();

Pass inflated view to .from()

It's can be usefull when you use libraries such as Butterknife to bind views.

GalleryBuilder.from(R.id.scroll_gallery_view)
 .media(File image)
 .media(String image)
 .media(URI image)
 .media(Bitmap image)
 .build();

So, .media() method should support image loading from different sources, but at the same time it should use custom MediaLoader provided by external dependencies such as picasso-loader.

The GalleryBuilder class should provide a bunch of overloaded .media() methods for loading images from different sources such as file, url, uri, etc. Also .media() method should recognize by url if it's image or video (I think it's can be done by extension in the end of url).

For now all fluent API code can be found in com.veinhorn.scrollgalleryview.builder package. I'm going to keep it away from ScrollGalleryView class to abstract from gallery representation. If you think there are any reasons to add some kind of static method in ScrollGalleryView to obtain new builder instance - just leave a comment with your arguments.

Adding media

Using Java static imports

Each custom MediaLoader (for example picasso-loader) should provide implementation of MediaHelpers interface, then you can use Java static imports feature to bring this methods into your app scope:

import static PicassoMediaHelpers.*

GalleryBuilder.from(R.id.scroll_gallery_view)
 .withMedia(media(new File("path/to/image")))
 .withMedia(media(new File("folder with images")))
 .withMedia(media("<uri>"))
 .withMedia(media("<bitmap>"))
 .build();

MediaHelpers interface might looks like this:

interface MediaHelpers {
  void media(String media);
  void media(File media);
  void media(Bitmap image);
  void media(Uri media);
  // and so on ...
}

Custom MediaHelpers might looks like this:

public final class PicassoMediaHelpers implements MediaHelpers {
  private PicassoMediaHelpers() {
  }
}

Gallery settings

Here is an example of how can look ScrollGalleryView configuration in new fluent API.

import static PicassoMediaHelpers.*

GalleryBuilder.from(R.id.scroll_gallery_view)
 .withMedia(media(new File("path/to/image")))
 .withMedia(media(new File("folder with images")))
 // more media invocations here
 .settings(
   // specify gallery settings here
 )
 .build();

Additional info

Java static imports Even more about static imports Fluent interface DSL About DSLs in Java