Commit d2000cfc authored by d4vidi's avatar d4vidi

Refactor business logic objects getters

parent ca6220fe
package com.wix.reactnativenotifications.core;
package com.wix.reactnativenotifications;
import android.app.Activity;
import android.app.Application;
......@@ -13,6 +13,10 @@ import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.ReactAppLifecycleFacade;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
......@@ -27,8 +31,10 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
public RNNotificationsModule(Application application, ReactApplicationContext reactContext) {
super(reactContext);
ReactAppLifecycleFacade.get().init(reactContext);
ReactAppLifecycleFacade.get().addVisibilityListener(this);
if (AppLifecycleFacadeHolder.get() instanceof ReactAppLifecycleFacade) {
((ReactAppLifecycleFacade) AppLifecycleFacadeHolder.get()).init(reactContext);
}
AppLifecycleFacadeHolder.get().addVisibilityListener(this);
application.registerActivityLifecycleCallbacks(this);
}
......@@ -58,7 +64,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
Object result = null;
try {
final PushNotificationProps notification = InitialNotification.getInstance().get();
final PushNotificationProps notification = InitialNotificationHolder.getInstance().get();
if (notification == null) {
return;
}
......@@ -73,7 +79,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
public void postLocalNotification(ReadableMap notificationPropsMap, int notificationId) {
Log.d(LOGTAG, "Native method invocation: postLocalNotification");
final Bundle notificationProps = Arguments.toBundle(notificationPropsMap);
final IPushNotification pushNotification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationProps, ReactAppLifecycleFacade.get());
final IPushNotification pushNotification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationProps);
pushNotification.onPostRequest(notificationId);
}
......
......@@ -7,7 +7,6 @@ import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.wix.reactnativenotifications.core.RNNotificationsModule;
import java.util.Arrays;
import java.util.Collections;
......
package com.wix.reactnativenotifications.core;
public class AppLifecycleFacadeHolder {
protected static AppLifecycleFacade sInstance = new ReactAppLifecycleFacade();
public static AppLifecycleFacade get() {
return sInstance;
}
public static void set(AppLifecycleFacade facade) {
sInstance = facade;
}
}
......@@ -4,22 +4,22 @@ import android.support.annotation.Nullable;
import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
public class InitialNotification {
public class InitialNotificationHolder {
private static InitialNotification sInstance;
private static InitialNotificationHolder sInstance;
private PushNotificationProps mNotification;
public static void setInstance(InitialNotification instance) {
public static void setInstance(InitialNotificationHolder instance) {
sInstance = instance;
}
/*package*/ InitialNotification() {
/*package*/ InitialNotificationHolder() {
}
public static InitialNotification getInstance() {
public static InitialNotificationHolder getInstance() {
if (sInstance == null) {
sInstance = new InitialNotification();
sInstance = new InitialNotificationHolder();
}
return sInstance;
}
......
......@@ -20,7 +20,7 @@ public class ProxyService extends IntentService {
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "New intent: "+intent);
final Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
final IPushNotification pushNotification = PushNotification.get(this, notificationData, ReactAppLifecycleFacade.get());
final IPushNotification pushNotification = PushNotification.get(this, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
......
......@@ -12,16 +12,10 @@ import static com.wix.reactnativenotifications.Defs.LOGTAG;
public class ReactAppLifecycleFacade implements AppLifecycleFacade {
private static final ReactAppLifecycleFacade sInstance = new ReactAppLifecycleFacade();
private ReactContext mReactContext;
private boolean mIsVisible;
private Set<AppVisibilityListener> mListeners = new CopyOnWriteArraySet<>();
public static ReactAppLifecycleFacade get() {
return sInstance;
}
public void init(ReactContext reactContext) {
mReactContext = reactContext;
reactContext.addLifecycleEventListener(new LifecycleEventListener() {
......
......@@ -7,5 +7,5 @@ import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
public interface INotificationsApplication {
IPushNotification getPushNotification(Context context, Bundle bundle, AppLifecycleFacade defaultFacade, AppLaunchHelper defaultAppLaunchHelper);
IPushNotification getPushNotification(Context context, Bundle bundle, AppLifecycleFacade facade, AppLaunchHelper defaultAppLaunchHelper);
}
......@@ -11,10 +11,11 @@ import com.facebook.react.bridge.ReactContext;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener;
import com.wix.reactnativenotifications.core.InitialNotification;
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
import com.wix.reactnativenotifications.core.ProxyService;
import com.wix.reactnativenotifications.core.JsIOHelper;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME;
......@@ -38,16 +39,12 @@ public class PushNotification implements IPushNotification {
}
};
public static IPushNotification get(Context context, Bundle bundle, AppLifecycleFacade facade) {
return PushNotification.get(context, bundle, facade, new AppLaunchHelper());
}
public static IPushNotification get(Context context, Bundle bundle, AppLifecycleFacade facade, AppLaunchHelper appLaunchHelper) {
public static IPushNotification get(Context context, Bundle bundle) {
Context appContext = context.getApplicationContext();
if (appContext instanceof INotificationsApplication) {
return ((INotificationsApplication) appContext).getPushNotification(context, bundle, facade, appLaunchHelper);
return ((INotificationsApplication) appContext).getPushNotification(context, bundle, AppLifecycleFacadeHolder.get(), new AppLaunchHelper());
}
return new PushNotification(context, bundle, facade, appLaunchHelper, new JsIOHelper());
return new PushNotification(context, bundle, AppLifecycleFacadeHolder.get(), new AppLaunchHelper(), new JsIOHelper());
}
protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper JsIOHelper) {
......@@ -110,7 +107,7 @@ public class PushNotification implements IPushNotification {
}
protected void setAsInitialNotification() {
InitialNotification.getInstance().set(mNotificationProps);
InitialNotificationHolder.getInstance().set(mNotificationProps);
}
protected void dispatchImmediately() {
......
......@@ -5,7 +5,7 @@ import android.app.NotificationManager;
import android.content.Context;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.InitialNotification;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
public class PushNotificationsDrawer implements IPushNotificationsDrawer {
......@@ -45,7 +45,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer {
boolean launchIntentsActivity = mAppLaunchHelper.isLaunchIntentsActivity(activity);
boolean launchIntentOfNotification = mAppLaunchHelper.isLaunchIntentOfNotification(activity.getIntent());
if (launchIntentsActivity && !launchIntentOfNotification) {
InitialNotification.getInstance().clear();
InitialNotificationHolder.getInstance().clear();
}
}
......
......@@ -6,7 +6,6 @@ import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.ReactAppLifecycleFacade;
import static com.wix.reactnativenotifications.Defs.LOGTAG;
......@@ -17,7 +16,7 @@ public class GcmMessageHandlerService extends GcmListenerService {
Log.d(LOGTAG, "New message from GCM: " + bundle);
try {
final IPushNotification notification = PushNotification.get(getApplicationContext(), bundle, ReactAppLifecycleFacade.get());
final IPushNotification notification = PushNotification.get(getApplicationContext(), bundle);
notification.onReceived();
} catch (IPushNotification.InvalidNotificationException e) {
// A GCM message, yes - but not the kind we know how to work with.
......
......@@ -10,18 +10,18 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class InitialNotificationTest {
public class InitialNotificationHolderTest {
@Test
public void initialState() throws Exception {
final InitialNotification uut = createUUT();
final InitialNotificationHolder uut = createUUT();
assertNull(uut.get());
}
@Test
public void setsInitialNotification() throws Exception {
PushNotificationProps props = mock(PushNotificationProps.class);
final InitialNotification uut = createUUT();
final InitialNotificationHolder uut = createUUT();
uut.set(props);
assertEquals(props, uut.get());
}
......@@ -29,7 +29,7 @@ public class InitialNotificationTest {
@Test
public void clearsInitialNotification() throws Exception {
PushNotificationProps props = mock(PushNotificationProps.class);
final InitialNotification uut = createUUT();
final InitialNotificationHolder uut = createUUT();
uut.set(props);
uut.clear();
assertNull(uut.get());
......@@ -39,7 +39,7 @@ public class InitialNotificationTest {
public void replacesInitialNotification() throws Exception {
PushNotificationProps props1 = mock(PushNotificationProps.class);
PushNotificationProps props2 = mock(PushNotificationProps.class);
final InitialNotification uut = createUUT();
final InitialNotificationHolder uut = createUUT();
uut.set(props1);
uut.set(props2);
assertNotEquals(props1, props2);
......@@ -48,12 +48,12 @@ public class InitialNotificationTest {
@Test
public void isALazySingleton() throws Exception {
final InitialNotification instance = InitialNotification.getInstance();
final InitialNotificationHolder instance = InitialNotificationHolder.getInstance();
assertNotNull(instance);
assertEquals(instance, InitialNotification.getInstance());
assertEquals(instance, InitialNotificationHolder.getInstance());
}
private InitialNotification createUUT() {
return new InitialNotification();
private InitialNotificationHolder createUUT() {
return new InitialNotificationHolder();
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ import com.facebook.react.bridge.ReactContext;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener;
import com.wix.reactnativenotifications.core.InitialNotification;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.JsIOHelper;
import org.junit.Before;
......@@ -58,7 +58,7 @@ public class PushNotificationTest {
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
InitialNotification.setInstance(mock(InitialNotification.class));
InitialNotificationHolder.setInstance(mock(InitialNotificationHolder.class));
when(mDefaultBundle.getString(eq("title"))).thenReturn(DEFAULT_NOTIFICATION_TITLE);
when(mDefaultBundle.getString(eq("body"))).thenReturn(DEFAULT_NOTIFICATION_BODY);
......@@ -96,7 +96,7 @@ public class PushNotificationTest {
final PushNotification uut = createUUT();
uut.onOpened();
verify(InitialNotification.getInstance()).set(any(PushNotificationProps.class));
verify(InitialNotificationHolder.getInstance()).set(any(PushNotificationProps.class));
}
@Test
......@@ -119,7 +119,7 @@ public class PushNotificationTest {
final PushNotification uut = createUUT();
uut.onOpened();
verify(InitialNotification.getInstance(), never()).set(any(PushNotificationProps.class));
verify(InitialNotificationHolder.getInstance(), never()).set(any(PushNotificationProps.class));
}
@Test
......@@ -176,7 +176,7 @@ public class PushNotificationTest {
final PushNotification uut = createUUT();
uut.onOpened();
verify(InitialNotification.getInstance(), never()).set(any(PushNotificationProps.class));
verify(InitialNotificationHolder.getInstance(), never()).set(any(PushNotificationProps.class));
}
@Test
......@@ -187,7 +187,7 @@ public class PushNotificationTest {
final PushNotification uut = createUUT();
uut.onOpened();
verify(InitialNotification.getInstance()).set(any(PushNotificationProps.class));
verify(InitialNotificationHolder.getInstance()).set(any(PushNotificationProps.class));
}
@Test
......
......@@ -7,7 +7,7 @@ import android.content.Intent;
import com.facebook.react.bridge.ReactContext;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.InitialNotification;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import org.junit.Before;
import org.junit.Test;
......@@ -35,7 +35,7 @@ public class PushNotificationsDrawerTest {
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
InitialNotification.setInstance(mock(InitialNotification.class));
InitialNotificationHolder.setInstance(mock(InitialNotificationHolder.class));
when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(mNotificationManager);
}
......@@ -60,7 +60,7 @@ public class PushNotificationsDrawerTest {
@Test
public void onNewActivity_activityIsTheOneLaunchedByNotifs_clearInitialNotification() throws Exception {
verify(InitialNotification.getInstance(), never()).clear();
verify(InitialNotificationHolder.getInstance(), never()).clear();
Activity activity = mock(Activity.class);
Intent intent = mock(Intent.class);
......@@ -70,7 +70,7 @@ public class PushNotificationsDrawerTest {
createUUT().onNewActivity(activity);
verify(InitialNotification.getInstance()).clear();
verify(InitialNotificationHolder.getInstance()).clear();
}
@Test
......@@ -83,7 +83,7 @@ public class PushNotificationsDrawerTest {
createUUT().onNewActivity(activity);
verify(InitialNotification.getInstance(), never()).clear();
verify(InitialNotificationHolder.getInstance(), never()).clear();
}
@Test
......@@ -96,7 +96,7 @@ public class PushNotificationsDrawerTest {
createUUT().onNewActivity(activity);
verify(InitialNotification.getInstance(), never()).clear();
verify(InitialNotificationHolder.getInstance(), never()).clear();
}
protected PushNotificationsDrawer createUUT() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment