Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from MRezaNasirloo/feature/infinite_instance
Browse files Browse the repository at this point in the history
Feature/infinite instance
  • Loading branch information
MRezaNasirloo authored Apr 14, 2018
2 parents cd145b6 + 56d763e commit e8d7cc5
Show file tree
Hide file tree
Showing 72 changed files with 1,055 additions and 373 deletions.
18 changes: 18 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ Packages are available in `jcenter`

```
// Base features
implementation 'com.mrezanasirloo:slick:1.0.5'
implementation 'com.mrezanasirloo:slick:1.1.0'

// Reactive features
implementation 'com.mrezanasirloo:slick-reactive:1.0.5'
implementation 'com.mrezanasirloo:slick-reactive:1.1.0'

implementation 'com.mrezanasirloo:slick-conductor:1.0.5'
implementation 'com.mrezanasirloo:slick-support-fragment:1.0.5'
implementation 'com.mrezanasirloo:slick-conductor:1.1.0'
implementation 'com.mrezanasirloo:slick-support-fragment:1.1.0'

annotationProcessor 'com.mrezanasirloo:slick-compiler:1.0.5'
annotationProcessor 'com.mrezanasirloo:slick-compiler:1.1.0'
```

Since Slick packages are not tied to a specific dependency you need to provide them.
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.novoda:bintray-release:0.8.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$Versions.vKotlin"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object Versions {
const val vVersionCode = 1
const val vVersionName = "1.0"
const val vBuildTool = "27.0.3"
const val vKotlin = "1.2.31"

const val vSupportLib = "27.1.1"
const val vRxjava2 = "2.1.10"
Expand All @@ -39,6 +40,8 @@ object Deps {
val depSupportCardView = "com.android.support:cardview-v7:${Versions.vSupportLib}"
val depConstraintLayout = "com.android.support.constraint:constraint-layout:1.0.2"

val depKotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.vKotlin}"

val depConductor = "com.bluelinelabs:conductor:2.1.4"
val depSparkButton = "com.github.varunest:sparkbutton:1.0.5"

Expand Down
16 changes: 10 additions & 6 deletions docs/CustomViews.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
### CustomViews Instruction

Since the View class does not have appropriate lifecycle callbacks,
Slick provides some delegates which should called manually.
Slick provides some delegates which should be called manually.

```java
public class CustomView extends LinearLayout implements ExampleView, OnDestroyListener {
public class CustomView extends LinearLayout implements ExampleView, SlickLifecycleListener {

@Presenter
ViewPresenter presenter;
Expand All @@ -16,7 +16,6 @@ public class CustomView extends LinearLayout implements ExampleView, OnDestroyLi
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ViewPresenter_Slick.bind(this);
ViewPresenter_Slick.onAttach(this);
}

Expand All @@ -27,8 +26,8 @@ public class CustomView extends LinearLayout implements ExampleView, OnDestroyLi
}

@Override
public void onDestroy() {
ViewPresenter_Slick.onDestroy(this);
public void onBind() {
ViewPresenter_Slick.bind(this);
}
}
```
Expand All @@ -50,15 +49,20 @@ public class CustomViewActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_view);
customView = (CustomView) findViewById(R.id.custom_view);
customView.onBind("some_random_id");// <--- Sending a unique id insures you don't lose your presenter if you have mutiple instance of your view
}

