Loading repository data…
Loading repository data…
msbelaid / repository
LivingRoom is another layer above the Android Room persistence library. LivingRoom generates all the boilerplate DAOs, Repositories, ViewModels and the Database.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
LivingRoom is another layer above the Android Room persistence library. LivingRoom generates all the boilerplate DAOs, Repositories and ViewModels.
You just need to mark your entities with the appropriate annotations (@Insertable, @Deletable, @Updatable, @SelectableAll ...) to harness the power of LivingRoom.
Using these annotations will generate boilerplate code for you.
LivingRoom will do it for you!
Add the following lines to your build.gradle (root)
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And these two lines to your build.gradle (app)
dependencies {
...
implementation 'com.github.msbelaid:LivingRoom:0.5'
annotationProcessor 'com.github.msbelaid.LivingRoom:LivingRoom-compiler:0.5'
...
}
LivingRoom works only with java 1.8, so you should maybe add this to your build.gradle (root)
android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
Just add @Crudable annotation to your entity and extend BasicEntity.
All the boilerplate code of Daos, Repositories and ViewModels will be generated for you.
@Crudable
@Entity
public class Note extends BasicEntity {
private String title;
private String content;
// Constructors, setters and getters
}
After building your project you can use these classes in your code
NoteDao, NoteRepository, NoteViewModel as well as a shared LivingRoomDatabase class.
These components come with the basic CRUD methods insert, delete, update, getAll and getById.
For example to use the ViewModel in your MainActivity you can do the following::
public class MainActivity extends AppCompatActivity {
NoteViewModel viewModel;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
viewModel = new ViewModelProvider(this).get(NoteViewModel.class);
viewModel.getAll().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
// Update your list;
}
});
}
public void addNote(View view) {
Note note = new Note(title, content);
viewModel.insert(note);
}
// ...
}
All annotations can only be applied to a class annotated with room @Entity.
The entity marked with LivingRoom annotations should also extend BasicEntity.
The BasicEntity contains some basic fields like the id, timestamps (created_at, updated_at) and isDeleted fields.
@CrudableAt compile time, LivingRoom generates an implementation of CRUD operations in a DAO class,
a Repository class and a ViewModel class as recommended by the Android Architecture Component
guidelines.
insert(item): inserts an object of type entity into the database and auto generates the id and created_at fields.delete(item): permanently deletes an item from the database.update(item): updates an item in the database, and updates the updated_at timestamps.archive(item): archives the item without deleting it, and sets isDeleted to true.getAll(): retrieves all the non-archived items from the database; returns a LiveData list.getById(long): gets an item using its unique id and returns a LiveData object.@InsertableUse this annotation to generate an insert method for your entities.
The method insert takes an object of the entity type and returns a long number representing the id of the inserted item.
It also saves the current timestamp in created_at.
@DeletableUse this annotation to generate a delete method for your entities.
The method delete takes an object of the entity type and permanently deletes the item from the database.
@UpdatableUse this annotation to generate an update method for your entities.
The method update takes an object of the entity type and updates it in the database.
It also sets the updated_at field to the current timestamp.
@ArchivableUse this annotation to generate an archive method for your entities.
The method archive takes an object of the entity type and soft-deletes it from the database.
It only changes the flag isDeleted to true.
@SelectableAllUse this annotation to generate a getAll() method for your entities.
The method getAll() retrieves all the items of an entity that are not archived.
It returns a LiveData list.
@SelectableByIdUse this annotation to generate getById() method for your entities.
The method getById() takes a long parameter representing the id, and returns an item.
It also returns a LiveData object.
@SelectableWhereUse this annotation to generate your own SELECT query.
This annotation takes three parameters:
methodName: the name of the generated method in the components.where: the WHERE clause in the select query. Other Statements, such as ORDER BY and LIMIT, can also be added.params: the list of the parameters (Separated by comma) .Here is an example using this annotation.
@SelectableWhere(methodName = "getArchived", where = "isDeleted = 1")
@SelectableWhere(methodName = "getDateRange",
where = "created_at > :from AND created_at < :to",
params = {"java.util.Date from", "java.util.Date to"})
@Entity
public class Note extends BasicEntity {
private String title;
private String content;
//...
}
This generates getArchived() method that returns all the archived items.
It also generates getDateRange(from, to) to select all notes in a date range.
Feel free to open issues