Skip to content

Commit

Permalink
Merge pull request #11 from zeoflow/activity-fragment
Browse files Browse the repository at this point in the history
Lifecycle Activity-Fragment
  • Loading branch information
teogor authored Apr 15, 2021
2 parents c95b1d0 + cef034b commit 0b4720e
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 49 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}

dependencies {
Expand Down
38 changes: 32 additions & 6 deletions app/src/main/java/com/zeoflow/eyejet/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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",
Expand Down

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/java/com/zeoflow/eyejet/demo/MainFragment.java
Original file line number Diff line number Diff line change
@@ -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> 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<User>()
{
@SuppressLint("SetTextI18n")
@Override
public void onChanged(User user)
{
if (user == null)
{
return;
}
binding.txtV.setText("txt: " + user.id + " > " + user.firstName);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/zeoflow/eyejet/demo/ThirdActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/zeoflow/eyejet/demo/UserScope.java

This file was deleted.

9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
android:text="Open Activity"
android:textColor="#ffffff"/>

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"/>

</LinearLayout>
17 changes: 17 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#212121">

<TextView
android:id="@+id/txtV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_marginTop="10dp"/>

</LinearLayout>
2 changes: 1 addition & 1 deletion eyejet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
8 changes: 5 additions & 3 deletions eyejet/src/main/java/com/zeoflow/eyejet/Eyejet.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static <T extends LifecycleOwner> void shareLifecycle(Object scopeOwner,
if (shareProperty.shareType()[0] == ShareType.ON_FIRST_USE)
{
store.clearField(annotation, value);
System.out.println("heerWeAre");
}
} else
{
Expand Down Expand Up @@ -112,8 +111,7 @@ public static <T extends LifecycleOwner> void shareLifecycle(Object scopeOwner,
if (!store.checkContainsEyejetLifecycleObserver(
annotation,
lifecycleOwner.toString()
)
)
))
{
lifecycleOwner.getLifecycle().addObserver(observer);
store.getLifecycleObserverStack(annotation).push(observer);
Expand All @@ -129,6 +127,10 @@ public static <T extends LifecycleOwner> 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()))
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b4720e

Please sign in to comment.