RxDrive


Source link: https://github.com/francescocervone/RxDrive

RxDrive

RxJava wrapper for Google Drive Android API

Why

Using Google Drive API for Android can be a little frustrating because sometimes you must define nested callbacks. If you want to avoid them, you should create an AsyncTask for each action and call synchronous methods ( await()). Anyway, you have to write some ugly and confusing code.

The purpose of RxDrive is to use Google Drive APIs with the elegance and the advantages of RxJava.

No more nested callbacks, no more AsyncTasks.

Before you start

Before you start using this library, you need to follow the instructions to create a new project with Google Drive API access ( https://developers.google.com/drive/android/get-started).

Features

Since this project is still a work in progress, additional features will come in next releases. Currently, RxDrive allows you to:

  • Authenticate users
  • Notify your app about changes of the connection state
  • Create files
  • Open files
  • Update files
  • Get metadata of Drive resources
  • List and query resources
  • Trash, untrash and delete Drive resources
  • Sync Drive

Examples

Connecting

public class MyActivity extends AppCompatActivity {

  private RxDrive mRxDrive;
  private Subscription mSubscription;
  @Override
  protected void onCreate(Bundle savedInstanceState) {

...

mRxDrive = new RxDrive(new GoogleApiClient.Builder(this)

 .addApi(Drive.API)

 .addScope(Drive.SCOPE_FILE) //If you want to access to user files

 .addScope(Drive.SCOPE_APPFOLDER) //If you want to access to the app folder

);

  
}

  @Override
  protected void onStart() {

...

mSubscription = mRxDrive.connectionObservable()

  .subscribe(new Action1<ConnectionState>() {

@Override

public void call(ConnectionState connectionState) {

 switch (connectionState.getState()) {

  case CONNECTED:

doSomething(connectionState.getBundle());

break;

  case SUSPENDED:

doSomethingElse(connectionState.getCause());

break;

  case FAILED:

mRxDrive.resolveConnection(MyActivity.this, connectionState.getConnectionResult());

break;

  case UNABLE_TO_RESOLVE:

showMessageToUser(connectionState.getConnectionResult());

break;

 
}

}

  
}
, new Action1<Throwable>() {

@Override

public void call(Throwable throwable) {
 ... 
}

  
}
);

mRxDrive.connect();

  
}

 @Override
  protected void onStop() {

...

mRxDrive.disconnect();

mSubscription.unsubscribe();

  
}

 @Override
  protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

mRxDrive.onActivityResult(requestCode, resultCode, data);

  
}
 
}

Creating a file

mRxDrive.createFile(mRxDrive.getAppFolder(), uriOrFile, optionalName, optionalMimeType)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Action1<DriveId>() {

@Override

public void call(DriveId driveId) {
 ... 
}

  
}
, new Action1<Throwable>() {

@Override

public void call(Throwable throwable) {
 ... 
}

  
}
);

Listing children of a folder

mRxDrive.listChildren(mRxDrive.getAppFolder())
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Action1<List<DriveId>>() {

@Override

public void call(List<DriveId> driveIds) {
 ... 
}

  
}
, new Action1<Throwable>() {

@Override

public void call(Throwable throwable) {
 ... 
}

  
}
);

Querying for children of a folder

Query query = new Query.Builder()  .addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))  .build() mRxDrive.queryChildren(mRxDrive.getAppFolder(), query)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Action1<List<DriveId>>() {

@Override

public void call(List<DriveId> driveIds) {
 ... 
}

  
}
, new Action1<Throwable>() {

@Override

public void call(Throwable throwable) {
 ... 
}

  
}
);

Getting metadata

mRxDrive.getMetadata(someDriveId)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Action1<Metadata>() {
  @Override

public void call(Metadata metadata) {
 ... 
}
  
}
, new Action1<Throwable>() {
  @Override

public void call(Throwable throwable) {
 ... 
}
  
}

Opening a file

mRxDrive.open(mDriveId, new Subscriber<Progress>() {

@Override

public void onCompleted() {
 ... 
}

  @Override

public void onError(Throwable e) {
 ... 
}

  @Override

public void onNext(Progress progress) {

 mTextView.setText(progress.getPercentage() + "%");

}

  
}
)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Action1<InputStream>() {

@Override

public void call(InputStream inputStream) {
 ... 
}

  
}
, new Action1<Throwable>() {

@Override

public void call(Throwable throwable) {
 ... 
}

  
}
);

Gradle

Add in your root build.gradle:

repositories {
  jcenter() 
}

Add in your app build.gradle the dependency:

dependencies {

...
compile 'com.francescocervone:rxdrive:0.2' 
}

Resources

A nice swipe layout that provides new actions with a material design look and feel.

FireCrasher is designed to handle the Uncaught Exceptions in your android application and helps to recover without exiting from the application.

EasyTabs is a library for android that helps create and customize material design tabs easily.

One of the best rich text editors for Android platform.

This Gradle plug-in creates tasks to run Infer on Android and Java projects.

Navigation tab strip with smooth interaction.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes