Skip to content

Commit

Permalink
New release 0.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jora committed Dec 25, 2017
1 parent 07a9038 commit 5c3ade0
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 42 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ project.ext {
siteUrl = 'https://github.com/Link184/Respiration'
gitUrl = 'https://github.com/Link184/Respiration.git'

libraryVersion = '0.5.1'
libraryVersion = '0.5.2'

developerId = 'link184'
developerName = 'Eugeniu Tufar'
Expand Down
61 changes: 61 additions & 0 deletions no_annotation_processor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

Respiration repositories can be initialized without annotation processor,
here you can find a example:

```java
// Init a repository:
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
GeneralRepository samplePrivateRepository = new GeneralRepository.Builder<>(SamplePrivateModel.class,
SAMPLE_PRIVATE_CHILD, currentUser != null ? currentUser.getUid() : null)
.setAccessPrivate(true) // Data are available only for authenticated users.
.setPersistence(true) // Set firebase db to be available offline
.build();

// Attach a subscriber to handle all data changes from firebase. SubscriberFirebase is a rxJava
// DisposableObserver so you can dispose or reatach it anytime.
samplePrivateRepository.subscribe(new SubscriberFirebase<SamplePrivateModel>() {
@Override
public void onSuccess(SamplePublicModel samplePublicModel) {
nameContainer.setVisibility(View.VISIBLE);
Log.d(TAG, "Found my name" + samplePublicModel.getName());
}

//Optional override
@Override
public void onFailure(Throwable error) {
//Some logic when fail.
nameContainer.setVisibility(View.GONE);
Log.e(TAG, "Fail! ", error);
}
});
```

What about firebase arrays
-----
With ListRepository you can easily wrap firebase Map<String, T> to List<T>
```
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
ListRepository<SampleFriendModel> listRepository = new ListRepository.Builder<>(SampleFriendModel.class)
.setChildren(SAMPLE_FRIENDS_CHILD, currentUser != null ? currentUser.getUid() : null)
.setAccessPrivate(true)
.setPersistence(true)
.build();
```
**Note** If user is not authenticated and you set setAccessPrivate(true) and database reference
path may include user id then you must reset database references path.
Just call samplePrivateRepository.resetRepository(), here is a example:
```
samplePrivateRepository.getFirebaseAuth()
.signInWithEmailAndPassword("sample@sample.sample", "password")
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
privateRepository.resetRepository(FirebaseModule.SAMPLE_PRIVATE_CHILD,
privateRepository.getFirebaseAuth().getCurrentUser().getUid());
}
});
```
With respiration-compiler the problem is avoided by using RespirationRepository.USER_ID as user id
in database children path

109 changes: 71 additions & 38 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,11 @@ repositories {
}
dependencies {
compile 'com.link184:respiration:0.5.0'
implementation 'com.link184:respiration:0.5.2'
annotationProcessor 'com.link184:respiration:0.5.2'
}
```

Maven:

```maven
<dependency>
<groupId>com.link184</groupId>
<artifactId>respiration</artifactId>
<version>0.5.0</version>
<type>pom</type>
</dependency>
```

**Note** Dont forget to add play services dependency
```
classpath 'com.google.gms:google-services:{version}'
Expand Down Expand Up @@ -62,16 +52,32 @@ Usage is simple like inspiration or expiration =)
Simple use cases with Respiration's will look something like this:

```java
// Init a repository:
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
GeneralRepository samplePrivateRepository = new GeneralRepository.Builder<>(SamplePrivateModel.class,
SAMPLE_PRIVATE_CHILD, currentUser != null ? currentUser.getUid() : null)
.setAccessPrivate(true) // Data are available only for authenticated users.
.setPersistence(true) // Set firebase db to be available offline
.build();

// Annotate class:
@RespirationModule
public class CustomModule {
public static final String SAMPLE_PRIVATE_CHILD = "private";

//Configure repositories with annotations
@RespirationRepository(dataSnapshotType = SamplePrivateModel.class,
isAccessPrivate = true,
children = {SAMPLE_PRIVATE_CHILD, RespirationRepository.USER_ID},
persistence = true)
public GeneralRepository samplePrivateRepository;


@RespirationRepository(dataSnapshotType = Vector.class)
public GeneralRepository myRepo2;
}
```

