Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C: 4 - Refactored example 1 to use MVP pattern. Closes #15 #33

Merged
merged 1 commit into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -21,7 +21,7 @@

import com.vestrel00.daggerbutterknifemvp.R;
import com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivity;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.Example1Fragment;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view.Example1Fragment;

/**
* Activity that contains a single Fragment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivityModule;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.Example1Fragment;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.Example1FragmentModule;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view.Example1Fragment;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view.Example1FragmentModule;

import dagger.Binds;
import dagger.Module;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017 Vandolf Estrellado
*
* 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.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.presenter;

import com.vestrel00.daggerbutterknifemvp.ui.common.presenter.Presenter;

/**
* A {@link Presenter} that does some work and shows the results.
*/
public interface Example1Presenter extends Presenter {

void onDoSomething();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017 Vandolf Estrellado
*
* 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.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.presenter;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.presenter.BasePresenter;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view.Example1View;
import com.vestrel00.daggerbutterknifemvp.util.PerActivityUtil;
import com.vestrel00.daggerbutterknifemvp.util.PerFragmentUtil;
import com.vestrel00.daggerbutterknifemvp.util.SingletonUtil;

import javax.inject.Inject;

/**
* An implementation of {@link Example1Presenter}.
*/
@PerFragment
final class Example1PresenterImpl extends BasePresenter<Example1View> implements Example1Presenter {

private final SingletonUtil singletonUtil;
private final PerActivityUtil perActivityUtil;
private final PerFragmentUtil perFragmentUtil;

@Inject
Example1PresenterImpl(Example1View view, SingletonUtil singletonUtil,
PerActivityUtil perActivityUtil, PerFragmentUtil perFragmentUtil) {
super(view);
this.singletonUtil = singletonUtil;
this.perActivityUtil = perActivityUtil;
this.perFragmentUtil = perFragmentUtil;
}

@Override
public void onDoSomething() {
// Do something here. Maybe make an asynchronous call to fetch data...
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
view.showSomething(something);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017 Vandolf Estrellado
*
* 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.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.presenter;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;

import dagger.Binds;
import dagger.Module;

/**
* Provides example 1 presenter dependencies.
*/
@Module
public abstract class Example1PresenterModule {

@Binds
@PerFragment
abstract Example1Presenter example1Presenter(Example1PresenterImpl example1PresenterImpl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment;
package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view;

import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -23,29 +23,17 @@
import android.widget.TextView;

import com.vestrel00.daggerbutterknifemvp.R;
import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragment;
import com.vestrel00.daggerbutterknifemvp.util.PerActivityUtil;
import com.vestrel00.daggerbutterknifemvp.util.PerFragmentUtil;
import com.vestrel00.daggerbutterknifemvp.util.SingletonUtil;

import javax.inject.Inject;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseViewFragment;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.presenter.Example1Presenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
* A fragment implementation of {@link Example1View}.
*/
public final class Example1Fragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;

@Inject
PerActivityUtil perActivityUtil;

@Inject
PerFragmentUtil perFragmentUtil;
public final class Example1Fragment extends BaseViewFragment<Example1Presenter>
implements Example1View {

@BindView(R.id.some_text)
TextView someText;
Expand All @@ -56,15 +44,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
return inflater.inflate(R.layout.example_1_fragment, container, false);
}

@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
showSomething(something);
@Override
public void showSomething(String something) {
someText.setText(something);
}

private void showSomething(String something) {
someText.setText(something);
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
presenter.onDoSomething();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment;
package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view;

import android.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragmentModule;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseFragmentModule;
import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.presenter.Example1PresenterModule;

import javax.inject.Named;

Expand All @@ -31,6 +32,7 @@
*/
@Module(includes = {
BaseFragmentModule.class,
Example1PresenterModule.class
})
public abstract class Example1FragmentModule {

Expand All @@ -46,4 +48,8 @@ public abstract class Example1FragmentModule {
@Named(BaseFragmentModule.FRAGMENT)
@PerFragment
abstract Fragment fragment(Example1Fragment example1Fragment);

@Binds
@PerFragment
abstract Example1View example1View(Example1Fragment example1Fragment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017 Vandolf Estrellado
*
* 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.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view;

import com.vestrel00.daggerbutterknifemvp.ui.common.view.MVPView;

/**
* A view that contains a button that does something.
*/
public interface Example1View extends MVPView {

void showSomething(String something);
}