diff --git a/app/build.gradle b/app/build.gradle index 3281efd..2381323 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,9 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + buildFeatures { + viewBinding true + } } dependencies { diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/MainActivity.java b/app/src/main/java/com/zeoflow/eyejet/demo/MainActivity.java index 412bacf..d78368f 100644 --- a/app/src/main/java/com/zeoflow/eyejet/demo/MainActivity.java +++ b/app/src/main/java/com/zeoflow/eyejet/demo/MainActivity.java @@ -2,30 +2,56 @@ import android.annotation.SuppressLint; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.view.View; import android.widget.TextView; import androidx.annotation.Nullable; import com.zeoflow.app.Activity; +import com.zeoflow.app.Fragment; +import com.zeoflow.eyejet.Eyejet; +import com.zeoflow.eyejet.EyejetField; +import com.zeoflow.eyejet.annotation.EyejetScope; +import com.zeoflow.eyejet.annotation.ShareProperty; +import com.zeoflow.eyejet.demo.databinding.ActivityMainBinding; import java.util.Random; +import java.util.Timer; +@EyejetScope public class MainActivity extends Activity { - private final MainActivityRepository repository = new MainActivityRepository(this); + @ShareProperty("user") + public static EyejetField user = new EyejetField<>(); + + private ActivityMainBinding binding; @SuppressLint("SetTextI18n") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - TextView txtV = findViewById(R.id.txtV); - findViewById(R.id.button).setOnClickListener(v -> configureNewActivity(SecondActivity.class).start()); - repository.user.observe(this, obj -> txtV.setText("txt: " + obj.id + " > " + obj.firstName)); - repository.user.setValue( + Eyejet.shareLifecycle( + this, + this + ); + + // Set the layout for the main activity + binding = ActivityMainBinding.inflate(getLayoutInflater()); + View view = binding.getRoot(); + setContentView(view); + + MainFragment fragment = new MainFragment(); + getSupportFragmentManager().beginTransaction().replace(binding.container.getId(), fragment).commit(); + + binding.button.setOnClickListener(v -> configureNewActivity(SecondActivity.class).start()); + user.observe(this, obj -> binding.txtV.setText("txt: " + obj.id + " > " + obj.firstName)); + + user.setValue( User.create( new Random().nextInt(99999999), "Teodor", diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/MainActivityRepository.java b/app/src/main/java/com/zeoflow/eyejet/demo/MainActivityRepository.java deleted file mode 100644 index d5463ff..0000000 --- a/app/src/main/java/com/zeoflow/eyejet/demo/MainActivityRepository.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.zeoflow.eyejet.demo; - -import androidx.lifecycle.LifecycleOwner; - -import com.zeoflow.eyejet.Eyejet; -import com.zeoflow.eyejet.EyejetField; -import com.zeoflow.eyejet.annotation.ShareProperty; - -@UserScope // custom scope -public class MainActivityRepository -{ - - @ShareProperty("user") - public EyejetField user = new EyejetField<>(); - - public MainActivityRepository(LifecycleOwner lifecycleOwner) - { - Eyejet.shareLifecycle( - this, - lifecycleOwner - ); - } -} diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/MainFragment.java b/app/src/main/java/com/zeoflow/eyejet/demo/MainFragment.java new file mode 100644 index 0000000..899d43d --- /dev/null +++ b/app/src/main/java/com/zeoflow/eyejet/demo/MainFragment.java @@ -0,0 +1,62 @@ +package com.zeoflow.eyejet.demo; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.lifecycle.Observer; + +import com.zeoflow.app.Fragment; +import com.zeoflow.eyejet.Eyejet; +import com.zeoflow.eyejet.EyejetField; +import com.zeoflow.eyejet.annotation.EyejetScope; +import com.zeoflow.eyejet.annotation.ShareProperty; +import com.zeoflow.eyejet.demo.databinding.FragmentMainBinding; + +@EyejetScope +public class MainFragment extends Fragment +{ + + @ShareProperty("user") + public static EyejetField user = new EyejetField<>(); + private FragmentMainBinding binding; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) + { + binding = FragmentMainBinding.inflate(inflater, container, false); + View view = binding.getRoot(); + return view; + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + Eyejet.shareLifecycle( + this, + this + ); + + user.observe(this, new Observer() + { + @SuppressLint("SetTextI18n") + @Override + public void onChanged(User user) + { + if (user == null) + { + return; + } + binding.txtV.setText("txt: " + user.id + " > " + user.firstName); + } + }); + } + +} diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/SecondActivity.java b/app/src/main/java/com/zeoflow/eyejet/demo/SecondActivity.java index 2531e23..07b9f45 100644 --- a/app/src/main/java/com/zeoflow/eyejet/demo/SecondActivity.java +++ b/app/src/main/java/com/zeoflow/eyejet/demo/SecondActivity.java @@ -9,12 +9,13 @@ import com.zeoflow.app.Activity; import com.zeoflow.eyejet.Eyejet; import com.zeoflow.eyejet.EyejetField; +import com.zeoflow.eyejet.annotation.EyejetScope; import com.zeoflow.eyejet.annotation.ShareProperty; import com.zeoflow.eyejet.annotation.ShareType; import java.util.Random; -@UserScope +@EyejetScope public class SecondActivity extends Activity { diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/ThirdActivity.java b/app/src/main/java/com/zeoflow/eyejet/demo/ThirdActivity.java index 30b97cd..9ba9632 100644 --- a/app/src/main/java/com/zeoflow/eyejet/demo/ThirdActivity.java +++ b/app/src/main/java/com/zeoflow/eyejet/demo/ThirdActivity.java @@ -9,10 +9,11 @@ import com.zeoflow.app.Activity; import com.zeoflow.eyejet.Eyejet; import com.zeoflow.eyejet.EyejetField; +import com.zeoflow.eyejet.annotation.EyejetScope; import com.zeoflow.eyejet.annotation.ShareProperty; import com.zeoflow.eyejet.annotation.ShareType; -@UserScope +@EyejetScope public class ThirdActivity extends Activity { diff --git a/app/src/main/java/com/zeoflow/eyejet/demo/UserScope.java b/app/src/main/java/com/zeoflow/eyejet/demo/UserScope.java deleted file mode 100644 index 312b97e..0000000 --- a/app/src/main/java/com/zeoflow/eyejet/demo/UserScope.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.zeoflow.eyejet.demo; - -import com.zeoflow.eyejet.annotation.EyejetScope; - -import java.lang.annotation.Retention; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@EyejetScope -@Retention(RUNTIME) -public @interface UserScope -{ -} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 62b2504..eb7a884 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -23,4 +23,13 @@ android:text="Open Activity" android:textColor="#ffffff"/> + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml new file mode 100644 index 0000000..1102cbc --- /dev/null +++ b/app/src/main/res/layout/fragment_main.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/eyejet/build.gradle b/eyejet/build.gradle index 5145c55..cfe4345 100644 --- a/eyejet/build.gradle +++ b/eyejet/build.gradle @@ -25,7 +25,7 @@ dependencies { api project(":eyejet-annotation") // Dependencies from maven -// def eyejet_version = "1.0.2" +// def eyejet_version = "1.0.3" // implementation("com.zeoflow:eyejet-arch:$eyejet_version") // api("com.zeoflow:eyejet-annotation:$eyejet_version") diff --git a/eyejet/src/main/java/com/zeoflow/eyejet/Eyejet.java b/eyejet/src/main/java/com/zeoflow/eyejet/Eyejet.java index 234f24b..748753e 100644 --- a/eyejet/src/main/java/com/zeoflow/eyejet/Eyejet.java +++ b/eyejet/src/main/java/com/zeoflow/eyejet/Eyejet.java @@ -83,7 +83,6 @@ public static void shareLifecycle(Object scopeOwner, if (shareProperty.shareType()[0] == ShareType.ON_FIRST_USE) { store.clearField(annotation, value); - System.out.println("heerWeAre"); } } else { @@ -112,8 +111,7 @@ public static void shareLifecycle(Object scopeOwner, if (!store.checkContainsEyejetLifecycleObserver( annotation, lifecycleOwner.toString() - ) - ) + )) { lifecycleOwner.getLifecycle().addObserver(observer); store.getLifecycleObserverStack(annotation).push(observer); @@ -129,6 +127,10 @@ public static void shareLifecycle(Object scopeOwner, */ public static boolean checkAnnotatedEyejetScope(Annotation annotation) { + if (annotation.annotationType().getName().equals(EyejetScope.class.getName())) + { + return true; + } for (Annotation s : annotation.annotationType().getAnnotations()) { if (s.annotationType().getName().equals(EyejetScope.class.getName())) diff --git a/gradle.properties b/gradle.properties index ffd3094..c0ef0da 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,7 +23,7 @@ kotlin.code.style=official # Maven POM_NAME=Eyejet POM_PACKAGING=aar -VERSION_NAME=1.0.2 +VERSION_NAME=1.0.3 GROUP=com.zeoflow POM_DESCRIPTION=Lifecycle-aware shared observable data holder class for android. POM_URL=https://github.com/zeoflow/eyejet