Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluent API for gallery initialization #63

Closed
VEINHORN opened this issue Feb 6, 2018 · 0 comments
Closed

Fluent API for gallery initialization #63

VEINHORN opened this issue Feb 6, 2018 · 0 comments
Labels
Milestone

Comments

@VEINHORN
Copy link
Owner

VEINHORN commented Feb 6, 2018

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

@VEINHORN VEINHORN added this to the 1.0.9 milestone Feb 6, 2018
@VEINHORN VEINHORN assigned VEINHORN and unassigned VEINHORN Feb 6, 2018
@VEINHORN VEINHORN modified the milestones: 1.0.9, 1.2.0 Jan 20, 2019
@VEINHORN VEINHORN closed this as completed Feb 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant