AutoValue: Firebase Extension


Source link: https://github.com/mattlogan/auto-value-firebase

AutoValue: Firebase Extension

For value classes to be compatible with Firebase Realtime Database, they must:

  • contain public "get"-prefixed accessors or public fields, and
  • contain an empty, or "default," constructor.

If we want to use AutoValue, the first requirement is achievable -- we can prefix getters with "get" -- though many among us may prefer to name our getters without the "get" prefix.

The second requirement is impossible -- AutoValue simply doesn't generate default constructors.

This library is an AutoValue extension for generating Firebase Realtime Database compatible value classes with easy conversions between your AutoValue and FirebaseValue instances.

Usage

Just add auto-value-firebase to your project and add the @FirebaseValue annotation to your existing @AutoValue annotated classes.

This extension creates Firebase Realtime Database compatible static inner classes, each named FirebaseValue, inside your generated AutoValue classes. From there, it's easy to convert your AutoValue object to a FirebaseValue object.

Example

Here's a simple AutoValue class called Taco:

@AutoValue public abstract class Taco {

 public static Taco create(String name, List<Ingredient> ingredients, Review review) {

  return new AutoValue_Taco(name, ingredients, review);

}

 public abstract String name();

 public abstract List<Ingredient> ingredients();

 public abstract Review review();
 
}

From here, we just add the @FirebaseValue annotation. This generates our FirebaseValue class as a static inner class of AutoValue_Taco.

@AutoValue @FirebaseValue public abstract class Taco {

 public static Taco create(String name, List<Ingredient> ingredients, Review review) {

  return new AutoValue_Taco(name, ingredients, review);

}

 public static Taco create(DataSnapshot dataSnapshot) {

  return dataSnapshot.getValue(AutoValue_Taco.FirebaseValue.class).toAutoValue();

}

 public Object toFirebaseValue() {

  return new AutoValue_Taco.FirebaseValue(this);

}

 public abstract String name();

 public abstract List<Ingredient> ingredients();

 public abstract Review review();
 
}

In addition to the empty constructor required by Firebase Realtime Database, the generated FirebaseValue class includes a constructor that takes your AutoValue class as an argument. The generated FirebaseValue class also includes a toAutoValue() method to convert it back to the AutoValue instance.

The create(DataSnapshot) and toFirebaseValue methods aren't required, but this is how I'd recommend converting your AutoValue objects to and from their corresponding FirebaseValue objects.

Collections

The generated FirebaseValue class has a toMap() method that can be used for DatabaseReference's updateChildren(Map<String, Object>) method.

For example, you could add the following method to the Taco class above:

public Map<String, Object> toMap() {

return new AutoValue_Taco.FirebaseValue(this).toMap();
 
}

Additionally, if you want to deserialize a collection of objects directly from a DataSnapshot, you could just iterate through its children and convert each object from FirebaseValue to AutoValue. This method could be added to the Taco class above.

public static Map<String, Taco> createMap(DataSnapshot snapshot) {

Map<String, Taco> tacos = new HashMap<>();

for (DataSnapshot child : snapshot.getChildren()) {

  tacos.put(child.getKey(), child.getValue(AutoValue_Taco.FirebaseValue.class).toAutoValue());

}

return tacos; 
}

TypeAdapters

Type adapters can be declared and used as follows:

public class DateAdapter implements TypeAdapter<Date, Long> {

@Override
public Date fromFirebaseValue(Long value) {

  return new Date(value);

}

 @Override
public Long toFirebaseValue(Date value) {

  return value.getTime();

}
 
}
@AutoValue @FirebaseValue public abstract class Review {

 public static Review create(String description, int rating, Date reviewDate) {

  return new AutoValue_Review(description, rating, reviewDate);

}

 public abstract String description();

 public abstract int rating();

 @FirebaseAdapter(DateAdapter.class) public abstract Date dateOfReview();
 
}
 

Supported types

This extension can generate FirebaseValue classes that contain any types that Firebase Realtime Database supports as described in their documentation except for nested collections.

For example, List<Ingredient> is supported but List<List<Ingredient>> is not. This might come in a later release.

Firebase annotations

Firebase Realtime Database provides four annotations to configure the mapping from your value classes to the cloud database and back. You can annotate your methods with @Exclude or @PropertyName, and you can annotate your classes with @IgnoreExtraProperties or @ThrowOnExtraProperties.

If you want these annotations to be included in your generated FirebaseValue class, just add them to your abstract AutoValue class.

Naming

This extension uses the method names from your AutoValue class to generate the fields and getters on the generated FirebaseValue class.

As a result, it's required that you don't prefix your getters with get.

Download

This extension should be included as an apt dependency (if you're using android-apt).

The @FirebaseValue annotation is packaged separately, and should be included as a provided dependency.

Type adapter support is optional and can be included as a compile dependency.

apt 'me.mattlogan.auto.value:auto-value-firebase:1.1.0' provided 'me.mattlogan.auto.value:auto-value-firebase-annotation:1.1.0'  // Optional type adapter dependency compile 'me.mattlogan.auto.value:auto-value-firebase-adapter:1.1.0'

License

Copyright 2017 Matt Logan  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

RouteDrawer wraps Google Directions API using RxJava for Android so developers can download, parse and draw path on the map in very fast and flexible way.

The library contains two main parts:

  • RouteApi is responsible for sending request to Google's Direction API and handling the response
  • DrawerApi is responsible for drawing the path on the map

android-ripple-background is a beautiful ripple animation for your app. You can easily change its color, speed of wave, one ripple or multiple ripples.

This is fully-fledged the native BOINC client for Android and BOINC Manager (based on AndroBOINC) especially designed for working with this version of BOINC client.

Official Website

RapidJSON is a JSON parser and generator for C++. It was inspired by RapidXml.

Features:

  • Small but complete. It supports both SAX and DOM style API.
  • Fast. Its performance can be comparable to strlen().
  • Self-contained. It does not depend on external libraries.
  • Memory friendly. Each JSON value occupies exactly 16/20 bytes.
  • Unicode friendly.

EdgeEffectOverride is library designed to help override the blue overscroll_edge and overscroll_glow effects used by the the EdgeEffect class.

Simply import the project into your library, replace existing references to ScrollView, ListView, ExpandableListView, GridView & ViewPager with references to the the classes found in this library.

No need to edit any graphics, simply colorize the edge effect dynamically or via the layout xml, easy!

A Reactive wrapper around Google's YouTube Data API.

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