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

Use the same logging interface everywhere #14

Merged
merged 7 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import net.grandcentrix.thirtyinch.internal.DelegatedTiActivity;
import net.grandcentrix.thirtyinch.internal.InterceptableViewBinder;
import net.grandcentrix.thirtyinch.internal.TiActivityDelegate;
import net.grandcentrix.thirtyinch.internal.TiPresenterLogger;
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
Expand All @@ -37,12 +37,11 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.List;

public class TiActivityPlugin<P extends TiPresenter<V>, V extends TiView> extends ActivityPlugin
implements TiViewProvider<V>, DelegatedTiActivity<P>, TiPresenterLogger,
implements TiViewProvider<V>, DelegatedTiActivity<P>, TiLoggingTagProvider,
InterceptableViewBinder<V> {

public static final String NCI_KEY_PRESENTER = "presenter";
Expand Down Expand Up @@ -93,6 +92,11 @@ public List<BindViewInterceptor> getInterceptors(
return mDelegate.getInterceptors(predicate);
}

@Override
public String getLoggingTag() {
return TAG;
}

public P getPresenter() {
return mDelegate.getPresenter();
}
Expand Down Expand Up @@ -127,11 +131,6 @@ public boolean isDontKeepActivitiesEnabled() {
return AndroidDeveloperOptions.isDontKeepActivitiesEnabled(getActivity());
}

@Override
public void logTiMessages(final String msg) {
Log.v(TAG, msg);
}

@Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package net.grandcentrix.thirtyinch.rx;

import net.grandcentrix.thirtyinch.TiLog;
import net.grandcentrix.thirtyinch.TiView;

import org.junit.After;
Expand Down Expand Up @@ -44,6 +45,13 @@ public class RxUtilsTest {

@Before
public void setUp() throws Exception {
TiLog.setLogger(new TiLog.Logger() {
@Override
public void log(final int level, final String tag, final String msg) {
// prevent RuntimeException: android.util.Log not mocked
}
});

mView = mock(TiView.class);

mPresenter = new TiMockPresenter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

package net.grandcentrix.thirtyinch.internal;

import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiConfiguration;
import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiView;

import org.junit.Before;
Expand Down Expand Up @@ -144,13 +144,13 @@ public void testRestorePresenter_withNonConfigurationInstance() throws Exception
}

@Test
public void testRestorePresenter_withSavior_whichIsDisabled() throws Exception {
public void testRestorePresenter_withSavior() throws Exception {
final TiPresenter firstPresenter = new TiPresenter(new TiConfiguration.Builder()
.setUseStaticSaviorToRetain(false)
.setUseStaticSaviorToRetain(true)
.build()) {
};
final TiPresenter secondPresenter = new TiPresenter(new TiConfiguration.Builder()
.setUseStaticSaviorToRetain(false)
.setUseStaticSaviorToRetain(true)
.build()) {
};
mPresenter = firstPresenter;
Expand All @@ -169,20 +169,20 @@ public void testRestorePresenter_withSavior_whichIsDisabled() throws Exception {

// check reuse of old presenter
mDelegate.onCreate_afterSuper(bundle);
assertEquals(secondPresenter, mDelegate.getPresenter());
assertEquals(firstPresenter, mDelegate.getPresenter());

// new one got created
assertEquals(TiPresenter.State.CREATED_WITH_DETACHED_VIEW, mPresenter.getState());
// new one got NOT created
assertEquals(TiPresenter.State.INITIALIZED, mPresenter.getState());
}

@Test
public void testRestorePresenter_withSavior() throws Exception {
public void testRestorePresenter_withSavior_whichIsDisabled() throws Exception {
final TiPresenter firstPresenter = new TiPresenter(new TiConfiguration.Builder()
.setUseStaticSaviorToRetain(true)
.setUseStaticSaviorToRetain(false)
.build()) {
};
final TiPresenter secondPresenter = new TiPresenter(new TiConfiguration.Builder()
.setUseStaticSaviorToRetain(true)
.setUseStaticSaviorToRetain(false)
.build()) {
};
mPresenter = firstPresenter;
Expand All @@ -201,10 +201,10 @@ public void testRestorePresenter_withSavior() throws Exception {

// check reuse of old presenter
mDelegate.onCreate_afterSuper(bundle);
assertEquals(firstPresenter, mDelegate.getPresenter());
assertEquals(secondPresenter, mDelegate.getPresenter());

// new one got NOT created
assertEquals(TiPresenter.State.INITIALIZED, mPresenter.getState());
// new one got created
assertEquals(TiPresenter.State.CREATED_WITH_DETACHED_VIEW, mPresenter.getState());
}

@NonNull
Expand Down Expand Up @@ -249,10 +249,10 @@ public TiPresenter<TiView> providePresenter() {
return mPresenter;
}
},
new TiPresenterLogger() {
new TiLoggingTagProvider() {
@Override
public void logTiMessages(final String msg) {
System.out.println(msg);
public String getLoggingTag() {
return "TestTag";
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import net.grandcentrix.thirtyinch.internal.InterceptableViewBinder;
import net.grandcentrix.thirtyinch.internal.PresenterNonConfigurationInstance;
import net.grandcentrix.thirtyinch.internal.TiActivityDelegate;
import net.grandcentrix.thirtyinch.internal.TiPresenterLogger;
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
Expand All @@ -30,7 +30,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.List;

Expand All @@ -40,7 +39,7 @@
public abstract class TiActivity<P extends TiPresenter<V>, V extends TiView>
extends AppCompatActivity
implements TiPresenterProvider<P>, TiViewProvider<V>, DelegatedTiActivity<P>,
TiPresenterLogger, InterceptableViewBinder<V> {
TiLoggingTagProvider, InterceptableViewBinder<V> {

private final String TAG = this.getClass().getSimpleName()
+ ":" + TiActivity.class.getSimpleName()
Expand Down Expand Up @@ -68,6 +67,11 @@ public List<BindViewInterceptor> getInterceptors(
return mDelegate.getInterceptors(predicate);
}

@Override
public String getLoggingTag() {
return TAG;
}

public P getPresenter() {
return mDelegate.getPresenter();
}
Expand Down Expand Up @@ -105,11 +109,6 @@ public boolean isDontKeepActivitiesEnabled() {
return AndroidDeveloperOptions.isDontKeepActivitiesEnabled(this);
}

@Override
public void logTiMessages(final String msg) {
Log.v(TAG, msg);
}

@Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import net.grandcentrix.thirtyinch.internal.InterceptableViewBinder;
import net.grandcentrix.thirtyinch.internal.PresenterSavior;
import net.grandcentrix.thirtyinch.internal.PresenterViewBinder;
import net.grandcentrix.thirtyinch.internal.TiPresenterLogger;
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
Expand All @@ -32,15 +32,14 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class TiFragment<P extends TiPresenter<V>, V extends TiView>
extends Fragment implements TiPresenterProvider<P>, TiPresenterLogger,
extends Fragment implements TiPresenterProvider<P>, TiLoggingTagProvider,
TiViewProvider<V>, InterceptableViewBinder<V> {

private static final String SAVED_STATE_PRESENTER_ID = "presenter_id";
Expand Down Expand Up @@ -76,6 +75,11 @@ public List<BindViewInterceptor> getInterceptors(
return mViewBinder.getInterceptors(predicate);
}

@Override
public String getLoggingTag() {
return TAG;
}

public P getPresenter() {
return mPresenter;
}
Expand All @@ -89,30 +93,25 @@ public void invalidateView() {
mViewBinder.invalidateView();
}

@Override
public void logTiMessages(final String msg) {
Log.v(TAG, msg);
}

@Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
Log.v(TAG, "onAttach()");
TiLog.v(TAG, "onAttach()");

}

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(" + savedInstanceState + ")");
TiLog.d(TAG, "onCreate(" + savedInstanceState + ")");

if (mPresenter == null && savedInstanceState != null) {
// recover with Savior
// this should always work.
final String recoveredPresenterId = savedInstanceState
.getString(SAVED_STATE_PRESENTER_ID);
if (recoveredPresenterId != null) {
Log.d(TAG, "try to recover Presenter with id: " + recoveredPresenterId);
TiLog.d(TAG, "try to recover Presenter with id: " + recoveredPresenterId);
//noinspection unchecked
mPresenter = (P) PresenterSavior.INSTANCE.recover(recoveredPresenterId);
if (mPresenter != null) {
Expand All @@ -122,13 +121,13 @@ public void onCreate(final Bundle savedInstanceState) {
PresenterSavior.INSTANCE.free(recoveredPresenterId);
mPresenterId = PresenterSavior.INSTANCE.safe(mPresenter);
}
Log.d(TAG, "recovered Presenter " + mPresenter);
TiLog.d(TAG, "recovered Presenter " + mPresenter);
}
}

if (mPresenter == null) {
mPresenter = providePresenter();
Log.d(TAG, "created Presenter: " + mPresenter);
TiLog.d(TAG, "created Presenter: " + mPresenter);
final TiConfiguration config = mPresenter.getConfig();
if (config.shouldRetainPresenter() && config.useStaticSaviorToRetain()) {
mPresenterId = PresenterSavior.INSTANCE.safe(mPresenter);
Expand Down Expand Up @@ -162,7 +161,7 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
public void onDestroy() {
super.onDestroy();
final FragmentActivity activity = getActivity();
Log.v(TAG, "onDestroy() recreating=" + !activity.isFinishing());
TiLog.v(TAG, "onDestroy() recreating=" + !activity.isFinishing());

boolean destroyPresenter = false;
if (activity.isFinishing()) {
Expand Down Expand Up @@ -197,15 +196,15 @@ public void onDestroy() {

@Override
public void onDestroyView() {
Log.v(TAG, "onDestroyView()");
TiLog.v(TAG, "onDestroyView()");
mPresenter.sleep();
super.onDestroyView();
}

@Override
public void onDetach() {
super.onDetach();
Log.v(TAG, "onDetach()");
TiLog.v(TAG, "onDetach()");
}

@Override
Expand All @@ -217,7 +216,7 @@ public void onSaveInstanceState(final Bundle outState) {
@Override
public void onStart() {
super.onStart();
Log.v(TAG, "onStart()");
TiLog.v(TAG, "onStart()");
mActivityStarted = true;

if (isUiPossible()) {
Expand All @@ -235,7 +234,7 @@ public void run() {

@Override
public void onStop() {
Log.v(TAG, "onStop()");
TiLog.v(TAG, "onStop()");
mActivityStarted = false;
mPresenter.sleep();
super.onStop();
Expand Down
Loading