-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jora
committed
Dec 25, 2017
1 parent
07a9038
commit 5c3ade0
Showing
4 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[email protected]", "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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}' | ||
|
@@ -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) { | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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("[email protected]", "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 | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters