Skip to content

Commit

Permalink
⚡ added main fragment for test.
Browse files Browse the repository at this point in the history
  • Loading branch information
PondThaitay committed May 14, 2017
1 parent 0d30cc6 commit 655fe09
Show file tree
Hide file tree
Showing 19 changed files with 444 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.wisdomlanna.www.dagger2_mvp_example.module.NetworkModule;
import com.wisdomlanna.www.dagger2_mvp_example.module.UtilModule;
import com.wisdomlanna.www.dagger2_mvp_example.template.activity.CustomActivity;
import com.wisdomlanna.www.dagger2_mvp_example.template.frangment.CustomFragment;
import com.wisdomlanna.www.dagger2_mvp_example.ui.MainActivity;
import com.wisdomlanna.www.dagger2_mvp_example.ui.frangment.MainFragment;

import javax.inject.Singleton;

Expand All @@ -21,5 +21,5 @@ public interface ApplicationComponent {

void inject(CustomActivity customActivity);

void inject(CustomFragment customFragment);
void inject(MainFragment mainFragment);
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
package com.wisdomlanna.www.dagger2_mvp_example.template.frangment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.wisdomlanna.www.dagger2_mvp_example.ApplicationComponent;
import com.wisdomlanna.www.dagger2_mvp_example.R;
import com.wisdomlanna.www.dagger2_mvp_example.Utils;
import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseFragment;

import javax.inject.Inject;

import butterknife.BindView;
import timber.log.Timber;

public class CustomFragment extends BaseFragment<CustomFragmentPresenter>
implements CustomFragmentInterface.View {

@Inject
Utils utils;

@BindView(R.id.tv_test)
TextView tvTest;
private int number;

public static CustomFragment newInstance() {
CustomFragment fragment = new CustomFragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}

@SuppressLint("DefaultLocale")
@Override
public void testResult() {
Timber.d("status network : %b", utils.isNetworkAvailable());
tvTest.setText(String.format("test : %d", number));
}

@Override
protected int layoutToInflate() {
return R.layout.custom_fragment;
return 0;
}

@Override
protected void doInjection(ApplicationComponent component) {
component.inject(CustomFragment.this);

}

@Override
Expand Down Expand Up @@ -76,19 +53,16 @@ protected void setupView() {

@Override
protected void initialize() {
Timber.d("initialize");
number = 5555;
getPresenter().testFragment();

}

@Override
protected void saveInstanceState(Bundle outState) {
outState.putInt("number", number);

}

@Override
public void restoreView(Bundle savedInstanceState) {
number = savedInstanceState.getInt("number");
testResult();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
class CustomFragmentInterface {

interface View extends BaseInterface.View {
void testResult();

}

interface Presenter {
void testFragment();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,4 @@ public void onViewStart() {
public void onViewStop() {

}

@Override
public void testFragment() {
getView().testResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.wisdomlanna.www.dagger2_mvp_example.R;
import com.wisdomlanna.www.dagger2_mvp_example.R2;
import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao;
import com.wisdomlanna.www.dagger2_mvp_example.template.frangment.CustomFragment;
import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseActivity;
import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent;
import com.wisdomlanna.www.dagger2_mvp_example.ui.frangment.MainFragment;

import org.parceler.Parcels;

Expand Down Expand Up @@ -100,7 +100,7 @@ protected void setupView() {
@Override
protected void initialize() {
getSupportFragmentManager().beginTransaction()
.add(R.id.container_fragment, CustomFragment.newInstance())
.add(R.id.container_fragment, MainFragment.newInstance())
.commit();
getPresenter().loadUserInfo("pondthaitay");
getPresenter().plus("5", "5");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

@org.parceler.Parcel(org.parceler.Parcel.Serialization.BEAN)
class BaseItem {

private int type;

public BaseItem(int type) {
this.type = type;
}

public BaseItem() {
}

int getType() {
return type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

public class BaseItemType{
public static final int TYPE_PROGRESS = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

import android.support.v7.widget.RecyclerView;

import com.wisdomlanna.www.dagger2_mvp_example.ui.base.exception.MvpPresenterNotCreateException;

import java.util.List;

public abstract class BaseListAdapter<VH extends BaseViewHolder, P extends BaseListAdapterInterface.Presenter>
extends RecyclerView.Adapter<VH>
implements BaseListAdapterInterface.Adapter{

private P presenter;

public interface OnLoadMoreListener{
void onLoadMore();
}

@SuppressWarnings( "unchecked" )
public BaseListAdapter(){
presenter = createPresenter();
presenter.setAdapter( this );
}

abstract P createPresenter();

@Override
public P getPresenter(){
if( presenter != null ) return presenter;
throw new MvpPresenterNotCreateException();
}

@SuppressWarnings( "unchecked" )
public List<BaseItem> getItems(){
return getPresenter().getItems();
}

public BaseItem getItem( int pos ){
return getPresenter().getItem( pos );
}

public boolean hasItems(){
return getPresenter().hasItems();
}

public void setItems( List<BaseItem> items ){
getPresenter().setItems( items );
}

public void addItem( BaseItem item ){
getPresenter().addItem( item );
}

public void removeItem( int index ){
getPresenter().removeItem( index );
}

public void removeAllItems(){
getPresenter().removeAllItems();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

import java.util.List;

interface BaseListAdapterInterface {

interface Adapter {
Presenter getPresenter();

void notifyDataSetChanged();

void notifyItemInserted(int index);

void notifyItemRemoved(int index);
}

interface Presenter<A extends BaseListAdapterInterface.Adapter> {
void setAdapter(A adapter);

A getAdapter();

int getItemViewType(int pos);

int getItemCount();

boolean hasItems();

List<BaseItem> getItems();

BaseItem getItem(int pos);

void setItems(List<BaseItem> items);

void addItem(BaseItem item);

void removeItem(int index);

void removeAllItems();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

import com.wisdomlanna.www.dagger2_mvp_example.ui.base.exception.MvpViewNotAttachedException;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseListAdapterPresenter<A extends BaseListAdapterInterface.Adapter>
implements BaseListAdapterInterface.Presenter<A> {

private WeakReference<A> adapter;
private List<BaseItem> items;

public BaseListAdapterPresenter() {
this.items = new ArrayList<>();
}

@Override
public void setAdapter(A adapter) {
this.adapter = new WeakReference<>(adapter);
}

@Override
public A getAdapter() {
if (adapter != null) return adapter.get();
throw new MvpViewNotAttachedException();
}

@Override
public int getItemViewType(int pos) {
return getPrivateItems().get(pos).getType();
}

@Override
public int getItemCount() {
return getPrivateItems().size();
}

@Override
public List<BaseItem> getItems() {
return getPrivateItems();
}

@Override
public BaseItem getItem(int pos) {
return getPrivateItems().get(pos);
}

@Override
public boolean hasItems() {
return getItemCount() > 0;
}

@Override
public void setItems(List<BaseItem> items) {
this.items = items;
getAdapter().notifyDataSetChanged();
}

@Override
public void addItem(BaseItem item) {
getPrivateItems().add(item);
getAdapter().notifyItemInserted(getItemCount() - 1);
}

@Override
public void removeItem(int index) {
getPrivateItems().remove(index);
getAdapter().notifyItemRemoved(index);
}

@Override
public void removeAllItems() {
getPrivateItems().clear();
getAdapter().notifyDataSetChanged();
}

private List<BaseItem> getPrivateItems() {
if (items == null) return new ArrayList<>();
return items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.base.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import butterknife.ButterKnife;

abstract class BaseViewHolder extends RecyclerView.ViewHolder {

public BaseViewHolder(ViewGroup parent, int layout) {
super(LayoutInflater.from(parent.getContext()).inflate(layout, parent, false));
ButterKnife.bind(this, itemView);
}

protected Context getContext() {
return itemView.getContext();
}
}
Loading

0 comments on commit 655fe09

Please sign in to comment.