Obtain repository from generated Module by access "Respiration{ModuleName}"
```
//SamplePrivateRepository is generated by field name annotated with @RespirationRepository from
//CustomModule annotated with @RespirationModule.
SamplePrivateRepository privateRepository = RespirationCustomModule.getSamplePrivateRepository();
// Attach a subscriber to handle all data changes from firebase. SubscriberFirebase is a rxJava
// DisposableObserver so you can dispose or reatach it anytime.
// DisposableObserver so you can dispose or reattach it anytime.
samplePrivateRepository.subscribe(new SubscriberFirebase<SamplePrivateModel>() {
@Override
public void onSuccess(SamplePublicModel samplePublicModel) {
Expand All @@ -89,16 +95,35 @@ samplePrivateRepository.subscribe(new SubscriberFirebase<SamplePrivateModel>() {
});
```

@RespirationRepository also works on classes. Single rule is to extend class from GeneralRepository
or ListRepository
```
@RespirationRepository(dataSnapshotType = SamplePrivateModel.class,
children = {"children1", "child2", RespirationRepository.USER_ID, "child3"})
public class CustomRepository extends GeneralRepository<SamplePrivateModel> {
public CustomRepository(Configuration<SamplePrivateModel> repositoryConfig) {
super(repositoryConfig);
}
public void testMethod() {
Log.d(TAG, "testMethod: ");
}
}
```
Generated object can be accessed by generated {AnnotatedClassName}Builder class
```
CustomRepository customRepository = CustomRepositoryBuilder.getInstance();
customRepository.testMethod();
```

Also you can avoid annotation processing. [INSTRUCTIONS][4]

What about firebase arrays
-----
With ListRepository you can easily wrap firebase Map<String, T> to List<T>
```
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
ListRepository<SampleFriendModel> listRepository = new ListRepository.Builder<>(SampleFriendModel.class)
.setChildren(SAMPLE_FRIENDS_CHILD, currentUser != null ? currentUser.getUid() : null)
.setAccessPrivate(true)
.setPersistence(true)
.build();
//Get our list from generated sources, and perviosuly declared into our respiration module.
SamplePrivateListRepository listRepository = RespirationCustomModule.getSamplePrivateListRepository();
listRepositorySubscriber = new SingleSubscriberFirebase<List<SampleFriendModel>>() {
@Override
Expand Down Expand Up @@ -132,6 +157,8 @@ myRepository.asObservale()
.subscribe(...);
```

For list repository you can use asListObservable()

Don't forget to subscribe/unsubscribe according to android lifecycle.
```
@Override
Expand All @@ -151,18 +178,23 @@ Don't forget to subscribe/unsubscribe according to android lifecycle.
Also you can use SingleSubscriberFirebase class to obtain a value just once without subscription
to data changes.

**Note** If user is not authenticated and you set setAccessPrivate(true) and database reference
path may include user id then you must reset database references path.
Just call samplePrivateRepository.resetRepository(), here is a example:
If you got to use firebase authentication user id as children in firebase database reference
then use RespirationRepository.USER_ID instead firebase user id. That approach will permit
you to avoid problems with firebase authentication changes.
Example:
```
samplePrivateRepository.getFirebaseAuth()
.signInWithEmailAndPassword("sample@sample.sample", "password")
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
privateRepository.resetRepository(FirebaseModule.SAMPLE_PRIVATE_CHILD,
privateRepository.getFirebaseAuth().getCurrentUser().getUid());
}
});
//Incorrect
String firebaseUserId = FirebaseAuth.getInstance().getCurrentUser().getUserId();
@RespirationRepository(dataSnapshotType = SamplePrivateModel.class,
isAccessPrivate = true,
children = {SAMPLE_PRIVATE_CHILD, firebaseUserId},
persistence = true)
//Correct
@RespirationRepository(dataSnapshotType = SamplePrivateModel.class,
isAccessPrivate = true,
children = {SAMPLE_PRIVATE_CHILD, RespirationRepository.USER_ID},
persistence = true)
```

Sample
Expand All @@ -178,3 +210,4 @@ See the [LICENSE][1] file for details.
[1]: https://github.com/Link184/Respiration/blob/master/LICENSE
[2]: https://github.com/Link184/Respiration/blob/master/firebase_database.json
[3]: https://github.com/Link184/Respiration/releases
[4]: https://github.com/Link184/Respiration/no_annotation_processor.md
6 changes: 3 additions & 3 deletions respiration-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ dependencies {
api 'com.squareup:javapoet:1.9.0'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

publishing {
publications {
releasePublication(MavenPublication) {
Expand All @@ -42,8 +45,5 @@ publishing {
}
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

apply from: '../installv1.gradle'
apply from: '../bintrayv1.gradle'

0 comments on commit 5c3ade0

Please sign in to comment.