├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── note_item.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── pentabin
│ │ │ └── livingroom
│ │ │ ├── Note.java
│ │ │ ├── NotesAdapter.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── pentabin
│ │ │ └── livingroom
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── pentabin
│ │ └── livingroom
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── livingroom
├── .gitignore
├── consumer-rules.pro
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── pentabin
│ │ │ └── livingroom
│ │ │ ├── BasicRepository.java
│ │ │ ├── DateConverter.java
│ │ │ ├── BasicEntity.java
│ │ │ └── LivingRoomDatabase.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── pentabin
│ │ │ └── livingroom
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── pentabin
│ │ └── livingroom
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── LivingRoom-compiler
├── .gitignore
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ └── services
│ │ │ └── javax.annotation.processing.Processor
│ │ └── java
│ │ └── com
│ │ └── pentabin
│ │ └── livingroom
│ │ └── compiler
│ │ ├── LivingRoomDatabase.java
│ │ ├── AsyncMethod.java
│ │ ├── SelectMethod.java
│ │ ├── EntityClass.java
│ │ ├── LivingroomMethod.java
│ │ └── LivingRoomProcessor.java
└── build.gradle
├── LivingRoom-annotations
├── .gitignore
├── src
│ └── main
│ │ └── java
│ │ └── com
│ │ └── pentabin
│ │ └── livingroom
│ │ └── annotations
│ │ ├── SelectableAll.java
│ │ ├── SelectableById.java
│ │ ├── SelectableWheres.java
│ │ ├── Archivable.java
│ │ ├── Deletable.java
│ │ ├── Updatable.java
│ │ ├── Insertable.java
│ │ ├── SelectableWhere.java
│ │ └── Crudable.java
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── settings.gradle
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/livingroom/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/livingroom/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | com.pentabin.livingroom.compiler.LivingRoomProcessor
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/livingroom/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/msbelaid/LivingRoom/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='LivingRoom'
2 | include ':app'
3 | include ':livingroom'
4 | include ':LivingRoom-annotations'
5 | include ':LivingRoom-compiler'
6 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/SelectableAll.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | public @interface SelectableAll {
4 | }
5 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/SelectableById.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | public @interface SelectableById {
4 | }
5 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/LivingRoomDatabase.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | // TODO LivingRoomDatabase class
4 | class LivingRoomDatabase {
5 | }
6 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/SelectableWheres.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | public @interface SelectableWheres{
4 | SelectableWhere[] value();
5 | }
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LivingRoom
3 | Title
4 | Add
5 | Content
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
7 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java-library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.msbelaid'
5 |
6 | dependencies {
7 | implementation fileTree(dir: 'libs', include: ['*.jar'])
8 |
9 | }
10 |
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 15 17:29:13 CET 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/Archivable.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.SOURCE)
9 | @Target(ElementType.TYPE)
10 | public @interface Archivable {
11 | }
12 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/Deletable.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.SOURCE)
9 | @Target(ElementType.TYPE)
10 | public @interface Deletable {
11 | }
12 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/Updatable.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.SOURCE)
9 | @Target(ElementType.TYPE)
10 | public @interface Updatable {
11 | }
12 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/Insertable.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.SOURCE)
9 | @Target(ElementType.TYPE)
10 | public @interface Insertable {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/pentabin/livingroom/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/livingroom/src/main/java/com/pentabin/livingroom/BasicRepository.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.app.Application;
4 |
5 | import androidx.lifecycle.LiveData;
6 |
7 | import java.util.List;
8 |
9 | public interface BasicRepository {
10 | LiveData> getAll();
11 | Long insert(T t);
12 | void delete(T t);
13 | //void archive(T t);
14 | void update(T t);
15 | BasicRepository getInstance(Application app);
16 | //LiveData getById(long id);
17 | }
18 |
--------------------------------------------------------------------------------
/livingroom/src/test/java/com/pentabin/livingroom/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/livingroom/src/main/java/com/pentabin/livingroom/DateConverter.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import androidx.room.TypeConverter;
4 |
5 | import java.util.Date;
6 |
7 | public class DateConverter {
8 | @TypeConverter
9 | public static Date toDate(Long timestamp) {
10 | return timestamp == null ? null : new Date(timestamp);
11 | }
12 |
13 | @TypeConverter
14 | public static Long toTimestamp(Date date) {
15 | return date == null ? null : date.getTime();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java-library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.msbelaid'
5 |
6 | dependencies {
7 | implementation fileTree(dir: 'libs', include: ['*.jar'])
8 | implementation 'com.squareup:javapoet:1.12.1'
9 | implementation 'androidx.room:room-runtime:2.2.5'
10 | annotationProcessor 'androidx.room:room-compiler:2.2.5'
11 | implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
12 | implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0'
13 | implementation project(path: ':LivingRoom-annotations')
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/SelectableWhere.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 |
4 | import java.lang.annotation.Repeatable;
5 |
6 | @Repeatable(SelectableWheres.class)
7 | public @interface SelectableWhere {
8 | /**
9 | *
10 | * @return method name that will be generated in Dao, Repository and ViewModel
11 | */
12 | String methodName();
13 | /**
14 | *
15 | * @return where clause
16 | */
17 | String[] params() default {""};
18 |
19 | String where();
20 | // TODO returns? List or One Live or not
21 | boolean liveData = true;
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/livingroom/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/pentabin/livingroom/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.pentabin.livingroom", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/livingroom/src/androidTest/java/com/pentabin/livingroom/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.pentabin.livingroom.test", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/livingroom/src/main/java/com/pentabin/livingroom/BasicEntity.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import androidx.room.PrimaryKey;
4 |
5 | import java.util.Date;
6 |
7 | public abstract class BasicEntity {
8 | @PrimaryKey(autoGenerate = true)
9 | private long id;
10 | private Date created_at;
11 | private Date updated_at;
12 | private boolean isDeleted;
13 |
14 | public long getId() {
15 | return id;
16 | }
17 |
18 | public void setId(long id) {
19 | this.id = id;
20 | }
21 |
22 | public Date getCreated_at() {
23 | return created_at;
24 | }
25 |
26 | public void setCreated_at(Date created_at) {
27 | this.created_at = created_at;
28 | }
29 |
30 | public Date getUpdated_at() {
31 | return updated_at;
32 | }
33 |
34 | public void setUpdated_at(Date updated_at) {
35 | this.updated_at = updated_at;
36 | }
37 |
38 | public boolean isDeleted() {
39 | return isDeleted;
40 | }
41 |
42 | public void setDeleted(boolean deleted) {
43 | isDeleted = deleted;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/pentabin/livingroom/Note.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import androidx.room.Entity;
4 |
5 | import com.pentabin.livingroom.annotations.Crudable;
6 | import com.pentabin.livingroom.annotations.SelectableWhere;
7 |
8 | @Crudable
9 | @SelectableWhere(methodName = "getArchived", where = "isDeleted = 1")
10 | @SelectableWhere(methodName = "getDateRange",
11 | where = "created_at > :from AND created_at < :to",
12 | params = {"java.util.Date from", "java.util.Date to"})
13 | @Entity
14 | public class Note extends BasicEntity {
15 | private String title;
16 | private String content;
17 |
18 | public Note(String title, String content) {
19 | this.title = title;
20 | this.content = content;
21 | }
22 |
23 | public String getTitle() {
24 | return title;
25 | }
26 |
27 | public void setTitle(String title) {
28 | this.title = title;
29 | }
30 |
31 | public String getContent() {
32 | return content;
33 | }
34 |
35 | public void setContent(String content) {
36 | this.content = content;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/livingroom/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.msbelaid'
5 |
6 | android {
7 | compileSdkVersion 29
8 | buildToolsVersion "29.0.3"
9 |
10 | defaultConfig {
11 | minSdkVersion 23
12 | targetSdkVersion 29
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | consumerProguardFiles 'consumer-rules.pro'
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 |
27 | }
28 |
29 | dependencies {
30 | implementation fileTree(dir: 'libs', include: ['*.jar'])
31 | implementation 'androidx.room:room-runtime:2.2.5'
32 |
33 | implementation 'androidx.appcompat:appcompat:1.1.0'
34 | testImplementation 'junit:junit:4.12'
35 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37 | annotationProcessor project(path: ':LivingRoom-compiler')
38 | implementation project(path: ':LivingRoom-annotations')
39 | }
40 |
--------------------------------------------------------------------------------
/livingroom/src/main/java/com/pentabin/livingroom/LivingRoomDatabase.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.app.Application;
4 | import android.util.Log;
5 |
6 | import java.lang.reflect.InvocationTargetException;
7 | import java.lang.reflect.Method;
8 |
9 | class LivingRoomDatabase {
10 | private static final String REPO_SUFFIX = "Repository";
11 | private static final String TAG = "LivingRoomDatabase";
12 | public static BasicRepository getRepository(Class entity, Application app) {
13 | /* ClassLoader classLoader = LivingRoomDatabase.class.getClassLoader();
14 | String className = entity.getName() + REPO_SUFFIX;
15 | Log.e(TAG, "Class Name -->" + className);
16 | Method method = null;
17 | try {
18 | method = classLoader.loadClass(className).getMethod("getInstance");
19 | } catch (NoSuchMethodException e) {
20 | e.printStackTrace();
21 | } catch (ClassNotFoundException e) {
22 | e.printStackTrace();
23 | }
24 | try {
25 | Object object = method.invoke(app);
26 | return (BasicRepository) object;
27 | } catch (IllegalAccessException e) {
28 | e.printStackTrace();
29 | } catch (InvocationTargetException e) {
30 | e.printStackTrace();
31 | }
32 | */
33 | return null;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/note_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
17 |
26 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/pentabin/livingroom/NotesAdapter.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.TextView;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.recyclerview.widget.RecyclerView;
10 |
11 | import java.util.List;
12 |
13 | public class NotesAdapter extends RecyclerView.Adapter {
14 | private List noteList;
15 |
16 | @NonNull
17 | @Override
18 | public NotesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
19 | View v = LayoutInflater.from(parent.getContext())
20 | .inflate(R.layout.note_item, parent, false);
21 | return new NotesViewHolder(v);
22 | }
23 |
24 | @Override
25 | public void onBindViewHolder(@NonNull NotesViewHolder holder, int position) {
26 | Note note = noteList.get(position);
27 | holder.title.setText(note.getTitle());
28 | holder.content.setText(note.getContent());
29 | }
30 |
31 | @Override
32 | public int getItemCount() {
33 | return noteList == null ? 0 : noteList.size();
34 | }
35 |
36 | static class NotesViewHolder extends RecyclerView.ViewHolder {
37 | TextView title;
38 | TextView content;
39 | NotesViewHolder(View v) {
40 | super(v);
41 | title = v.findViewById(R.id.title);
42 | content = v.findViewById(R.id.content);
43 | }
44 | }
45 |
46 | public void setNoteList(List noteList) {
47 | this.noteList = noteList;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.pentabin.livingroom"
9 | minSdkVersion 23
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | compileOptions {
25 | sourceCompatibility 1.8
26 | targetCompatibility 1.8
27 | }
28 |
29 | }
30 |
31 | dependencies {
32 | implementation fileTree(dir: 'libs', include: ['*.jar'])
33 |
34 | implementation 'androidx.appcompat:appcompat:1.1.0'
35 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
36 | testImplementation 'junit:junit:4.12'
37 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
39 |
40 | implementation project(path: ':LivingRoom-annotations')
41 | annotationProcessor project(path: ':LivingRoom-compiler')
42 | implementation project(path: ':livingroom')
43 | implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
44 | implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0'
45 |
46 | implementation "androidx.recyclerview:recyclerview:1.1.0"
47 | implementation "androidx.cardview:cardview:1.0.0"
48 |
49 | implementation 'androidx.room:room-runtime:2.2.5'
50 | annotationProcessor 'androidx.room:room-compiler:2.2.5'
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
22 |
23 |
30 |
31 |
39 |
40 |
46 |
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/pentabin/livingroom/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 | import android.widget.EditText;
6 |
7 | import androidx.appcompat.app.AppCompatActivity;
8 | import androidx.lifecycle.ViewModelProvider;
9 | import androidx.recyclerview.widget.LinearLayoutManager;
10 | import androidx.recyclerview.widget.RecyclerView;
11 |
12 | import java.util.Collections;
13 |
14 | public class MainActivity extends AppCompatActivity {
15 | private NoteViewModel viewModel;
16 | private EditText title;
17 | private EditText content;
18 | private RecyclerView recyclerView;
19 | private NotesAdapter notesAdapter;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 | initViews();
26 | viewModel = new ViewModelProvider(this).get(NoteViewModel.class);
27 | viewModel.getAll().observe(this, notes -> {
28 | Collections.reverse(notes);
29 | notesAdapter.setNoteList(notes);
30 | notesAdapter.notifyDataSetChanged();
31 | });
32 | }
33 |
34 | private void initViews(){
35 | recyclerView = findViewById(R.id.notesList);
36 | title = findViewById(R.id.title);
37 | content = findViewById(R.id.content);
38 | recyclerView.setLayoutManager(new LinearLayoutManager(this));
39 | notesAdapter = new NotesAdapter();
40 | recyclerView.setAdapter(notesAdapter);
41 | }
42 |
43 | public void addNote(View view) {
44 | String t = title.getText().toString();
45 | String c = content.getText().toString();
46 | if (!t.isEmpty()) {
47 | Note note = new Note(t, c);
48 | viewModel.insert(note);
49 | title.setText("");
50 | content.setText("");
51 | }
52 |
53 | }
54 |
55 | }
56 |
57 |
--------------------------------------------------------------------------------
/LivingRoom-annotations/src/main/java/com/pentabin/livingroom/annotations/Crudable.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.annotations;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * Mark an entity class as Crudable (Create, Read, Update and Delete)
10 | *
11 | * This annotation can only be applied to a class annotated with room @Entity.
12 | *
13 | * The entity marked with {@link Crudable} should also extend BasicEntity.
14 | *
15 | * The BasicEntity contains some basic fields like the id and timestamps (created_at, updated_at).
16 | *
17 | * At compile time, LivingRoom will generate an implementation of CRUD operations in a DAO class,
18 | * a Repository class and ViewModel class as recommended by the Android Architecture Component.
19 | *
20 | * In order to use these classes in your proper code just use the class name suffixed with the appropriate component name.
21 | *
22 | *
23 | * ClassNameDao
24 | * ClassNameRepository
25 | * ClassNameViewModel
26 | *
27 | *
28 | * Each one of these classes will contain these must have operations
29 | *
30 | *
31 | * insert(item) to insert an item to the database, the timestamp created_at will automatically be set.
32 | * update(item) to update an item, the timestamp updated_at will automatically be set.
33 | * delete(item) to delete the item permanently
34 | * archive(item) to archive the item, will set the field isDeleted to true
35 | * getAll() returns the list of all the items in a LiveData List
36 | * getById(long id) returns the item with the specific id
37 | *
38 | *
39 |
40 | * Example:
41 | *
42 | * {@link Crudable}
43 | * @Entity
44 | * public class Note extends BasicEntity {
45 | * private String title;
46 | * private String content;
47 | * //...
48 | * }
49 | *
50 | * Using {@link Crudable} in this class will generate NoteDao, NoteRepository and NoteViewModel with all CRUD operations:
51 | * insert, update, delete, archive, getAll and getById.
52 | */
53 | @Retention(RetentionPolicy.SOURCE)
54 | @Target(ElementType.TYPE)
55 | public @interface Crudable {
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/AsyncMethod.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | import com.squareup.javapoet.ArrayTypeName;
4 | import com.squareup.javapoet.ClassName;
5 | import com.squareup.javapoet.CodeBlock;
6 | import com.squareup.javapoet.MethodSpec;
7 | import com.squareup.javapoet.ParameterizedTypeName;
8 | import com.squareup.javapoet.TypeName;
9 | import com.squareup.javapoet.TypeSpec;
10 |
11 | import javax.lang.model.element.Modifier;
12 |
13 | public class AsyncMethod extends LivingroomMethod {
14 | // this sub class has only one parameter (item of type Entity)
15 |
16 | private static final String asyncTaskSuffix = "AsyncTask";
17 | private static final String ITEM_PARAM = "item";
18 |
19 | AsyncMethod(EntityClass entityClass, String methodName) {
20 | super(entityClass, methodName);
21 | }
22 |
23 | @Override
24 | public MethodSpec.Builder generateDaoMethod() {
25 | MethodSpec.Builder methodBuilder = super.generateDaoMethod();
26 | methodBuilder.addAnnotation(this.getAnnotation());
27 |
28 | return methodBuilder;
29 | }
30 |
31 | // Example: InsertEntityAsyncTask;
32 | private String asyncTaskClassName(EntityClass entityClass) {
33 | return this.getMethodName().substring(0, 1).toUpperCase()
34 | + this.getMethodName().substring(1)
35 | + entityClass.getName()
36 | + asyncTaskSuffix;
37 | }
38 |
39 | private ParameterizedTypeName getAsyncTaskType() {
40 | ClassName asyncTaskClass = ClassName.get("android.os", "AsyncTask");
41 | return ParameterizedTypeName.get(asyncTaskClass,
42 | this.hasParams()? getParams().get(ITEM_PARAM):TypeName.get(Void.class),
43 | ClassName.get(Void.class),
44 | this.getReturnType());
45 | }
46 |
47 | @Override
48 | public MethodSpec.Builder generateRepositoryMethod(EntityClass entityClass) {
49 | MethodSpec.Builder builder = super.generateMethod();
50 | CodeBlock.Builder innerCode = CodeBlock.builder();
51 | if (getPreCode() != null) builder.addCode(this.getPreCode());
52 |
53 | if (this.isReturnVoid())
54 | innerCode
55 | .addStatement("new $N().execute($N)",
56 | asyncTaskClassName(entityClass),
57 | this.hasParams() ? ITEM_PARAM : "");
58 | else innerCode
59 | .beginControlFlow("try")
60 | .addStatement("return new $N().execute($N).get()", asyncTaskClassName(entityClass), hasParams() ? ITEM_PARAM : "")
61 | .nextControlFlow("catch ($T e)", ClassName.get(Throwable.class))
62 | .addStatement("e.printStackTrace()")
63 | .endControlFlow()
64 | .addStatement("return null");
65 |
66 | builder.addCode(innerCode.build());
67 |
68 | return builder;
69 | }
70 |
71 | public TypeSpec.Builder generateAsyncTaskClass(EntityClass entityClass) {
72 | final String asyncTaskClassName = asyncTaskClassName(entityClass);
73 | String params = this.hasParams() ? "(items[0])" : "()";
74 |
75 | MethodSpec.Builder doInBackground = MethodSpec.methodBuilder("doInBackground")
76 | .addModifiers(Modifier.PROTECTED)
77 | .addParameter(ArrayTypeName.of(entityClass.getTypeName()), "items").varargs()
78 | .addAnnotation(Override.class);
79 |
80 | if (this.isReturnVoid())
81 | doInBackground.addStatement(entityClass.getDaoClassName().toLowerCase() + "." + this.getMethodName() + params)
82 | .addStatement("return null");
83 | else
84 | doInBackground.addStatement("return " + entityClass.getDaoClassName().toLowerCase() + "." + this.getMethodName() + params);
85 |
86 | doInBackground.returns(this.getReturnType());
87 |
88 | TypeSpec.Builder asyncTask = TypeSpec.classBuilder(asyncTaskClassName)
89 | .superclass(getAsyncTaskType())
90 | .addMethod(doInBackground.build());
91 |
92 | return asyncTask;
93 |
94 | }
95 |
96 | @Override
97 | public MethodSpec.Builder generateViewModelMethod(EntityClass entityClass) {
98 | MethodSpec.Builder builder = super.generateMethod();
99 | CodeBlock.Builder innerCode = CodeBlock.builder();
100 | innerCode.addStatement("$N $N.$N($N)",
101 | this.isReturnVoid()?"":"return",
102 | entityClass.getRepositoryClassName().toLowerCase(),
103 | this.getMethodName(),
104 | this.hasParams() ? ITEM_PARAM : "");
105 |
106 | builder.addCode(innerCode.build());
107 | return builder;
108 | }
109 |
110 | }
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/SelectMethod.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | import androidx.room.Query;
4 |
5 | import com.squareup.javapoet.AnnotationSpec;
6 | import com.squareup.javapoet.ClassName;
7 | import com.squareup.javapoet.CodeBlock;
8 | import com.squareup.javapoet.MethodSpec;
9 | import com.squareup.javapoet.ParameterizedTypeName;
10 | import com.squareup.javapoet.TypeName;
11 |
12 | import java.util.Iterator;
13 | import java.util.Map;
14 |
15 | public class SelectMethod extends LivingroomMethod {
16 | private static final String LIST = "List";
17 | private final String where;
18 | private boolean isLiveData; // TODO can either be live or not
19 | private boolean isList;
20 |
21 | SelectMethod(String methodName, String where, EntityClass entityClass, String[] params, boolean isList) {
22 | super(entityClass, methodName);
23 | this.isList = isList;
24 | //this.isLiveData = isLiveData;
25 | this.where = where;
26 | this.setReturnType(getReturnType());
27 | this.setAnnotation(Query.class);
28 |
29 | if (params != null && params.length>=1) {
30 | for (String s : params) {
31 | String[] a = (s!=null) ? s.split("\\s+"): new String[1];
32 | if (a.length != 2) ; // TODO error
33 | else this.addParam(a[0], a[1]);
34 | }
35 | }
36 | }
37 |
38 | @Override
39 | public MethodSpec.Builder generateDaoMethod() {
40 | MethodSpec.Builder methodBuilder = super.generateDaoMethod();
41 | methodBuilder.addAnnotation(AnnotationSpec.builder(this.getAnnotation())
42 | .addMember("value", "\"SELECT * FROM " + getEntityClass().getName() + " WHERE " + where + "\"")
43 | .build());
44 |
45 | return methodBuilder;
46 | }
47 |
48 | @Override
49 | public MethodSpec.Builder generateRepositoryMethod(EntityClass entityClass) {
50 | MethodSpec.Builder builder = super.generateMethod();
51 | CodeBlock.Builder innerCode = CodeBlock.builder();
52 | if (!this.hasParams()) {
53 | innerCode = innerCode.beginControlFlow("if ($N == null)", this.getMethodName() + LIST)
54 | .addStatement("$N = $N.$N($N)", this.getMethodName() + LIST,
55 | entityClass.getDaoClassName().toLowerCase(),
56 | this.getMethodName(),
57 | getParametersString())
58 | .endControlFlow()
59 | .addStatement("return $N", this.getMethodName() + LIST);
60 | }
61 | else {
62 | innerCode = innerCode
63 | .addStatement("return $N.$N($N)",
64 | entityClass.getDaoClassName().toLowerCase(),
65 | this.getMethodName(),
66 | getParametersString());
67 | }
68 | builder.addCode(innerCode.build());
69 | return builder;
70 | }
71 |
72 | // TODO Repo and VM are similar
73 | @Override
74 | public MethodSpec.Builder generateViewModelMethod(EntityClass entityClass){
75 | MethodSpec.Builder builder = super.generateMethod();
76 | CodeBlock.Builder innerCode = CodeBlock.builder();
77 | if (!this.hasParams()) {
78 | innerCode = innerCode.beginControlFlow("if ($N == null)", this.getMethodName() + LIST)
79 | .addStatement("$N = $N.$N()", this.getMethodName() + LIST,
80 | entityClass.getRepositoryClassName().toLowerCase(),
81 | this.getMethodName())
82 | .endControlFlow()
83 | .addStatement("return $N", this.getMethodName() + LIST);
84 | }
85 | else {
86 | innerCode = innerCode
87 | .addStatement("return $N.$N($N)",
88 | entityClass.getRepositoryClassName().toLowerCase(),
89 | this.getMethodName(),
90 | getParametersString());
91 | }
92 | builder.addCode(innerCode.build());
93 |
94 | return builder;
95 | }
96 |
97 | ParameterizedTypeName getReturnType(){
98 | ClassName liveDataClass = ClassName.get("androidx.lifecycle", "LiveData");
99 | ClassName listClass = ClassName.get("java.util", LIST);
100 | return isList?
101 | ParameterizedTypeName.get(liveDataClass, ParameterizedTypeName.get(listClass, getEntityClass().getTypeName()))
102 | : ParameterizedTypeName.get(liveDataClass,getEntityClass().getTypeName());
103 | }
104 |
105 | private String getParametersString(){
106 | if (!hasParams()) return "";
107 | StringBuilder parameters = new StringBuilder();
108 | Iterator> iterator = getParams().entrySet().iterator();
109 | while (iterator.hasNext()) {
110 | Map.Entry entry = iterator.next();
111 | parameters.append(entry.getKey()).append(iterator.hasNext() ? "," : "");
112 | }
113 | return parameters.toString();
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/EntityClass.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | import androidx.room.Dao;
4 |
5 | import com.squareup.javapoet.ClassName;
6 | import com.squareup.javapoet.MethodSpec;
7 | import com.squareup.javapoet.TypeName;
8 | import com.squareup.javapoet.TypeSpec;
9 |
10 | import java.util.HashSet;
11 | import java.util.List;
12 | import java.util.Set;
13 |
14 | import javax.lang.model.element.Modifier;
15 | import javax.lang.model.element.TypeElement;
16 |
17 | import static com.pentabin.livingroom.compiler.LivingRoomProcessor.dbClassName;
18 |
19 | class EntityClass {
20 | private static final String SUFFIX_DAO = "Dao";
21 | private static final String SUFFIX_REPO = "Repository";
22 | private static final String SUFFIX_VM = "ViewModel";
23 |
24 | private String packageName;
25 | private final String name;
26 | private final TypeName typeName;
27 | private final TypeElement typeElement;
28 | private final String daoClassName;
29 | private final String repositoryClassName;
30 | private final String viewModelClassName;
31 | private final Set methodsSet;
32 |
33 | public EntityClass(TypeElement entityClass) {
34 | this.typeElement = entityClass;
35 | this.name = entityClass.getSimpleName().toString();
36 | this.typeName = TypeName.get(entityClass.asType());
37 | this.daoClassName = entityClass.getSimpleName().toString() + SUFFIX_DAO;
38 | this.repositoryClassName = entityClass.getSimpleName().toString() + SUFFIX_REPO;
39 | this.viewModelClassName = entityClass.getSimpleName().toString() + SUFFIX_VM;
40 | int lastDot = entityClass.getQualifiedName().toString().lastIndexOf('.');
41 | if (lastDot > 0) {
42 | this.packageName = entityClass.getQualifiedName().toString().substring(0, lastDot);
43 | }
44 | methodsSet = new HashSet<>();
45 | }
46 |
47 | private String getPackageName() {
48 | return packageName;
49 | }
50 |
51 | String getName() {
52 | return name;
53 | }
54 |
55 | TypeName getTypeName() {
56 | return typeName;
57 | }
58 |
59 | TypeElement getTypeElement() {
60 | return typeElement;
61 | }
62 |
63 | String getDaoClassName() {
64 | return daoClassName;
65 | }
66 |
67 | String getRepositoryClassName() {
68 | return repositoryClassName;
69 | }
70 |
71 | private String getViewModelClassName() {
72 | return viewModelClassName;
73 | }
74 |
75 | private Set extends LivingroomMethod> getMethodsSet() {
76 | return methodsSet; // TODO use immutableSet
77 | }
78 |
79 | void addMethod(LivingroomMethod method) {
80 | methodsSet.add(method);
81 | }
82 |
83 | void addMethods(List method) {
84 | methodsSet.addAll(method);
85 | }
86 |
87 | @Override
88 | public boolean equals(Object o) {
89 | if (this == o) {
90 | return true;
91 | }
92 | if (o instanceof EntityClass) {
93 | return this.typeElement.getQualifiedName().equals(((EntityClass) o).typeElement.getQualifiedName());
94 | }
95 | return super.equals(o);
96 | }
97 |
98 | TypeSpec generateDaoClass(){
99 | TypeSpec.Builder daoClass = TypeSpec.interfaceBuilder(this.getDaoClassName())
100 | .addModifiers(Modifier.PUBLIC)
101 | .addAnnotation(Dao.class);
102 |
103 | for (LivingroomMethod m: this.getMethodsSet()) {
104 | daoClass.addMethod(m.generateDaoMethod().build());
105 | }
106 | return daoClass.build();
107 | }
108 |
109 | TypeSpec generateRepositoryClass() {
110 | final String dbField = dbClassName.toLowerCase();
111 |
112 | MethodSpec constructor = MethodSpec.constructorBuilder()
113 | .addModifiers(Modifier.PUBLIC)
114 | .addParameter(ClassName.get("android.app", "Application"), "app")
115 | .addStatement("$N = $T.getDatabase(app)", dbField, ClassName.get(this.getPackageName(), dbClassName))
116 | .addStatement("$N = $N.$N()", this.getDaoClassName().toLowerCase(), dbField, this.getDaoClassName().toLowerCase())
117 | .build();
118 |
119 | TypeSpec.Builder repositoryClass = TypeSpec.classBuilder(this.getRepositoryClassName())
120 | .addModifiers(Modifier.PUBLIC)
121 | .addField(ClassName.get(this.getPackageName(), dbClassName), dbField, Modifier.PRIVATE) // TODO package for db!
122 | .addField(ClassName.get(this.getPackageName(), this.getDaoClassName()), this.getDaoClassName().toLowerCase(), Modifier.PRIVATE)
123 | .addMethod(constructor);
124 |
125 | for (LivingroomMethod m: this.getMethodsSet()) {
126 | if (!m.hasParams())
127 | repositoryClass.addField(((SelectMethod)m).getReturnType(), m.getMethodName()+"List", Modifier.PRIVATE);// TODO test if live or not????
128 | repositoryClass.addMethod(m.generateRepositoryMethod(this).build());
129 | if (m instanceof AsyncMethod) {
130 | repositoryClass.addType(
131 | ((AsyncMethod)m).generateAsyncTaskClass(this).build());
132 | }
133 | }
134 | return repositoryClass.build();
135 | }
136 |
137 | TypeSpec generateViewModelClass() {
138 | MethodSpec constructor = MethodSpec.constructorBuilder()
139 | .addModifiers(Modifier.PUBLIC)
140 | .addParameter(ClassName.get("android.app", "Application"), "app")
141 | .addStatement("super(app)")
142 | .addStatement("$N = new $T(app)", this.getRepositoryClassName().toLowerCase(), ClassName.get(this.getPackageName(), this.getRepositoryClassName()))
143 | .build();
144 |
145 | TypeSpec.Builder viewModelClass = TypeSpec.classBuilder(this.getViewModelClassName())
146 | .superclass(ClassName.get("androidx.lifecycle","AndroidViewModel"))
147 | .addModifiers(Modifier.PUBLIC)
148 | .addField(ClassName.get(this.getPackageName(), this.getRepositoryClassName()), this.getRepositoryClassName().toLowerCase(), Modifier.PRIVATE)
149 | .addMethod(constructor);
150 | for (LivingroomMethod m: this.getMethodsSet()) {
151 | if (!m.hasParams()) {
152 | viewModelClass.addField(((SelectMethod)m).getReturnType(), m.getMethodName()+"List", Modifier.PRIVATE);
153 | }
154 | viewModelClass.addMethod(m.generateViewModelMethod(this).build());
155 | }
156 | return viewModelClass.build();
157 | }
158 |
159 | }
160 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/LivingroomMethod.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | import androidx.room.Delete;
4 | import androidx.room.Insert;
5 | import androidx.room.Update;
6 |
7 | import com.squareup.javapoet.ClassName;
8 | import com.squareup.javapoet.CodeBlock;
9 | import com.squareup.javapoet.MethodSpec;
10 | import com.squareup.javapoet.TypeName;
11 |
12 | import java.util.ArrayList;
13 | import java.util.Date;
14 | import java.util.HashMap;
15 | import java.util.List;
16 | import java.util.Map;
17 |
18 | import javax.lang.model.element.Modifier;
19 |
20 | abstract class LivingroomMethod {
21 | private final String methodName;
22 | private CodeBlock preCode;
23 | private Class annotation;
24 | private final EntityClass entityClass;
25 | private final Map params;
26 | private TypeName returnType;
27 |
28 | static final String INSERT = "insert";
29 | static final String DELETE = "delete";
30 | static final String SOFT_DELETE = "archive";
31 | static final String UPDATE = "update";
32 | static final String GET_ALL = "getAll";
33 | static final String GET_BY_ID = "getById";
34 |
35 | LivingroomMethod(EntityClass entityClass, String methodName) {
36 | this.entityClass = entityClass;
37 | this.methodName = methodName;
38 | this.params = new HashMap<>();
39 | }
40 |
41 | static LivingroomMethod of(EntityClass entityClass, String methodName) {
42 | switch (methodName) {
43 | case INSERT:
44 | return insertMethod(entityClass);
45 | case DELETE:
46 | return deleteMethod(entityClass);
47 | case SOFT_DELETE:
48 | return archiveMethod(entityClass);
49 | case UPDATE:
50 | return updateMethod(entityClass);
51 | case GET_ALL:
52 | return selectAllMethod(entityClass);
53 | case GET_BY_ID:
54 | return selectByIdMethod(entityClass);
55 | default:
56 | }
57 | return null;
58 | }
59 |
60 | private static LivingroomMethod insertMethod(EntityClass entityClass) {
61 | AsyncMethod method = new AsyncMethod(entityClass, INSERT);
62 | method.setAnnotation(Insert.class);
63 | method.addParam(entityClass.getTypeName(), "item");
64 | method.setReturnType(TypeName.get(Long.class));
65 | method.setPreCode(CodeBlock.builder()
66 | .addStatement("item.setCreated_at(new $T())", Date.class) //TODO CreatedAt string as constant!!
67 | .build());
68 | return method;
69 | }
70 |
71 | private static LivingroomMethod deleteMethod(EntityClass entityClass) {
72 | AsyncMethod method = new AsyncMethod(entityClass, DELETE);
73 | method.addParam(entityClass.getTypeName(), "item");
74 | method.setReturnType(TypeName.get(Void.class));
75 | method.setAnnotation(Delete.class);
76 | return method;
77 | }
78 |
79 | private static LivingroomMethod updateMethod(EntityClass entityClass) {
80 | AsyncMethod method = new AsyncMethod(entityClass, UPDATE);
81 | method.setAnnotation(Update.class);
82 | method.addParam(entityClass.getTypeName(), "item");
83 | method.setReturnType(TypeName.get(Void.class));
84 | method.setPreCode(CodeBlock.builder()
85 | .addStatement("item.setUpdated_at(new $T())", Date.class)
86 | .build());
87 | return method;
88 | }
89 |
90 |
91 | private static LivingroomMethod archiveMethod(EntityClass entityClass) {
92 | AsyncMethod method = new AsyncMethod(entityClass, UPDATE);
93 | method.setAnnotation(Update.class);
94 | method.addParam(entityClass.getTypeName(), "item");
95 | method.setReturnType(TypeName.get(Void.class));
96 | method.setPreCode(CodeBlock.builder()
97 | .addStatement("item.setUpdated_at(new $T())", Date.class)
98 | .addStatement("item.setDeleted($N)", "true")
99 | .build());
100 | return method;
101 | }
102 |
103 | private static LivingroomMethod selectAllMethod(EntityClass entityClass) {
104 | return selectWhereMethod(entityClass, GET_ALL, "isDeleted = 0", null, true);
105 | }
106 |
107 | private static LivingroomMethod selectByIdMethod(EntityClass entityClass) {
108 | String[] params = {"Long id"};
109 |
110 | return selectWhereMethod(entityClass, GET_BY_ID, "id = :id", params, false);
111 | }
112 |
113 | static LivingroomMethod selectWhereMethod(EntityClass entityClass, String methodName, String where, String[] params, boolean isList) {
114 | return new SelectMethod(methodName, where, entityClass, params, isList);
115 | }
116 |
117 | static List crud(EntityClass entityClass) {
118 | List list = new ArrayList<>();
119 | list.add(insertMethod(entityClass));
120 | list.add(deleteMethod(entityClass));
121 | list.add(archiveMethod(entityClass));
122 | list.add(updateMethod(entityClass));
123 | list.add(selectAllMethod(entityClass));
124 | list.add(selectByIdMethod(entityClass));
125 | return list;
126 | }
127 | String getMethodName() {
128 | return methodName;
129 | }
130 |
131 | TypeName getReturnType() {
132 | return returnType;
133 | }
134 |
135 | boolean isReturnVoid() {
136 | return returnType.equals(TypeName.get(Void.class));
137 | }
138 |
139 | void setPreCode(CodeBlock preCode) {
140 | this.preCode = preCode;
141 | }
142 |
143 | CodeBlock getPreCode() {
144 | return preCode;
145 | }
146 |
147 | Class getAnnotation() {
148 | return annotation;
149 | }
150 |
151 | void setAnnotation(Class annotation) {
152 | this.annotation = annotation;
153 | }
154 |
155 | void setReturnType(TypeName returnType) {
156 | this.returnType = returnType;
157 | }
158 |
159 | Map getParams() {
160 | return params;
161 | }
162 |
163 | EntityClass getEntityClass() {
164 | return entityClass;
165 | }
166 |
167 |
168 | @Override
169 | public boolean equals(Object o) {
170 | if (this == o) {
171 | return true;
172 | }
173 | if (o instanceof LivingroomMethod) {
174 | return this.methodName.equals(((LivingroomMethod) o).getMethodName());
175 | }
176 |
177 | if (o instanceof String) {
178 | return this.methodName.equals(o);
179 | }
180 | return super.equals(o);
181 | }
182 |
183 | @Override
184 | public int hashCode() {
185 | return methodName.hashCode();
186 | }
187 |
188 | void addParam(String type, String name) {
189 | params.put(name, ClassName.bestGuess(type));
190 | }
191 |
192 | void addParam(TypeName type, String name) {
193 | params.put(name, type);
194 | }
195 |
196 | MethodSpec.Builder generateMethod(){
197 | MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(this.getMethodName())
198 | .addModifiers(Modifier.PUBLIC);
199 |
200 | if (hasParams()) {
201 | params.forEach((k,v) -> {
202 | methodBuilder.addParameter(v, k);
203 | });
204 | }
205 |
206 | if (!this.isReturnVoid())
207 | methodBuilder.returns(returnType);
208 |
209 | return methodBuilder;
210 | }
211 |
212 | public MethodSpec.Builder generateDaoMethod(){
213 | return generateMethod().addModifiers(Modifier.ABSTRACT);
214 | }
215 |
216 | public abstract MethodSpec.Builder generateRepositoryMethod(EntityClass entityClass);
217 |
218 | public abstract MethodSpec.Builder generateViewModelMethod(EntityClass entityClass);
219 |
220 | boolean hasParams() {
221 | return (params != null) && !params.isEmpty();
222 | }
223 |
224 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LivingRoom [](https://twitter.com/intent/tweet?url=https%3A%2F%2Fgithub.com%2Fmsbelaid%2FLivingRoom&via=msbelaid&text=LivingRoom%20generates%20all%20the%20boilerplate%20code%20for%20Android%20Room%20Persistence%20Library&hashtags=Android%2C%20AndroidDev)
2 | LivingRoom is another layer above the Android [Room](https://developer.android.com/topic/libraries/architecture/room)
3 | persistence library. LivingRoom generates all the boilerplate [DAOs](https://developer.android.com/training/data-storage/room/accessing-data), Repositories and [ViewModels](https://developer.android.com/topic/libraries/architecture/viewmodel).
4 |
5 | You just need to mark your entities with the appropriate annotations (`@Insertable`, `@Deletable`, `@Updatable`, `@SelectableAll` ...) to harness the power of [LivingRoom](https://github.com/msbelaid/LivingRoom).
6 |
7 | [](https://jitpack.io/#msbelaid/LivingRoom)
8 | [](https://www.producthunt.com/posts/livingroom)
9 |
10 | ### Pros
11 | Using these annotations will generate boilerplate code for you.
12 | * No need to create similar [DAOs](https://developer.android.com/training/data-storage/room/accessing-data) abstract classes for each Entity.
13 | * No need to create repositories for each Entity class.
14 | * No need to create the annoying asyncTasks for each database operation
15 | * No need to create [ViewModels](https://developer.android.com/topic/libraries/architecture/viewmodel)
16 | * No need to create a [RoomDatabase](https://developer.android.com/reference/androidx/room/RoomDatabase.html) and declare all your entities.
17 |
18 | [LivingRoom](https://github.com/msbelaid/LivingRoom) will do it for you!
19 |
20 | ### Cons
21 | * Does not support migrations yet
22 |
23 | # How to install
24 | Add the following lines to your `build.gradle` (root)
25 |
26 | ```
27 | allprojects {
28 | repositories {
29 | ...
30 | maven { url 'https://jitpack.io' }
31 | }
32 | }
33 | ```
34 | And these two lines to your `build.gradle` (app)
35 | ```
36 | dependencies {
37 | ...
38 | implementation 'com.github.msbelaid:LivingRoom:0.5'
39 | annotationProcessor 'com.github.msbelaid.LivingRoom:LivingRoom-compiler:0.5'
40 | ...
41 | }
42 | ```
43 | *LivingRoom* works only with java 1.8, so you should maybe add this to your `build.gradle` (root)
44 | ```
45 | android {
46 | ...
47 | compileOptions {
48 | sourceCompatibility 1.8
49 | targetCompatibility 1.8
50 | }
51 | }
52 | ```
53 | # How to use
54 | Just add `@Crudable` annotation to your entity and extend `BasicEntity`.
55 | All the boilerplate code of Daos, Repositories and ViewModels will be generated for you.
56 |
57 | ```java
58 | @Crudable
59 | @Entity
60 | public class Note extends BasicEntity {
61 | private String title;
62 | private String content;
63 | // Constructors, setters and getters
64 | }
65 | ```
66 |
67 | After building your project you can use these classes in your code
68 | `NoteDao`, `NoteRepository`, `NoteViewModel` as well as a shared `LivingRoomDatabase` class.
69 | These components come with the basic CRUD methods `insert`, `delete`, `update`, `getAll` and `getById`.
70 |
71 | For example to use the ViewModel in your MainActivity you can do the following::
72 |
73 | ```java
74 | public class MainActivity extends AppCompatActivity {
75 | NoteViewModel viewModel;
76 | //...
77 | @Override
78 | protected void onCreate(Bundle savedInstanceState) {
79 | //...
80 | viewModel = new ViewModelProvider(this).get(NoteViewModel.class);
81 | viewModel.getAll().observe(this, new Observer>() {
82 | @Override
83 | public void onChanged(List notes) {
84 | // Update your list;
85 | }
86 | });
87 | }
88 |
89 | public void addNote(View view) {
90 | Note note = new Note(title, content);
91 | viewModel.insert(note);
92 | }
93 | // ...
94 | }
95 | ```
96 | # Annotations
97 | All annotations can only be applied to a class annotated with room `@Entity`.
98 | The entity marked with `LivingRoom` annotations should also extend `BasicEntity`.
99 | The `BasicEntity` contains some basic fields like the `id`, timestamps (`created_at`, `updated_at`) and `isDeleted` fields.
100 |
101 | ## `@Crudable`
102 | At compile time, LivingRoom generates an implementation of CRUD operations in a `DAO` class,
103 | a Repository class and a ViewModel class as recommended by the [Android Architecture Component](https://developer.android.com/topic/libraries/architecture)
104 | guidelines.
105 | * `insert(item)`: inserts an object of type entity into the database and auto generates the `id` and `created_at` fields.
106 | * `delete(item)`: permanently deletes an item from the database.
107 | * `update(item)`: updates an item in the database, and updates the `updated_at` timestamps.
108 | * `archive(item)`: archives the item without deleting it, and sets `isDeleted` to true.
109 | * `getAll()`: retrieves all the non-archived items from the database; returns a [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) list.
110 | * `getById(long)`: gets an item using its unique `id` and returns a [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) object.
111 |
112 | ## `@Insertable`
113 | Use this annotation to generate an insert method for your entities.
114 | The method `insert` takes an object of the entity type and returns a long number representing the id of the inserted item.
115 | It also saves the current timestamp in `created_at`.
116 |
117 | ## `@Deletable`
118 | Use this annotation to generate a delete method for your entities.
119 | The method `delete` takes an object of the entity type and permanently deletes the item from the database.
120 |
121 | ## `@Updatable`
122 | Use this annotation to generate an update method for your entities.
123 | The method `update` takes an object of the entity type and updates it in the database.
124 | It also sets the `updated_at` field to the current timestamp.
125 |
126 | ## `@Archivable`
127 | Use this annotation to generate an archive method for your entities.
128 | The method `archive` takes an object of the entity type and soft-deletes it from the database.
129 | It only changes the flag `isDeleted` to true.
130 |
131 | ## `@SelectableAll`
132 | Use this annotation to generate a `getAll()` method for your entities.
133 | The method `getAll()` retrieves all the items of an entity that are not archived.
134 | It returns a [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) list.
135 |
136 | ## `@SelectableById`
137 | Use this annotation to generate `getById()` method for your entities.
138 | The method `getById()` takes a long parameter representing the id, and returns an item.
139 | It also returns a [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) object.
140 |
141 | ## `@SelectableWhere`
142 | Use this annotation to generate your own `SELECT` query.
143 | This annotation takes three parameters:
144 | * `methodName`: the name of the generated method in the components.
145 | * `where`: the `WHERE` clause in the select query. Other Statements, such as `ORDER BY` and `LIMIT`, can also be added.
146 | * `params`: the list of the parameters (Separated by comma) .
147 |
148 | Here is an example using this annotation.
149 | ```java
150 | @SelectableWhere(methodName = "getArchived", where = "isDeleted = 1")
151 | @SelectableWhere(methodName = "getDateRange",
152 | where = "created_at > :from AND created_at < :to",
153 | params = {"java.util.Date from", "java.util.Date to"})
154 | @Entity
155 | public class Note extends BasicEntity {
156 | private String title;
157 | private String content;
158 | //...
159 | }
160 | ```
161 | This generates `getArchived()` method that returns all the archived items.
162 | It also generates `getDateRange(from, to)` to select all notes in a date range.
163 |
164 | # TODOs
165 | * Add the database class.
166 | * Migrations in the database class.
167 | * Tests automation.
168 | * Let the user choose whether LiveData is returned or not.
169 | * Generic queries.
170 |
171 | # Issues
172 | Feel free to open [issues](https://github.com/msbelaid/LivingRoom/issues/new)
173 |
--------------------------------------------------------------------------------
/LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/LivingRoomProcessor.java:
--------------------------------------------------------------------------------
1 | package com.pentabin.livingroom.compiler;
2 |
3 | import androidx.room.Database;
4 | import androidx.room.Entity;
5 | import androidx.room.TypeConverters;
6 |
7 | import com.pentabin.livingroom.annotations.Archivable;
8 | import com.pentabin.livingroom.annotations.Crudable;
9 | import com.pentabin.livingroom.annotations.Deletable;
10 | import com.pentabin.livingroom.annotations.Insertable;
11 | import com.pentabin.livingroom.annotations.SelectableAll;
12 | import com.pentabin.livingroom.annotations.SelectableById;
13 | import com.pentabin.livingroom.annotations.SelectableWhere;
14 | import com.pentabin.livingroom.annotations.SelectableWheres;
15 | import com.pentabin.livingroom.annotations.Updatable;
16 | import com.squareup.javapoet.AnnotationSpec;
17 | import com.squareup.javapoet.ClassName;
18 | import com.squareup.javapoet.FieldSpec;
19 | import com.squareup.javapoet.JavaFile;
20 | import com.squareup.javapoet.MethodSpec;
21 | import com.squareup.javapoet.TypeName;
22 | import com.squareup.javapoet.TypeSpec;
23 |
24 | import java.io.IOException;
25 | import java.util.ArrayList;
26 | import java.util.Collection;
27 | import java.util.HashMap;
28 | import java.util.List;
29 | import java.util.Map;
30 | import java.util.Set;
31 |
32 | import javax.annotation.processing.AbstractProcessor;
33 | import javax.annotation.processing.Filer;
34 | import javax.annotation.processing.FilerException;
35 | import javax.annotation.processing.ProcessingEnvironment;
36 | import javax.annotation.processing.RoundEnvironment;
37 | import javax.annotation.processing.SupportedAnnotationTypes;
38 | import javax.annotation.processing.SupportedSourceVersion;
39 | import javax.lang.model.SourceVersion;
40 | import javax.lang.model.element.Element;
41 | import javax.lang.model.element.Modifier;
42 | import javax.lang.model.element.TypeElement;
43 | import javax.lang.model.type.DeclaredType;
44 | import javax.tools.Diagnostic;
45 |
46 | import static com.pentabin.livingroom.compiler.LivingroomMethod.GET_ALL;
47 | import static com.pentabin.livingroom.compiler.LivingroomMethod.GET_BY_ID;
48 | import static com.pentabin.livingroom.compiler.LivingroomMethod.INSERT;
49 | import static com.pentabin.livingroom.compiler.LivingroomMethod.selectWhereMethod;
50 |
51 | /**
52 | *
53 | */
54 | @SupportedAnnotationTypes(
55 | {
56 | "com.pentabin.livingroom.annotations.Crudable",
57 | "com.pentabin.livingroom.annotations.Insertable",
58 | "com.pentabin.livingroom.annotations.Deletable",
59 | "com.pentabin.livingroom.annotations.Updatable",
60 | "com.pentabin.livingroom.annotations.Archivable",
61 | "com.pentabin.livingroom.annotations.SelectableAll",
62 | "com.pentabin.livingroom.annotations.SelectableById",
63 | "com.pentabin.livingroom.annotations.SelectableWhere",
64 | "com.pentabin.livingroom.annotations.SelectableWheres",
65 | })
66 | @SupportedSourceVersion(SourceVersion.RELEASE_8)
67 | public class LivingRoomProcessor extends AbstractProcessor {
68 |
69 | private List entities;
70 | private HashMap entitiesList;
71 | private static String packageName; //TODO from the database class maybe?
72 | private static final String SUFFIX_DAO = "Dao"; // Todo remove
73 | static final String dbClassName = "LivingRoomDatabase";
74 |
75 |
76 | @Override
77 | public synchronized void init(ProcessingEnvironment processingEnvironment) {
78 | super.init(processingEnvironment);
79 | entities = new ArrayList<>();
80 | entitiesList = new HashMap<>();
81 | }
82 |
83 | public LivingRoomProcessor(){}
84 |
85 | @Override
86 | public boolean process(Set extends TypeElement> set, RoundEnvironment env) {
87 | parseCrudable(env);
88 | parseInsertable(env);
89 | parseDeletable(env);
90 | parseUpdatable(env);
91 | parseArchivable(env);
92 | parseSelectable(env);
93 | parseSelectables(env);
94 | parseSelectableAll(env);
95 | parseSelectableById(env);
96 |
97 | try {
98 | generateClasses();
99 | } catch (IOException e) {
100 | e.printStackTrace();
101 | }
102 | return false;
103 | }
104 |
105 | private void parseAnnotation(Collection extends Element> elements, String method) {
106 | for (Element e: elements ) {
107 | checkIfAnnotatedWithEntity(e);
108 | checkIfExtendsBasicEntity(e);
109 | if (entitiesList.containsKey(e)) {
110 | EntityClass entityClass = entitiesList.get(e);
111 | entitiesList.get(e).addMethod(LivingroomMethod.of(entityClass, method));
112 | }
113 | else {
114 | EntityClass entityClass = new EntityClass((TypeElement)e);
115 | entityClass.addMethod(LivingroomMethod.of(entityClass, method));
116 | entitiesList.put((TypeElement) e, entityClass);
117 | }
118 | }
119 | }
120 |
121 | private void generateClasses() throws IOException {
122 | for (Map.Entry e: entitiesList.entrySet()) {
123 | generateCodeForEntity(e.getValue());
124 | entities.add(e.getValue().getTypeName());
125 | }
126 |
127 | try {
128 | generateDatabaseClass();
129 | } catch (FilerException e){
130 |
131 | }
132 | }
133 |
134 | private void parseCrudable(RoundEnvironment env) {
135 | Collection extends Element> elements =
136 | env.getElementsAnnotatedWith(Crudable.class);
137 | for (Element e: elements ) {
138 | if (entitiesList.containsKey(e)) {
139 | EntityClass entityClass = entitiesList.get(e);
140 | entitiesList.get(e).addMethods(LivingroomMethod.crud(entityClass));
141 | }
142 | else {
143 | EntityClass entityClass = new EntityClass((TypeElement)e);
144 | entityClass.addMethods(LivingroomMethod.crud(entityClass));
145 | entitiesList.put((TypeElement) e, entityClass);
146 | }
147 | }
148 | }
149 |
150 | private void parseInsertable(RoundEnvironment env) {
151 | Collection extends Element> insertableElements =
152 | env.getElementsAnnotatedWith(Insertable.class);
153 | parseAnnotation(insertableElements, INSERT);
154 | }
155 |
156 | private void parseDeletable(RoundEnvironment env) {
157 | Collection extends Element> deletableElements =
158 | env.getElementsAnnotatedWith(Deletable.class);
159 | parseAnnotation(deletableElements, LivingroomMethod.DELETE);
160 | }
161 |
162 | private void parseUpdatable(RoundEnvironment env) {
163 | Collection extends Element> updatableElements =
164 | env.getElementsAnnotatedWith(Updatable.class);
165 | parseAnnotation(updatableElements, LivingroomMethod.UPDATE);
166 | }
167 |
168 |
169 | private void parseArchivable(RoundEnvironment env) {
170 | Collection extends Element> archivableElements =
171 | env.getElementsAnnotatedWith(Archivable.class);
172 | parseAnnotation(archivableElements, LivingroomMethod.SOFT_DELETE);
173 | }
174 |
175 | private void parseSelectableAll(RoundEnvironment env) {
176 | Collection extends Element> archivableElements =
177 | env.getElementsAnnotatedWith(SelectableAll.class);
178 | parseAnnotation(archivableElements, GET_ALL);
179 | }
180 |
181 | private void parseSelectableById(RoundEnvironment env) {
182 | Collection extends Element> archivableElements =
183 | env.getElementsAnnotatedWith(SelectableById.class);
184 | parseAnnotation(archivableElements, GET_BY_ID);
185 | }
186 |
187 | private void parseSelectable(RoundEnvironment env) { // TODO refactor
188 | Collection extends Element> elements =
189 | env.getElementsAnnotatedWith(SelectableWhere.class);
190 |
191 | for (Element e: elements ) {
192 | SelectableWhere a = e.getAnnotation(SelectableWhere.class);
193 | if (entitiesList.containsKey(e)) {
194 | entitiesList.get(e).addMethod(
195 | selectWhereMethod(entitiesList.get(e),
196 | a.methodName(),
197 | a.where(),
198 | a.params(), true));
199 | } else {
200 | EntityClass entityClass = new EntityClass((TypeElement) e);
201 | entityClass.addMethod(
202 | selectWhereMethod(entitiesList.get(e),
203 | a.methodName(),
204 | a.where(),
205 | a.params(), true));
206 | entitiesList.put((TypeElement) e, entityClass);
207 | }
208 | }
209 | }
210 |
211 | private void parseSelectables(RoundEnvironment env) { // TODO refactor
212 | Collection extends Element> elements =
213 | env.getElementsAnnotatedWith(SelectableWheres.class);
214 |
215 | for (Element e: elements ) {
216 | for (SelectableWhere a: e.getAnnotation(SelectableWheres.class).value() ) {
217 |
218 | if (entitiesList.containsKey(e)) {
219 | entitiesList.get(e).addMethod(
220 | selectWhereMethod(entitiesList.get(e),
221 | a.methodName(),
222 | a.where(),
223 | a.params(), true));
224 | } else {
225 | EntityClass entityClass = new EntityClass((TypeElement) e);
226 | entityClass.addMethod(
227 | selectWhereMethod(entitiesList.get(e),
228 | a.methodName(),
229 | a.where(),
230 | a.params(), true));
231 | entitiesList.put((TypeElement) e, entityClass);
232 | }
233 | }
234 | }
235 | }
236 |
237 | private void generateCodeForEntity(EntityClass clazz) throws IOException {
238 | String path = clazz.getTypeElement().getQualifiedName().toString();
239 | if (packageName == null) { // TODO get out package from here (only for the database)
240 | int lastDot = path.lastIndexOf('.');
241 | if (lastDot > 0) {
242 | packageName = path.substring(0, lastDot);
243 | }
244 | }
245 | try {
246 | generateDaoClass(clazz);
247 | generateRepositoryClass(clazz);
248 | generateViewModelClass(clazz);
249 | } catch (FilerException e) {
250 |
251 | }
252 |
253 | }
254 |
255 | private void checkIfExtendsBasicEntity(Element annotatedElement){
256 | TypeElement superClassTypeElement =
257 | (TypeElement)((DeclaredType)((TypeElement)annotatedElement).getSuperclass()).asElement();
258 | if (!superClassTypeElement.getSimpleName().toString().equals("BasicEntity"))
259 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Your entity class must inherit from BasicEntity in order to use LivingRoom annotations", annotatedElement);
260 |
261 | }
262 | private void checkIfAnnotatedWithEntity(Element annotatedElement){
263 | if (annotatedElement.getAnnotation(Entity.class) == null)
264 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Please consider marking your class as a room @Entity", annotatedElement);
265 |
266 | }
267 |
268 | private void generateDaoClass(EntityClass clazz) throws IOException {
269 | JavaFile javaFile = JavaFile.builder(packageName, clazz.generateDaoClass()).build();
270 | Filer filer = processingEnv.getFiler();
271 | //javaFile.writeTo(System.out);
272 | javaFile.writeTo(filer);
273 | }
274 |
275 | private void generateViewModelClass(EntityClass clazz) throws IOException {
276 | JavaFile javaFile = JavaFile.builder(packageName, clazz.generateViewModelClass()).build();
277 | Filer filer = processingEnv.getFiler();
278 | //javaFile.writeTo(System.out);
279 | javaFile.writeTo(filer);
280 | }
281 |
282 | private void generateRepositoryClass(EntityClass clazz) throws IOException {
283 | JavaFile javaFile = JavaFile.builder(packageName, clazz.generateRepositoryClass()).build();
284 | Filer filer = processingEnv.getFiler();
285 | //javaFile.writeTo(System.out);
286 | javaFile.writeTo(filer);
287 | }
288 |
289 | private void generateDatabaseClass() throws IOException {
290 | final String instanceName = "INSTANCE";
291 | final String dataBaseName = "custom_database";
292 | StringBuilder listEntities = new StringBuilder("{");
293 | List listDaoMethods = new ArrayList<>();
294 |
295 | for (TypeName entity: entities) {
296 | int lastDot = entity.toString().lastIndexOf('.');
297 | String entityClassName = entity.toString().substring(lastDot + 1);
298 | listEntities.append(entityClassName).append(".class, ");
299 | listDaoMethods.add(
300 | MethodSpec.methodBuilder(
301 | (entityClassName+ SUFFIX_DAO).toLowerCase())
302 | .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
303 | .returns(ClassName.get(packageName, entityClassName+ SUFFIX_DAO))
304 | .build());
305 | }
306 |
307 | FieldSpec instance = FieldSpec.builder(ClassName.get(packageName, dbClassName), instanceName)
308 | .addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.VOLATILE).build();
309 | listEntities.append("}");
310 |
311 | MethodSpec getDatabase = MethodSpec.methodBuilder("getDatabase")
312 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
313 | .addParameter(ClassName.get("android.content",
314 | "Context"),
315 | "context",
316 | Modifier.FINAL)
317 | .addCode("if ($N == null) {\n" +
318 | " synchronized ($T.class) {\n" +
319 | " if ($N == null) {\n" +
320 | " $N = androidx.room.Room.databaseBuilder(context.getApplicationContext(),\n" +
321 | " $T.class, \""+dataBaseName+"\")\n" +
322 | " .fallbackToDestructiveMigration()\n" +
323 | " .build();\n" +
324 | " }\n" +
325 | " }\n" +
326 | "}\n" +
327 | "return $N;",
328 | instanceName, ClassName.get(packageName, dbClassName) , instanceName, instanceName, ClassName.get(packageName, dbClassName), instanceName)
329 | .returns(ClassName.get(packageName, dbClassName))
330 | .build();
331 |
332 | TypeSpec dbClass = TypeSpec.classBuilder(dbClassName)
333 | .superclass(ClassName.get("androidx.room", "RoomDatabase"))
334 | .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
335 | .addAnnotation(AnnotationSpec.builder(Database.class)
336 | .addMember("entities", listEntities.toString())
337 | .addMember("version", "1")
338 | .addMember("exportSchema", "false")
339 | .build())
340 | .addAnnotation(AnnotationSpec.builder(TypeConverters.class)
341 | .addMember("value", "$T.class", ClassName.get("com.pentabin.livingroom", "DateConverter") )
342 | .build())
343 | .addField(instance)
344 | .addMethods(listDaoMethods)
345 | .addMethod(getDatabase)
346 | .build();
347 |
348 | Filer filer = processingEnv.getFiler();
349 | JavaFile javaFile = JavaFile.builder(packageName, dbClass)
350 | .build();
351 | javaFile.writeTo(filer);
352 | }
353 |
354 | }
355 |
--------------------------------------------------------------------------------