@Override
protected void onDestroy() {
super.onDestroy();
customView.onDestroy(); //<--- onDestroy notification should be passed to view
ViewPresenter_Slick.onDestroy(customView); //<--- onDestroy notification should be passed to the generated class
// It's possible to send the `onDestory` callbacks with the view's id if you don't have access to its instance anymore. (View hosted in Fragment)
// ViewPresenter_Slick.onDestroy("some_random_id", this);
}
}
```
However you may manually remove a view from its parent too, if you don't want to leak its presenter you should call its
`onDestroy` method.

For more samples take a look at the `sample-app` module

36 changes: 13 additions & 23 deletions docs/MultiInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
If you have multiple instance of a Fragment or a CustomView at the same time you need to implement `SlickUniqueId`
interface, Since there is no way to differentiate them from each other out of the box.
Here is its implementation.

#### Fragment:
```java
public abstract class SlickFragment extends Fragment implements SlickUniqueId {
Expand All @@ -29,42 +30,31 @@ public abstract class SlickFragment extends Fragment implements SlickUniqueId {
}
}
```
You can just copy & paste the above code to your base class and you are good to go.

#### CustomView:

Since the View's lifecylce is not rich enough, its parent should provide a unique id for it when it calls its `onBind(String)` method.
```java
public class CustomView extends AppCompatTextView implements SlickUniqueId {
public class CustomView extends AppCompatTextView implements SlickLifecycleListener, SlickUniqueId {

private String id;

public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/* ... */

@Nullable
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
bundle.putString(SLICK_UNIQUE_KEY, this.id);
return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
this.id = bundle.getString(SLICK_UNIQUE_KEY);
state = bundle.getParcelable("superState");
}
super.onRestoreInstanceState(state);
public void onBind(@NonNull String instanceId) {
this.id = instanceId;
ViewPresenter_Slick.bind(this);
}

@Override
public String getUniqueId() {
return id = (id != null ? id : UUID.randomUUID().toString());
return id;
}
}
```
You can just copy & paste them to your base class and you are good to go.

In Slick the Activity and Conductor Controller classes have this feature out of the box, There is no need for any of
this for them. Because of Android API limitation it wasn't possible to do this for Fragment and View classes.

For an advance sample take a look at the sample-app module. package: sample -> customeview -> infinite
11 changes: 9 additions & 2 deletions sample-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion Versions.vSdkCompile
Expand All @@ -41,11 +44,15 @@ dependencies {
implementation project(':slick')
implementation project(':slick-conductor')
implementation project(':slick-support-fragment')
annotationProcessor project(':slick-compiler')
annotationProcessor Deps.depDaggerCompiler
kapt project(':slick-compiler')
kapt Deps.depDaggerCompiler

implementation Deps.depSupportAppCompat
implementation Deps.depSupportFragment
implementation Deps.depConductor
implementation Deps.depDagger
implementation Deps.depKotlin
}
repositories {
mavenCentral()
}
1 change: 1 addition & 0 deletions sample-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<activity android:name=".fragment.ActivityFragmentHost"/>
<activity android:name=".fragmentsupport.SupportFragmentHostActivity"/>
<activity android:name=".cutstomview.ActivityCustomView"/>
<activity android:name=".cutstomview.infinate.ActivityFragmentContainer"/>
<activity android:name=".cutstomview.dagger.ActivityCustomViewDagger"/>
<activity android:name=".multipresenter.ActivityMultiPresenter"/>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.mrezanasirloo.slick.sample.conductor.dagger.ActivityConductorHostDagger;
import com.mrezanasirloo.slick.sample.cutstomview.ActivityCustomView;
import com.mrezanasirloo.slick.sample.cutstomview.dagger.ActivityCustomViewDagger;
import com.mrezanasirloo.slick.sample.cutstomview.infinate.ActivityFragmentContainer;
import com.mrezanasirloo.slick.sample.fragment.ActivityFragmentHost;
import com.mrezanasirloo.slick.sample.fragmentsupport.SupportFragmentHostActivity;
import com.mrezanasirloo.slick.sample.multipresenter.ActivityMultiPresenter;
Expand Down Expand Up @@ -97,5 +98,13 @@ public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityMultiPresenter.class));
}
});

findViewById(R.id.button_custom_view_fragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityFragmentContainer.class));
}
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ActivitySimple extends AppCompatActivity implements ViewSimple {

@Override
protected void onCreate(Bundle savedInstanceState) {
PresenterSimple_Slick.bind(this, R.id.textView3, "foo");
PresenterSimple_Slick.bind(this, R.id.textView_simple, "foo");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.mrezanasirloo.slick.sample.activity.dagger;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.mrezanasirloo.slick.Presenter;
import com.mrezanasirloo.slick.sample.App;
Expand All @@ -36,14 +38,18 @@ public class ActivitySimpleDagger extends AppCompatActivity implements ViewSimpl

private static final String TAG = ActivitySimpleDagger.class.getSimpleName();

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
App.getDaggerComponent(this).inject(this);
PresenterSimpleDagger_Slick.bind(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
TextView textView = findViewById(R.id.textView_simple);
textView.setText("Activity's Presenter has injected with Dagger");
}


@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ControllerSimple extends Controller implements ViewConductor {
@Override
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
PresenterConductor_Slick.bind(this);
return inflater.inflate(R.layout.home_layout, container, false);
return inflater.inflate(R.layout.contoller_home, container, false);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ControllerDagger extends Controller implements ViewConductorDagger
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
App.getDaggerComponent(getApplicationContext()).inject(this);
PresenterConductorDagger_Slick.bind(this);
return inflater.inflate(R.layout.home_layout, container, false);
return inflater.inflate(R.layout.contoller_home, container, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ public class ActivityCustomView extends AppCompatActivity {

private CustomView customView1;
private CustomView customView2;
private String view2UniqueId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_view);
customView1 = findViewById(R.id.custom_view_1);
customView2 = findViewById(R.id.custom_view_2);
customView1.onBind("some_unique_id_1");
customView2.onBind("some_unique_id_2");
view2UniqueId = customView2.getUniqueId();
}

@Override
protected void onDestroy() {
super.onDestroy();
customView1.onDestroy(); //<--- onDestroy notification should be passed to view
customView2.onDestroy();
// onDestroy callbacks should be passed to the generated class
ViewPresenter_Slick.onDestroy(customView1);
ViewPresenter_Slick.onDestroy(customView2);
// If you don't have access to the view anymore, i.e View's hosted in a fragment and the onDestroyView of the fragment has called
// Retain its uniqueId and send the onDestroy callbacks yourself
// ViewPresenter_Slick.onDestroy(view2UniqueId, this);
}
}
Loading

0 comments on commit e8d7cc5

Please sign in to comment.