Git Product home page Git Product logo

reactive-repository's Introduction

Reactive Repository

Repository code generator with Reactive Programming for Android

This library is based on k-kagurazaka/reactive-repository that is distributed in the Apache License 2.0.
Changes from the original library are listed in the CHANGELOG.md.

Compatibility

RxJava3 is only supported as a ReactiveX implementation.
If you want to use this library with RxJava2, please use the original worked by k-kagurazaka.

Download

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

dependencies {
    implementation 'com.github.kazukinr.reactive-repository:annotation:2.0.0-alpha1'
    annotationProcessor 'com.github.kazukinr.reactive-repository:processor:2.0.0-alpha1'
}

Getting Started

Declare entity

First, declare an entity class with @InMemoryEntity or @PrefsEntity. @InMemoryEntity will be stored in memory cache and @PrefsEntity will be stored in SharedPreferences.

@InMemoryEntity
public class UserStatus {
    public String userId;
}
@PrefsEntity
public class ReadFlags {
    @PrefsKey
    public boolean isRead;
}

If you want to specify name of SharedPreferences or use default SharedPreferences, you can use preferencesName or useDefaultPreferences = true in @PrefsEntity. And you can also specify a key of SharedPreferences like @PrefsKey("is_read").

Declare repository

Next, declare a repository interface with @InMemoryRepository or @PrefsRepository.

@InMemoryRepository(UserStatus.class)
public interface UserStatusRepository {
    @Nullable
    UserStatus get();

    void save(@Nullable UserStatus userStatus);
}
@PrefsRepository(ReadFlags.class)
public interface ReadFlagsRepository {
    @NonNull
    ReadFlags get();

    void save(@Nullable ReadFlags readFlags);

    @NonNull
    Observable<ReadFlags> observe();
}

You are done!

Repactive Repository generates implementations of above repository interfaces as UserStatusRepositoryImpl and ReadFlagsRepositoryImpl. You can specify the generated class name with generatedClassName parameter of repository annotation.

Supported methods

Reactive Repository supports following methods for code generation;

getter method taking no parameter and returning the entity class
setter method taking the entity class as the only one parameter and returning void
observable method taking no parameter and returning RxJava2 Observable or Flowable of the entity class

Kotlin data class support

If you use Kotlin, immutable data class is good option for entity declaration.

@PrefsEntity
data class RealdFlags(
    @get:PrefsKey val isRead: Boolean = false
)

Note that @PrefsKey must be set to getter by @get:PrefsKey.

Commit to SharedPreferences

Generated @PrefsRepository calls SharedPreferences.Editor#apply() to store an entity as default. You can use @PrefsEntity(commitOnSave = true) to use commit() instead of apply().

Use types which is not supported by SharedPreferences

You can define @PrefsTypeAdapter to use types which is not supported by SharedPreferences.

public class Person {
    public String name;
}

@PrefsEntity(typeAdapter = PersonPairTypeAdapter.class)
public class PersonPair {
    @PrefsKey
    public Person person1;
    @PrefsKey
    public Person person2;
}

@PrefsTypeAdapter
public class PersonPairTypeAdapter {
    public static Person convert(String value) {
        return new Person(value);
    }

    public static String convert(Person value) {
        return value.name;
    }
}

@PrefsRepository(PersonPair.class)
public interface PersonPairRepository {
    @NonNull
    PersonPair get();

    void save(@Nullable PersonPair personPair);

    @NonNull
    Observable<PersonPair> observe();
}

Converter method of @PrefsTypeAdapter can be non-static. In the case, generated repository class requires @PrefsTypeAdapter instance at constructor.

Limitation

Reactive Repository requires some limitations for entity / repository definitions. If you violate a following limitation, annotation processing will be failed.

Both of @InMemoryRepository and @PrefsRepository

  • Getter and setter can be defined only once
  • @Nullable setter cannot be defined with RxJava2 integration since all streams of RxJava2 never accept null value

@InMemoryRepository

  • @Nullable getter can be defined only if the entity class has a constructor with no parameters
  • @NonNull getter and @Nullable setter cannot be defined together

@PrefsEntity

  • Constructor with no parameters is required and it is used to get default values
  • Cannot set useDefaultPreferences to true when preferencesName is set

@PrefsRepository

  • @Nullable getter cannot be defined since @PrefsRepository return the entity instance filled with default values when no data are stored

License

Copyright 2020 Kazuki Nara

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.


Copyright 2019 Keita Kagurazaka

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.

reactive-repository's People

Contributors

k-kagurazaka avatar kazukinr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.