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.
- No need to create similar DAOs abstract classes for each Entity.
- No need to create repositories for each Entity class.
- No need to create the annoying asyncTasks for each database operation
- No need to create ViewModels
- No need to create a RoomDatabase and declare all your entities.
LivingRoom will do it for you!
- Does not support migrations yet
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.
At 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 theid
andcreated_at
fields.delete(item)
: permanently deletes an item from the database.update(item)
: updates an item in the database, and updates theupdated_at
timestamps.archive(item)
: archives the item without deleting it, and setsisDeleted
to true.getAll()
: retrieves all the non-archived items from the database; returns a LiveData list.getById(long)
: gets an item using its uniqueid
and returns a LiveData object.
Use 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
.
Use 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.
Use 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.
Use 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.
Use 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.
Use 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.
Use this annotation to generate your own SELECT
query.
This annotation takes three parameters:
methodName
: the name of the generated method in the components.where
: theWHERE
clause in the select query. Other Statements, such asORDER BY
andLIMIT
, 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.
- Add the database class.
- Migrations in the database class.
- Tests automation.
- Let the user choose whether LiveData is returned or not.
- Generic queries.
Feel free to open issues