Mara


Source link: https://github.com/txusballesteros/mara

Mara

Mara is a library to provide traits or composition capabilities to your Android or Java projects.

But at first let me tell who's Mara. Mara is my little princess, she's the first of my two sons and this library is dedicated to she. I love you my princess.

Latest Version

Why?

Why I do this library, many times I think about how I can do reusable components for my projects and how it can be isolated from all of the rest. A month ago someone show me Kotlin, a language created by JetBrains and based 100% on JVM. And I thought how I can migrate the Traits capability from Kotlin to Java. The answer to the question is this library.

How it Works

How to use

Configuring your project

Configure the APT plugin on your build.gradle file.

buildscript {

  repositories {

mavenCentral()
  
}

  dependencies {

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
  
}
 
}
  apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt'

Add the library dependencies.

dependencies {

  ...
  compile 'com.txusballesteros:mara:0.1'
  apt 'com.txusballesteros:mara-compiler:0.1' 
}

Let's play

Creating your first Trait.

@Trait public class MyFirstTrait {

  public void MyMethod() {

...
  
}

  ... 
}

Creating your first Composer.

@TraitComposer(
  traits = {

 MyFirstTrait.class,

 ...
  
}
 ) public interface MyComposer {
 
}

Consuming your Composer and your injected Traits.

public class MyApp {

  public void onCreate() {

Mara_MyComposer myComposer = new Mara_MyComposer.Builder().build();

myComposer.MyMethod();

  
}
 
}

Constructor Parameters

If your traits has constructor parameters, don't worry, Mara provides automatically setters into the composer builder class to allow set this values. Please see the example below.

@Trait public class MyFirstTrait {

  public MyFirstTrait(Context context) {

...
  
}

public void MyMethod() {

...
  
}

  ... 
}
@TraitComposer(
  traits = {

 MyFirstTrait.class,

 ...
  
}
 ) public interface MyComposer {
 
}
public class MyApp {

  public void onCreate() {

Mara_MyComposer myComposer = new MyComposer.Builder()

  .setContext(...) // This setter has been mapped

  .build();

  // to your constructor parameter automatically.
  
}
 
}

Mixing Traits

Imagine that you have two or more traits with one or more methods with the same signature. Mara can solve that stage doing method mixing. When you add some traits to a composer and these have this condition, the compiler only generate a method and call automatically to all trait with this signature. Please see the example below.

@Trait public class MyFirstTrait {

  public void initialize() {

...
  
}

  ... 
}
  @Trait public class MySecondTrait {

  public void initialize() {

...
  
}

  ... 
}
@TraitComposer(
  traits = {

 MyFirstTrait.class,

 MySecondTrait.class,

 ...
  
}
 ) public interface MyComposer {
 
}
public class MyApp {

  public void onCreate() {

Mara_MyComposer myComposer = new MyComposer.Builder().build();

myComposer.initialize();

  
}
 
}

If you want to call a method of an specific trait, you can do it. Please see the example below.

public class MyApp {

  public void onCreate() {

Mara_MyComposer myComposer = new MyComposer.Builder().build();

myComposer.get(MyFirstTrait.class).initialize();

  
}
 
}

Customizing Traits Builders

In many cases it's possible you will need customize the instantiation of your traits, for example to be integrated with third party libraries. Well, don't worry because you can create your own TraitBuilder. Please see the example below.

public class MyTraitBuilder extends TraitBuilder {

  private Context context;

public MyTraitBuilder(Context context) {

this.context = context;
  
}

@Override
  protected Collection<Object> onPrepareTraits() {

Collection<Object> result = new ArrayList<>();

result.add(new MyFirstTrait(context));

return result;
  
}
 
}
public class MyApp {

  public void onCreate() {

MyTraitBuilder myTraitBuilder = new MyTraitBuilder(this);

Mara_MyComposer myComposer = new MyComposer.Builder()

.setBuilder(myTraitBuilder)

.build();

...
  
}
 
}

License

Copyright Txus Ballesteros 2015 (@txusballesteros)

This file is part of some open source application.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

Contact: Txus Ballesteros [email protected]

Resources

With Oblique explore new styles of displaying images.

The library allows you to check if a specific application is installed or not by its package name. Also you have a helper which provides the packagenames of the most popular applications.

Periodically tracking user's location in the background.

DataFragment is a tiny tiny library that helps to prevent a little boilerplate code for retained data fragments in Android. This is really useful for holding data and long-running tasks over activity lifecycle, namely orientation changes.

Multiselect library is a powerful library to select multiple images and videos efficiently.

Common RecyclerView decorations and helpers for the Android platform.

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