From 285fa7e98fd00f7246416a33020c6c324558e64d Mon Sep 17 00:00:00 2001 From: Kejun Xia Date: Thu, 18 Aug 2016 22:47:32 +1000 Subject: [PATCH] Remove unused code --- samples/simple-mvvm/core/build.gradle | 48 ---- .../mvvm/controller/AbstractController.java | 24 -- .../controller/AbstractScreenController.java | 44 ---- .../controller/AppDelegateController.java | 103 -------- .../controller/CounterDetailController.java | 118 --------- .../controller/CounterMasterController.java | 137 ----------- .../CounterMasterInsideController.java | 47 ---- .../controller/CounterServiceController.java | 85 ------- .../samples/simple/mvvm/dto/IpPayload.java | 25 -- .../simple/mvvm/factory/ServiceFactory.java | 37 --- .../samples/simple/mvvm/http/IpService.java | 29 --- .../simple/mvvm/manager/AppManager.java | 53 ----- .../simple/mvvm/manager/CounterManager.java | 89 ------- .../simple/mvvm/service/ResourceService.java | 23 -- .../internal/TestCounterMasterController.java | 223 ------------------ 15 files changed, 1085 deletions(-) delete mode 100644 samples/simple-mvvm/core/build.gradle delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractScreenController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AppDelegateController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterDetailController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterInsideController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterServiceController.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/dto/IpPayload.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/factory/ServiceFactory.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/http/IpService.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/AppManager.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/CounterManager.java delete mode 100644 samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/service/ResourceService.java delete mode 100644 samples/simple-mvvm/core/src/test/java/com/shipdream/lib/android/mvc/samples/simple/mvp/controller/internal/TestCounterMasterController.java diff --git a/samples/simple-mvvm/core/build.gradle b/samples/simple-mvvm/core/build.gradle deleted file mode 100644 index 9386b4c..0000000 --- a/samples/simple-mvvm/core/build.gradle +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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. - */ - -apply plugin: 'java' - -sourceCompatibility = JavaVersion.VERSION_1_7 -targetCompatibility = JavaVersion.VERSION_1_7 - -repositories { - jcenter() - mavenLocal() -} - -sourceSets { - main { - java.srcDir 'src/main/java' - resources.srcDir 'src/main/resources' - } - - test { - java.srcDir 'src/test/java' - resources.srcDir 'src/test/resources' - } -} - -dependencies { - compile project(':library:android-mvc-core') - - compile 'com.squareup.retrofit2:retrofit:2.1.0' - compile 'com.squareup.retrofit2:converter-gson:2.1.0' - - testCompile rootProject.ext.lib.junit - testCompile rootProject.ext.lib.mokito - testCompile rootProject.ext.lib.slf4jLog -} \ No newline at end of file diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractController.java deleted file mode 100644 index 7779747..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractController.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.FragmentController; -import com.shipdream.lib.android.mvc.UiView; - -public abstract class AbstractController - extends FragmentController { -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractScreenController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractScreenController.java deleted file mode 100644 index 982689c..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AbstractScreenController.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.Reason; -import com.shipdream.lib.android.mvc.UiView; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.AppManager; -import com.shipdream.lib.android.mvc.samples.simple.mvp.service.ResourceService; - -import javax.inject.Inject; - -/** - * Controller for fragments used as a screen - full screen page - */ -public abstract class AbstractScreenController - extends AbstractController { - @Inject - protected AppManager appManager; - - @Inject - protected ResourceService resourceService; - - @Override - public void onViewReady(Reason reason) { - super.onViewReady(reason); - - //Refresh title when the controller's screen creates or recreates view - appManager.setTitle(resourceService.getDefaultTitle(getClass())); - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AppDelegateController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AppDelegateController.java deleted file mode 100644 index fe70417..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/AppDelegateController.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.NavLocation; -import com.shipdream.lib.android.mvc.NavigationManager; -import com.shipdream.lib.android.mvc.Reason; -import com.shipdream.lib.android.mvc.UiView; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.AppManager; - -import javax.inject.Inject; - -public class AppDelegateController extends AbstractController { - /** - * Define the interface of the controller's view - */ - public interface View extends UiView { - void updateTitle(String title); - void changeNavIcon(boolean showBackArrow); - } - - @Inject - private AppManager appManager; - - @Inject - private NavigationManager navigationManager; - - @Override - public Class modelType() { - return null; - } - - @Override - public void onViewReady(Reason reason) { - super.onViewReady(reason); - view.updateTitle(appManager.getModel().getTitle()); - doUpdateNavIcon(navigationManager.getModel().getCurrentLocation()); - - } - - public void startApp(Object sender) { - navigationManager.navigate(sender).to(CounterMasterController.class); - } - - public void navigateBack(Object sender) { - navigationManager.navigate(sender).back(); - } - - /** - * Subscribe to event when app manager updates current page's title - * @param event - */ - private void onEvent(AppManager.Event.OnTitleUpdated event) { - view.updateTitle(event.getTitle()); - } - - /** - * Subscribe to forward navigation - * @param event - */ - private void onEvent(NavigationManager.Event.OnLocationForward event) { - updateToolbar(event.getCurrentValue()); - } - - /** - * Subscribe to backward navigation - * @param event - */ - private void onEvent(NavigationManager.Event.OnLocationBack event) { - updateToolbar(event.getCurrentValue()); - } - - private void updateToolbar(final NavLocation location) { - uiThreadRunner.post(new Runnable() { - @Override - public void run() { - doUpdateNavIcon(location); - } - }); - } - - private void doUpdateNavIcon(NavLocation location) { - if (location != null) { - view.changeNavIcon(location.getPreviousLocation() != null); - } else { - view.changeNavIcon(false); - } - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterDetailController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterDetailController.java deleted file mode 100644 index 7c42b25..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterDetailController.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.NavigationManager; -import com.shipdream.lib.android.mvc.Reason; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.CounterManager; - -import javax.inject.Inject; - -public class CounterDetailController extends AbstractScreenController { - private class ContinuousCounter implements Runnable { - private final boolean incrementing; - private boolean canceled = false; - private static final long INTERVAL = 200; - - public ContinuousCounter(boolean incrementing) { - this.incrementing = incrementing; - } - - @Override - public void run() { - if (!canceled) { - if (incrementing) { - increment(this); - } else { - decrement(this); - } - - uiThreadRunner.postDelayed(this, INTERVAL); - } - } - - private void cancel() { - canceled = true; - } - } - - @Override - public Class modelType() { - return null; - } - - @Inject - private NavigationManager navigationManager; - - @Inject - private CounterManager counterManager; - - private ContinuousCounter incrementer; - private ContinuousCounter decrementer; - - @Override - public void onViewReady(Reason reason) { - super.onViewReady(reason); - view.update(); - } - - public void startContinuousIncrement() { - stopContinuousIncrement(); - incrementer = new ContinuousCounter(true); - incrementer.run(); - } - - public void stopContinuousIncrement() { - if (incrementer != null) { - incrementer.cancel(); - } - } - - public void startContinuousDecrement() { - stopContinuousDecrement(); - decrementer = new ContinuousCounter(false); - decrementer.run(); - } - - public void stopContinuousDecrement() { - if (decrementer != null) { - decrementer.cancel(); - } - } - - public String getCount() { - return String.valueOf(counterManager.getModel().getCount()); - } - - public void increment(Object sender) { - int count = counterManager.getModel().getCount(); - counterManager.setCount(sender, ++count); - } - - public void decrement(Object sender) { - int count = counterManager.getModel().getCount(); - counterManager.setCount(sender, --count); - } - - public void goBackToBasicView(Object sender) { - navigationManager.navigate(sender).back(); - } - - private void onEvent(CounterManager.Event2C.OnCounterUpdated event) { - view.update(); - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterController.java deleted file mode 100644 index 05a82ea..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterController.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.NavigationManager; -import com.shipdream.lib.android.mvc.Task; -import com.shipdream.lib.android.mvc.UiView; -import com.shipdream.lib.android.mvc.samples.simple.mvp.dto.IpPayload; -import com.shipdream.lib.android.mvc.samples.simple.mvp.factory.ServiceFactory; -import com.shipdream.lib.android.mvc.samples.simple.mvp.http.IpService; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.CounterManager; - -import java.io.IOException; - -import javax.inject.Inject; - -import retrofit2.Response; - -public class CounterMasterController extends AbstractScreenController { - @Override - public Class modelType() { - return Model.class; - } - - /** - * The view model of the CounterMasterScreen - */ - public static class Model { - private String count; - - public String getCount() { - return count; - } - } - - public interface View extends UiView{ - void showProgress(); - void hideProgress(); - void updateIpValue(String ip); - void showHttpError(int statusCode, String message); - void showNetworkError(IOException e); - } - - @Inject - private NavigationManager navigationManager; - - @Inject - private CounterManager counterManager; - - @Inject - private ServiceFactory serviceFactory; - - public void increment(Object sender) { - int count = counterManager.getModel().getCount(); - counterManager.setCount(sender, ++count); - } - - public void decrement(Object sender) { - int count = counterManager.getModel().getCount(); - counterManager.setCount(sender, --count); - } - - public void refreshIp() { - view.showProgress(); - - runTask(new Task>() { - @Override - public Response execute(Monitor> monitor) throws Exception { - return serviceFactory.createService(IpService.class) - .getIp("json").execute(); - } - }, new Task.Callback>() { - @Override - public void onSuccess(Response response) { - super.onSuccess(response); - if (response.isSuccessful()) { - view.updateIpValue(response.body().getIp()); - } else { - view.showHttpError(response.code(), response.message()); - logger.warn("Http error to get ip. error({}): {}", response.code(), response.message()); - } - } - - @Override - public void onException(Exception e) { - if (e instanceof IOException) { - view.showNetworkError((IOException) e); - } - - logger.warn(e.getMessage(), e); - } - - @Override - public void onFinally() { - super.onFinally(); - view.hideProgress(); - } - }); - } - - /** - * Go to detail view. - * @param sender - */ - public void goToDetailScreen(Object sender) { - //Navigate to CounterDetailController which is paired by CounterDetailScreen - navigationManager.navigate(sender).to(CounterDetailController.class); - } - - /** - * Event subscriber: notified by counterManager - * @param event - */ - private void onEvent(CounterManager.Event2C.OnCounterUpdated event) { - getModel().count = String.valueOf(event.getCount()); - - if (view != null) { - view.update(); - } - } - -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterInsideController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterInsideController.java deleted file mode 100644 index feeeff4..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterMasterInsideController.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.CounterManager; - -import javax.inject.Inject; - -public class CounterMasterInsideController extends AbstractController { - - @Override - public Class modelType() { - return null; - } - - @Inject - private CounterManager counterManager; - - public String getCount() { - return String.valueOf(counterManager.getModel().getCount()); - } - - public String getCountInEnglish() { - return counterManager.convertNumberToEnglish(counterManager.getModel().getCount()); - } - - private void onEvent(CounterManager.Event2C.OnCounterUpdated event) { - if (view != null) { - view.update(); - } - } - -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterServiceController.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterServiceController.java deleted file mode 100644 index c889a28..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/controller/CounterServiceController.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller; - -import com.shipdream.lib.android.mvc.NavigationManager; -import com.shipdream.lib.android.mvc.UiView; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.CounterManager; - -import javax.inject.Inject; - -public class CounterServiceController extends AbstractController { - @Override - public Class modelType() { - return null; - } - - public interface View extends UiView{ - void counterFinished(); - } - - private class AutoCounter implements Runnable { - private int count = 0; - private boolean canceled = false; - - @Override - public void run() { - if (!canceled) { - if (count ++ <= AUTO_FINISH_COUNT) { - counterManager.setCount(this, counterManager.getModel().getCount() + 1); - - uiThreadRunner.postDelayed(this, 1000); - } else { - view.counterFinished(); - } - } - } - - private void cancel() { - canceled = true; - } - } - - private static final int AUTO_FINISH_COUNT = 10; - private AutoCounter autoCounter; - - @Inject - private NavigationManager navigationManager; - - @Inject - private CounterManager counterManager; - - private void onEvent(CounterManager.Event2C.OnCounterUpdated event) { - view.update(); - } - - public int getCount() { - return counterManager.getModel().getCount(); - } - - public void startAutoIncrement() { - stopAutoIncrement(); - autoCounter = new AutoCounter(); - autoCounter.run(); - } - - public void stopAutoIncrement() { - if (autoCounter != null) { - autoCounter.cancel(); - } - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/dto/IpPayload.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/dto/IpPayload.java deleted file mode 100644 index 82b7554..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/dto/IpPayload.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.dto; - -public class IpPayload { - private String ip; - - public String getIp() { - return ip; - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/factory/ServiceFactory.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/factory/ServiceFactory.java deleted file mode 100644 index 56f27d2..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/factory/ServiceFactory.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.factory; - -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; - -public class ServiceFactory { - private Retrofit retrofit; - - public SERVICE createService(Class clazz) { - return retrofit().create(clazz); - } - - private Retrofit retrofit() { - if (retrofit == null) { - retrofit = new Retrofit.Builder() - .addConverterFactory(GsonConverterFactory.create()) - .baseUrl("https://api.ipify.org/").build(); - } - return retrofit; - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/http/IpService.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/http/IpService.java deleted file mode 100644 index 9522a05..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/http/IpService.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.http; - -import com.shipdream.lib.android.mvc.samples.simple.mvp.dto.IpPayload; - -import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.Query; - -public interface IpService { - //https://api.ipify.org/?format=json - @GET("/") - Call getIp(@Query("format") String format); -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/AppManager.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/AppManager.java deleted file mode 100644 index 5ee851a..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/AppManager.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.manager; - -import com.shipdream.lib.android.mvc.Manager; - -public class AppManager extends Manager{ - public interface Event { - class OnTitleUpdated{ - private final String title; - - public OnTitleUpdated(String title) { - this.title = title; - } - - public String getTitle() { - return title; - } - } - } - - public static class Model { - private String title; - - public String getTitle() { - return title; - } - } - - @Override - public Class modelType() { - return Model.class; - } - - public void setTitle(String title) { - getModel().title = title; - postEvent2C(new Event.OnTitleUpdated(title)); - } -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/CounterManager.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/CounterManager.java deleted file mode 100644 index 2784874..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/manager/CounterManager.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.manager; - -import com.shipdream.lib.android.mvc.Manager; - -public class CounterManager extends Manager { - /** - * Namespace the events for this controller by nested interface so that all its events would - * be referenced as CounterController.EventC2V.BlaBlaEvent. - */ - public interface Event2C { - /** - * Event2C to notify views counter has been updated - */ - class OnCounterUpdated { - private final int count; - public OnCounterUpdated(int count) { - this.count = count; - } - - public int getCount() { - return count; - } - } - } - - public static class Model { - private int count; - - public int getCount() { - return count; - } - - public void setCount(int count) { - this.count = count; - } - } - - /** - * Just return the class type of the model managed by this controller - */ - @Override - public Class modelType() { - return Model.class; - } - - public void setCount(Object sender, int count) { - getModel().setCount(count); - postEvent2C(new Event2C.OnCounterUpdated(count)); - } - - public String convertNumberToEnglish(int number) { - if (number < -3) { - return "Less than negative three"; - } else if (number == -3) { - return "Negative three"; - } else if (number == -2) { - return "Negative two"; - } else if (number == -1) { - return "Negative one"; - } else if (number == 0) { - return "Zero"; - } else if (number == 1) { - return "One"; - } else if (number == 2) { - return "Two"; - } else if (number == 3) { - return "Three"; - } else { - return "Greater than three"; - } - } - -} diff --git a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/service/ResourceService.java b/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/service/ResourceService.java deleted file mode 100644 index 0440ed8..0000000 --- a/samples/simple-mvvm/core/src/main/java/com/shipdream/lib/android/mvc/samples/simple/mvvm/service/ResourceService.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.service; - -import com.shipdream.lib.android.mvc.FragmentController; - -public interface ResourceService { - String getDefaultTitle(Class controllerClass); -} diff --git a/samples/simple-mvvm/core/src/test/java/com/shipdream/lib/android/mvc/samples/simple/mvp/controller/internal/TestCounterMasterController.java b/samples/simple-mvvm/core/src/test/java/com/shipdream/lib/android/mvc/samples/simple/mvp/controller/internal/TestCounterMasterController.java deleted file mode 100644 index 64953c2..0000000 --- a/samples/simple-mvvm/core/src/test/java/com/shipdream/lib/android/mvc/samples/simple/mvp/controller/internal/TestCounterMasterController.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * Copyright 2016 Kejun Xia - * - * 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 com.shipdream.lib.android.mvc.samples.simple.mvp.controller.internal; - -import com.shipdream.lib.android.mvc.Mvc; -import com.shipdream.lib.android.mvc.MvcComponent; -import com.shipdream.lib.android.mvc.NavigationManager; -import com.shipdream.lib.android.mvc.TestUtil; -import com.shipdream.lib.android.mvc.samples.simple.mvp.controller.CounterDetailController; -import com.shipdream.lib.android.mvc.samples.simple.mvp.controller.CounterMasterController; -import com.shipdream.lib.android.mvc.samples.simple.mvp.dto.IpPayload; -import com.shipdream.lib.android.mvc.samples.simple.mvp.factory.ServiceFactory; -import com.shipdream.lib.android.mvc.samples.simple.mvp.http.IpService; -import com.shipdream.lib.android.mvc.samples.simple.mvp.manager.CounterManager; -import com.shipdream.lib.android.mvc.samples.simple.mvp.service.ResourceService; -import com.shipdream.lib.poke.Provides; - -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.util.Random; - -import javax.inject.Inject; - -import okhttp3.ResponseBody; -import retrofit2.Call; -import retrofit2.Response; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class TestCounterMasterController extends BaseTest { - @Inject - private CounterManager counterManager; - - @Inject - private NavigationManager navigationManager; - - private CounterMasterController.View view; - private CounterMasterController controller; - - private ResourceService resourceServiceMock; - private Call ipServiceCallMock; - - //Prepare injection graph before calling setup method - @Override - protected void prepareGraph(MvcComponent overriddingComponent) throws Exception { - super.prepareGraph(overriddingComponent); - - overriddingComponent.register(new Object(){ - /** - * Mock resource service - * @return - */ - @Provides - public ResourceService resourceService() { - resourceServiceMock = mock(ResourceService.class); - return resourceServiceMock; - } - - /** - * Prepare objects to mock http calls - * @return - * @throws IOException - */ - @Provides - public ServiceFactory serviceFactory() throws IOException { - ipServiceCallMock = mock(Call.class); - - IpService ipServiceMock = mock(IpService.class); - when(ipServiceMock.getIp(anyString())).thenReturn(ipServiceCallMock); - - ServiceFactory serviceFactoryMock = mock(ServiceFactory.class); - when(serviceFactoryMock.createService(IpService.class)).thenReturn(ipServiceMock); - return serviceFactoryMock; - } - }); - } - - @Override - public void setUp() throws Exception { - super.setUp(); - - controller = new CounterMasterController(); - Mvc.graph().inject(controller); - controller.onCreated(); - - view = mock(CounterMasterController.View.class); - TestUtil.assignControllerView(controller, view); - } - - @Test - public void increment_should_post_counter_update_event_with_incremented_value() { - //1. Prepare view - CounterMasterController.View view = mock(CounterMasterController.View.class); - TestUtil.assignControllerView(controller, view); - - //mock controller model for count value - int value = new Random().nextInt(); - CounterManager.Model counterModel = new CounterManager.Model(); - counterModel.setCount(value); - //Mock the model of manager - counterManager.bindModel(this, counterModel); - - //2. Act - controller.increment(this); - - //3. Verify - verify(view, times(1)).update(); - Assert.assertEquals(String.valueOf(value + 1), controller.getModel().getCount()); - } - - @Test - public void should_navigate_correctly() { - //Act: navigate to MasterScreen - navigationManager.navigate(this).to(CounterMasterController.class); - - //Verify: location should be changed to MasterScreen - Assert.assertEquals(CounterMasterController.class.getName(), - navigationManager.getModel().getCurrentLocation().getLocationId()); - - //Act: navigate to DetailScreen - controller.goToDetailScreen(this); - - //Verify: Current location should be at the view paired with CounterDetailController - Assert.assertEquals(CounterDetailController.class.getName(), - navigationManager.getModel().getCurrentLocation().getLocationId()); - } - - @Test - public void should_update_view_with_correct_ip_and_show_and_dismiss_progress_bar() throws Exception { - //Prepare - //Prepare a good http response - final String fakeIpResult = "abc.123.456.xyz"; - - IpPayload payload = mock(IpPayload.class); - when(payload.getIp()).thenReturn(fakeIpResult); - when(ipServiceCallMock.execute()).thenReturn(Response.success(payload)); - - //Action - controller.refreshIp(); - - //Verify - //Showed loading progress - verify(view).showProgress(); - //Dismissed loading progress - verify(view).hideProgress(); - //Updated view's text view by the given fake ip result - verify(view).updateIpValue(fakeIpResult); - //Should not show error message - verify(view, times(0)).showHttpError(anyInt(), anyString()); - //Should not show network error message - verify(view, times(0)).showNetworkError(any(IOException.class)); - } - - @Test - public void should_show_error_message_on_HttpError_and_show_and_dismiss_progress_bar() throws Exception { - //Prepare - //Return 401 in the http response - int errorStatusCode = 401; - ResponseBody responseBody = mock(ResponseBody.class); - when(ipServiceCallMock.execute()).thenReturn( - Response.error(errorStatusCode, responseBody)); - - //Action - controller.refreshIp(); - - //Verify - //Showed loading progress - verify(view).showProgress(); - //Dismissed loading progress - verify(view).hideProgress(); - //View's ip address text view should not be updated - verify(view, times(0)).updateIpValue(anyString()); - //Should show http error message with given mocking data - verify(view, times(1)).showHttpError(errorStatusCode, null); - //Should not show network error message - verify(view, times(0)).showNetworkError(any(IOException.class)); - } - - @Test - public void should_show_error_message_on_NetworkError_and_show_and_dismiss_progress_bar() throws Exception { - //Prepare - //Throw an IOException to simulate an network error - IOException ioExceptionMock = mock(IOException.class); - when(ipServiceCallMock.execute()).thenThrow(ioExceptionMock); - - //Action - controller.refreshIp(); - - //Verify - //Showed loading progress - verify(view).showProgress(); - //Dismissed loading progress - verify(view).hideProgress(); - //View's ip address text view should not be updated - verify(view, times(0)).updateIpValue(anyString()); - //Should not show http error message - verify(view, times(0)).showHttpError(anyInt(), anyString()); - //Should show network error message with the given mocking exception - verify(view, times(1)).showNetworkError(ioExceptionMock); - } -}