Napkin


Source link: https://github.com/AleksanderMielczarek/Napkin

Napkin

Common scopes, qualifiers and few utilities for Dagger 2. Scopes and qualifiers can be used also with toothpick.

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {

...

maven {
 url "https://jitpack.io" 
}

  
}
 
}

Core

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:napkin:0.7.0' 
}
  • with Napkin you have easy access to Component in Application
public class MyApplication extends Application implements ComponentProvider<AppComponent> {

private AppComponent appComponent;

@Override
  public void onCreate() {

super.onCreate();

appComponent = DaggerAppComponent.builder()

  .appModule(new AppModule(this))

  .build();

  
}

@Override
  public AppComponent provideComponent() {

return appComponent;
  
}
  
}
DaggerMainComponent.builder()

  .appComponent(Napkin.<AppComponent>provideAppComponent(context))

  .build()

  .inject(this);
  • in case you just need component you can use:
AppComponent appComponent = Napkin.<AppComponent>provideAppComponent(context);
DaggerMainComponent.builder()

  .appComponent(Napkin.provideAppComponent(context))

  .build()

  .inject(this);

AppComponent appComponent = Napkin.provideAppComponent(context);
  • access component stored in any class implementing ComponentProvider
Component component = Napkin.<Component>provideComponent(object);
  • access component stored in Application class implementing ComponentProvider by any Context
Component component = Napkin.<Component>provideAppComponent(context);
  • access component stored in Application class implementing ComponentProvider by Fragment
Component component = Napkin.<Component>provideAppComponent(fragment);
  • access component stored in Application class implementing ComponentProvider by View
Component component = Napkin.<Component>provideAppComponent(view);
  • access component stored in Activity class implementing ComponentProvider by Fragment
Component component = Napkin.<Component>provideActivityComponent(fragment);
  • access component stored in Activity class implementing ComponentProvider by View
Component component = Napkin.<Component>provideActivityComponent(view);
  • you can use Napkin with sub components:
@AppScope @Component(modules = AppModule.class) public interface AppComponent {

ActivityComponent with(ActivityModule activityModule);

}
@ActivityScope @Subcomponent(modules = ActivityModule.class) public interface ActivityComponent {

FragmentComponent with(FragmentModule fragmentModule);

void inject(MyActivity myActivity);
 
}
@FragmentScope @Subcomponent(modules = FragmentModule.class) public interface FragmentComponent {

void inject(MyFragment myFragment);
 
}
public class MyApplication extends Application implements ComponentProvider<AppComponent> {

  private AppComponent appComponent;

@Override
  public void onCreate() {

super.onCreate();

 appComponent = DaggerAppComponent.builder()

  .appModule(new AppModule(this))

  .build();

  
}

@Override
  public AppComponent provideComponent() {

return appComponent;
  
}
 
}
public class MyActivity extends AppCompatActivity implements ComponentProvider<ActivityComponent> {

  private ActivityComponent activityComponent;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

activityComponent = Napkin.<AppComponent>provideAppComponent(this)

  .with(new ActivityModule(this));

activityComponent.inject(this);

  
}

@Override
  public ActivityComponent provideComponent() {

return activityComponent;
  
}
 
}
public class MyFragment extends Fragment {

 @Override
  public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 Napkin.<ActivityComponent>provideActivityComponent(this)

  .with(new FragmentModule(this))

  .inject(this);

  
}

 
}

Common qualifiers

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:qualifier-common:0.7.0' 
}
  • list of annotations
@RestEndpoint

Scopes

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:scope:0.7.0' 
}
  • list of annotations
@AppScope @ActivityScope @FragmentScope @ServiceScope @ReceiverScope @ProviderScope @UserScope @SessionScope

Qualifiers

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:qualifier:0.7.0' 
}
  • list of annotations
@AppContext  @ActivityContext @FragmentContext @ServiceContext @ReceiverContext @ProviderContext

Modules

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module:0.7.0' 
}

Napkin modules can be override to add custom methods.

  • list of modules
@Module @AppScope public class NapkinAppModule extends AbstractNapkinAppModule {

public NapkinAppModule(Application application) {

super(application);

  
}

@Override
  @Provides
  @AppScope
  public Application provideApplication() {

return application;
  
}

@Override
  @Provides
  @AppScope
  @AppContext
  public Context provideContext() {

return application;
  
}
 
}
@Module @ActivityScope public class NapkinActivityModule extends AbstractNapkinActivityModule {

public NapkinActivityModule(AppCompatActivity activity) {

super(activity);

  
}

@Override
  @Provides
  @ActivityScope
  public AppCompatActivity provideActivity() {

return activity;
  
}

@Override
  @Provides
  @ActivityScope
  @ActivityContext
  public Context provideContext() {

return activity;
  
}
 
}
@Module @FragmentScope public class NapkinFragmentModule extends AbstractNapkinFragmentModule {

public NapkinFragmentModule(Fragment fragment) {

super(fragment);

  
}

@Override
  @Provides
  @FragmentScope
  public Fragment provideFragment() {

return fragment;
  
}
  
}
@Module @ServiceScope public class NapkinServiceModule extends AbstractNapkinServiceModule {

public NapkinServiceModule(Context context) {

super(context);

  
}

@Override
  @Provides
  @ServiceScope
  @ServiceContext
  public Context provideContext() {

return context;
  
}
 
}
 
@Module @ReceiverScope public class NapkinReceiverModule extends AbstractNapkinReceiverModule {

public NapkinReceiverModule(Context context) {

super(context);

  
}

@Override
  @Provides
  @ReceiverScope
  @ReceiverContext
  public Context provideContext() {

return context;
  
}
 
}
@Module @ProviderScope public class NapkinProviderModule extends AbstractNapkinProviderModule {

public NapkinProviderModule(Context context) {

super(context);

  
}

@Override
  @Provides
  @ProviderScope
  @ProviderContext
  public Context provideContext() {

return context;
  
}
 
}

Scopes with prefix Per

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:scope-per:0.7.0' 
}
  • list of annotations
@PerApp @PerActivity @PerFragment @PerService @PerReceiver @PerProvider @PerUser @PerSession

Scopes with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:scope-inverse:0.7.0' 
}
  • list of annotations
@ScopeApp @ScopeActivity @ScopeFragment @ScopeService @ScopeReceiver @ScopeProvider @ScopeUser @ScopeSession

Qualifiers with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:qualifier-inverse:0.7.0' 
}
  • list of annotations
@ContextApp  @ContextActivity @ContextFragment @ContextService @ContextReceiver @ContextProvider

Modules with scopes with prefix Per

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per:0.7.0' 
}

Modules with scopes with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse:0.7.0' 
}

Modules with qualifiers with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module-qualifier-inverse:0.7.0' 
}

Modules with scopes with prefix Per and qualifiers with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per-qualifier-inverse:0.7.0' 
}

Modules with scopes with inverted names and qualifiers with inverted names

Add the dependency

dependencies {

  compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse-qualifier-inverse:0.7.0' 
}

Changelog

0.7.0 (2017-01-07)

  • remove Dagger 2 dependency from scopes and qualifiers
  • move common qualifiers to separate sub project
  • update dependencies

0.6.1 (2016-12-01)

  • update dependencies

0.6.0 (2016-11-18)

  • rename modules
  • changes in methods for providing component

0.5.0 (2016-11-11)

  • split into sub projects
  • add modules

0.4.0 (2016-10-31)

  • add new annotations
  • delete method for retrieving component by Class

0.3.0 (2016-10-25)

  • update SDK
  • add new method for retrieve component

0.2.1 (2016-09-14)

  • update SDK

0.2.0 (2016-05-14)

  • add component provider
  • add new annotation

0.1.1 (2016-05-13)

  • add new annotations

License

Copyright 2016 Aleksander Mielczarek  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

Easy way to add TextView above other views.

LoadingButton is a custom view that shows and hide progressBar with text animation. You can specify the text animation direction.

A project which showcases usage of Dagger, RxJava and Retrolambda among other open source libraries.

Font manager for Android.

Custom layout that can rotate its view.

This project aims to solve the "password forgotten" problem for (Android) developers who happen to manage java keystore(s).

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