Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

[Draft] Composition #109

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions build.gradle
Expand Up @@ -13,6 +13,7 @@ buildscript {
allprojects {
repositories {
jcenter()
maven { url "https://maven.google.com" }
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
Expand All @@ -24,11 +25,12 @@ ext {
VERSION_NAME = "0.8.1-SNAPSHOT"
VERSION_CODE = 6
MIN_SDK_VERSION = 14
TARGET_SDK_VERSION = 25
COMPILE_SDK_VERSION = 25
TARGET_SDK_VERSION = 26
COMPILE_SDK_VERSION = 26
BUILD_TOOLS_VERSION = '25.0.2'

supportLibraryVersion = '25.2.0'
//TODO wait for stable 26.0.0 release
supportLibraryVersion = '26.0.0-beta2'

bintrayVersion = '0.3.4'
junitVersion = '4.12'
Expand Down
Expand Up @@ -18,34 +18,31 @@

import com.jakewharton.rxbinding.view.RxView;

import net.grandcentrix.thirtyinch.TiActivity;
import net.grandcentrix.thirtyinch.TiPresenterBinder;
import net.grandcentrix.thirtyinch.TiPresenterBinders;
import net.grandcentrix.thirtyinch.logginginterceptor.LoggingInterceptor;
import net.grandcentrix.thirtyinch.sample.fragmentlifecycle.FragmentLifecycleActivity;
import net.grandcentrix.thirtyinch.sample.fragmentlifecycle.viewpager.LifecycleViewPagerActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import rx.Observable;

public class HelloWorldActivity extends TiActivity<HelloWorldPresenter, HelloWorldView>
implements HelloWorldView {
public class HelloWorldActivity extends AppCompatActivity implements HelloWorldView {

private Button mButton;

private TextView mOutput;

private TextView mUptime;
private HelloWorldPresenter mPresenter;

public HelloWorldActivity() {
addBindViewInterceptor(new LoggingInterceptor());
}
private TextView mUptime;

@Override
public Observable<Void> onButtonClicked() {
Expand All @@ -71,12 +68,6 @@ public boolean onOptionsItemSelected(final MenuItem item) {
return false;
}

@NonNull
@Override
public HelloWorldPresenter providePresenter() {
return new HelloWorldPresenter();
}

@Override
public void showPresenterUpTime(final Long uptime) {
mUptime.setText(String.format("Presenter alive for %ss", uptime));
Expand All @@ -90,24 +81,24 @@ public void showText(final String text) {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TiPresenterBinder<HelloWorldPresenter, HelloWorldView> binder = TiPresenterBinders
.attachPresenter(this, savedInstanceState, () -> new HelloWorldPresenter());
binder.addBindViewInterceptor(new LoggingInterceptor());
mPresenter = binder.getPresenter();

setContentView(R.layout.activity_hello_world);

mButton = (Button) findViewById(R.id.button);
mOutput = (TextView) findViewById(R.id.output);
mUptime = (TextView) findViewById(R.id.uptime);
mButton = findViewById(R.id.button);
mOutput = findViewById(R.id.output);
mUptime = findViewById(R.id.uptime);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SampleFragment())
.commit();
}

findViewById(R.id.recreate).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
recreate();
}
});
findViewById(R.id.recreate).setOnClickListener(v -> recreate());
}

}
Expand Up @@ -15,20 +15,31 @@

package net.grandcentrix.thirtyinch.sample;

import net.grandcentrix.thirtyinch.TiFragment;
import net.grandcentrix.thirtyinch.TiPresenterBinders;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SampleFragment extends TiFragment<SamplePresenter, SampleView> implements SampleView {
public class SampleFragment extends Fragment implements SampleView {


private SamplePresenter mPresenter;

private TextView mSampleText;

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mPresenter = TiPresenterBinders.attachPresenter(this, savedInstanceState,
() -> new SamplePresenter()).getPresenter();
}

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container,
Expand All @@ -38,16 +49,10 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
final ViewGroup view = (ViewGroup) inflater
.inflate(R.layout.fragment_sample, container, false);

mSampleText = (TextView) view.findViewById(R.id.sample_text);
mSampleText = view.findViewById(R.id.sample_text);
return view;
}

@NonNull
@Override
public SamplePresenter providePresenter() {
return new SamplePresenter();
}

@Override
public void showText(final String s) {
mSampleText.setText(s);
Expand Down
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2017 grandcentrix GmbH
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.grandcentrix.thirtyinch;


import net.grandcentrix.thirtyinch.internal.InterceptableViewBinder;
import net.grandcentrix.thirtyinch.internal.PresenterAccessor;

//TODO I'm not happy with the binder name
public interface TiPresenterBinder<P extends TiPresenter<V>, V extends TiView>
extends PresenterAccessor<P, V>, InterceptableViewBinder<V> {

}
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2017 grandcentrix GmbH
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.grandcentrix.thirtyinch;


import net.grandcentrix.thirtyinch.internal.ActivityPresenterBinder;
import net.grandcentrix.thirtyinch.internal.FragmentPresenterBinder;
import net.grandcentrix.thirtyinch.internal.PresenterAccessor;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class TiPresenterBinders {

/**
* must be called in {@link Activity#onCreate(Bundle)}
*/
public static <P extends TiPresenter<V>, V extends TiView> TiPresenterBinder<P, V> attachPresenter(
@NonNull final Activity activity,
@Nullable final Bundle savedInstanceState,
@NonNull final TiPresenterProvider<P> presenterProvider) {

return attachPresenter(activity, savedInstanceState, presenterProvider, null);
}

/**
* must be called in {@link Activity#onCreate(Bundle)}
*/
public static <P extends TiPresenter<V>, V extends TiView> TiPresenterBinder<P, V> attachPresenter(
@NonNull final Activity activity,
@Nullable final Bundle savedInstanceState,
@NonNull final TiPresenterProvider<P> presenterProvider,
@Nullable final TiViewProvider<V> viewProvider) {

if (activity instanceof TiActivity) {
throw new IllegalStateException(
"Can't attach a TiPresenter to a TiActivity which already has a TiPresenter");
}

final ActivityPresenterBinder<P, V> binder = new ActivityPresenterBinder<>(activity,
savedInstanceState, presenterProvider, viewProvider);

Application app = activity.getApplication();
app.registerActivityLifecycleCallbacks(binder);

return binder;
}

/**
* must be called in {@link Fragment#onCreate(Bundle)}
*/
public static <P extends TiPresenter<V>, V extends TiView> PresenterAccessor<P, V> attachPresenter(
@NonNull final Fragment fragment,
@Nullable final Bundle savedInstanceState,
@NonNull final TiPresenterProvider<P> presenterProvider,
@Nullable final TiViewProvider<V> viewProvider) {

if (fragment instanceof TiFragment) {
throw new IllegalStateException(
"Can't attach a TiPresenter to a TiFragment which already has a TiPresenter");
}

final FragmentPresenterBinder<P, V> binder = new FragmentPresenterBinder<>(fragment,
savedInstanceState, presenterProvider, viewProvider);

FragmentManager fragmentManager = fragment.getActivity().getSupportFragmentManager();
fragmentManager.registerFragmentLifecycleCallbacks(binder, false);

return binder;
}

/**
* must be called in {@link Fragment#onCreate(Bundle)}
*/
public static <P extends TiPresenter<V>, V extends TiView> PresenterAccessor<P, V> attachPresenter(
@NonNull final Fragment fragment,
@Nullable final Bundle savedInstanceState,
@NonNull final TiPresenterProvider<P> presenterProvider) {

return attachPresenter(fragment, savedInstanceState, presenterProvider, null);
}
}