Skip to content

Commit

Permalink
Broken shared logic and data from note controllers into note manager …
Browse files Browse the repository at this point in the history
…in the note sample project
  • Loading branch information
kejunxia committed Jan 21, 2016
1 parent 250a6c1 commit 74682de
Show file tree
Hide file tree
Showing 28 changed files with 746 additions and 583 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.shipdream.lib.android.mvc.controller;

import com.shipdream.lib.android.mvc.manager.BaseManagerImpl;
import com.shipdream.lib.android.mvc.manager.internal.BaseManagerImpl;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.shipdream.lib.android.mvc.event.bus.EventBus;
import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusC;
import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusV;
import com.shipdream.lib.android.mvc.manager.BaseManagerImpl;
import com.shipdream.lib.android.mvc.manager.internal.BaseManagerImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.manager;

/**
* This is the manager contract that needs to be extended by managers. When multiple controllers
* share logic and data, extract them into a manager.
* @param <MODEL>
*/
public interface BaseManager<MODEL> {
/**
* Gets the model which represents the state of the manager. <b>Don't change any values of
* the model by its setter methods inside from controllers.</b> Only this manager can change its
* own state directly.
*
* @return null when the manager doesn't need to get its model saved and restored
* automatically.
*/
MODEL getModel();

/**
* Binds a non-null model to the manager
* @param sender Who wants to bind it
* @param model The model to bind to this manager. CANNOT be NULL otherwise a runtime
* exception will be thrown
*/
void bindModel(Object sender, MODEL model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

package com.shipdream.lib.android.mvc.manager;
package com.shipdream.lib.android.mvc.manager.internal;

import com.shipdream.lib.android.mvc.MvcBean;
import com.shipdream.lib.android.mvc.event.BaseEventC;
import com.shipdream.lib.android.mvc.event.bus.EventBus;
import com.shipdream.lib.android.mvc.event.bus.annotation.EventBusC;
import com.shipdream.lib.android.mvc.manager.BaseManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,13 +36,23 @@
* to controllers to notify the state change in the shared manager.
* </p>
*/
public abstract class BaseManagerImpl<MODEL> extends MvcBean<MODEL> {
public abstract class BaseManagerImpl<MODEL> extends MvcBean<MODEL> implements BaseManager<MODEL> {
protected Logger logger = LoggerFactory.getLogger(getClass());

@Inject
@EventBusC
private EventBus eventBus2C;

/**
* Bind model to this manager
* @param sender Who wants to bind it
* @param model The model to bind to this manager. CANNOT be NULL otherwise a runtime
*/
@Override
public void bindModel(Object sender, MODEL model) {
super.bindModel(model);
}

/**
* Post an event to other controllers. Event will be posted on the same thread as the caller.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.shipdream.lib.android.mvc.event.BaseEventC;
import com.shipdream.lib.android.mvc.event.bus.EventBus;
import com.shipdream.lib.android.mvc.manager.BaseManagerImpl;
import com.shipdream.lib.android.mvc.manager.internal.BaseManagerImpl;

import org.junit.After;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.shipdream.lib.android.mvc.view.injection.manager.internal;

import com.shipdream.lib.android.mvc.manager.BaseManagerImpl;
import com.shipdream.lib.android.mvc.manager.internal.BaseManagerImpl;
import com.shipdream.lib.android.mvc.view.injection.manager.AccountManager;
import com.shipdream.lib.android.mvc.view.injection.service.StorageService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void onClick(View v) {

@Override
protected void onStartUp() {
appController.navigateToInitialLocation();
appController.startApp(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import android.widget.Toast;

import com.shipdream.lib.android.mvc.samples.note.R;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteController;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteCreateController;

import javax.inject.Inject;

public class NoteCreateFragment extends BaseFragment {
@Inject
private NoteController noteController;
private NoteCreateController noteCreateController;

private EditText title;
private EditText content;
Expand All @@ -53,11 +53,11 @@ public void onResume() {

@Override
public boolean onBackButtonPressed() {
noteController.addNote(title.getText().toString(), content.getText().toString());
noteCreateController.addNote(title.getText().toString(), content.getText().toString());
return true;
}

public void onEvent(NoteController.EventC2V.OnNoteCreated event) {
public void onEvent(NoteCreateController.EventC2V.OnNoteCreated event) {
Toast.makeText(getActivity(), "Note created.", Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import android.widget.Toast;

import com.shipdream.lib.android.mvc.samples.note.R;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteController;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteDetailController;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteListController;
import com.shipdream.lib.android.mvc.samples.note.model.dto.Note;

import org.slf4j.Logger;
Expand All @@ -37,7 +38,7 @@ public class NoteDetailFragment extends BaseFragment {
private Logger logger = LoggerFactory.getLogger(getClass());

@Inject
private NoteController noteController;
private NoteDetailController noteDetailController;

@Override
protected int getLayoutResId() {
Expand All @@ -51,7 +52,7 @@ public void onViewReady(View view, Bundle savedInstanceState, Reason reason) {
title = (TextView) view.findViewById(R.id.fragment_note_detail_title);
content = (TextView) view.findViewById(R.id.fragment_note_detail_content);

Note note = noteController.getModel().getViewingNote();
Note note = noteDetailController.getViewingNote();
displayNote(note);
}

Expand All @@ -72,16 +73,14 @@ public void onResume() {
@Override
public void onPause() {
super.onPause();
if (isVisible()) {
noteController.updateViewingNote(this, title.getText().toString(), content.getText().toString());
}
noteDetailController.updateViewingNote(this, title.getText().toString(), content.getText().toString());
}

private void onEvent(NoteController.EventC2V.OnNoteSelected event) {
private void onEvent(NoteListController.EventC2V.OnNoteSelected event) {
displayNote(event.getNote());
}

public void onEvent(NoteController.EventC2V.OnNoteUpdated event) {
public void onEvent(NoteDetailController.EventC2V.OnNoteUpdated event) {
Toast.makeText(getActivity(), "Note updated.", Toast.LENGTH_SHORT).show();
logger.debug("Note updated by {}", event.getSender().hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import android.widget.TextView;

import com.shipdream.lib.android.mvc.samples.note.R;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteController;
import com.shipdream.lib.android.mvc.samples.note.controller.NoteListController;
import com.shipdream.lib.android.mvc.samples.note.model.dto.Note;

import javax.inject.Inject;
Expand All @@ -43,8 +43,11 @@ public class NoteListFragment extends BaseFragment {
private RecyclerView.LayoutManager layoutManager;
private ActionMode actionMode;

// @Inject
// private NoteController noteController;

@Inject
private NoteController noteController;
private NoteListController noteListController;

@Override
protected int getLayoutResId() {
Expand All @@ -67,7 +70,7 @@ public void onViewReady(View view, Bundle savedInstanceState, Reason reason) {
buttonAddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteController.toCreateNote();
noteListController.toCreateNote();
}
});

Expand Down Expand Up @@ -100,15 +103,15 @@ public boolean onBackButtonPressed() {
}
}

public void onEvent(NoteController.EventC2V.OnNoteSelectionChanged event) {
public void onEvent(NoteListController.EventC2V.OnNoteSelectionChanged event) {
updateList();
}

public void onEvent(NoteController.EventC2V.OnNoteRemoved event) {
public void onEvent(NoteListController.EventC2V.OnNoteRemoved event) {
updateList();
}

public void onEvent(NoteController.EventC2V.OnEditModeBegan event) {
public void onEvent(NoteListController.EventC2V.OnEditModeBegan event) {
showActionMode();
}

Expand All @@ -129,7 +132,7 @@ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
noteController.removeNote(item);
noteListController.removeNote(item);
mode.finish();
return true;
}
Expand All @@ -138,7 +141,7 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

@Override
public void onDestroyActionMode(ActionMode mode) {
noteController.clearSelections(mode);
noteListController.clearSelections(mode);
actionMode = null;
}
});
Expand All @@ -156,9 +159,9 @@ private void updateList() {
listView.setVisibility(View.GONE);
}

if(actionMode != null && !noteController.inSelectionMode()) {
if(actionMode != null && !noteListController.inSelectionMode()) {
actionMode.finish();
} else if(noteController.inSelectionMode() && actionMode == null) {
} else if(noteListController.inSelectionMode() && actionMode == null) {
showActionMode();
}
}
Expand All @@ -182,28 +185,28 @@ public NoteItemHolder onCreateViewHolder(ViewGroup parent, int i) {

@Override
public void onBindViewHolder(NoteItemHolder holder, final int i) {
final Note note = noteListFragment.noteController.getNotes().get(i);
final Note note = noteListFragment.noteListController.getNotes().get(i);
holder.title.setText(note.getTitle());
holder.updateTime.setText(noteListFragment.noteController.getDisplayNoteUpdateTime(note.getUpdateTime()));
holder.root.setSelected(noteListFragment.noteController.isSelected(note.getId()) >= 0);
holder.updateTime.setText(noteListFragment.noteListController.getDisplayNoteUpdateTime(note.getUpdateTime()));
holder.root.setSelected(noteListFragment.noteListController.isSelected(note.getId()) >= 0);
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
noteListFragment.noteController.selectNote(note.getId());
noteListFragment.noteListController.selectNote(note.getId());
}
});
holder.root.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
noteListFragment.noteController.toggleSelection(v, note.getId());
noteListFragment.noteListController.toggleSelection(v, note.getId());
return true;
}
});
}

@Override
public int getItemCount() {
return noteListFragment.noteController.getNotes().size();
return noteListFragment.noteListController.getNotes().size();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ enum Orientation {
LANDSCAPE
}

/**
* Navigate the app to the initial location. Current logic is navigate the app to note list if
* the current location is null.
*/
void navigateToInitialLocation();
void startApp(Object sender);

void notifyOrientationChanged(Orientation lastOrientation, Orientation currentOrientation);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.shipdream.lib.android.mvc.samples.note.controller;

import com.shipdream.lib.android.mvc.controller.BaseController;
import com.shipdream.lib.android.mvc.event.BaseEventV;
import com.shipdream.lib.android.mvc.samples.note.model.NoteCreateModel;

public interface NoteCreateController extends BaseController<NoteCreateModel> {
/**
* Add a new note and will raise {@link EventC2V.OnNoteCreated}
* @param title The title
* @param content The content
*/
void addNote(String title, String content);

interface EventC2V {
/**
* Raises when a new note is created
*/
class OnNoteCreated extends BaseEventV {
public OnNoteCreated(Object sender) {
super(sender);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.shipdream.lib.android.mvc.samples.note.controller;

import com.shipdream.lib.android.mvc.controller.BaseController;
import com.shipdream.lib.android.mvc.event.BaseEventV;
import com.shipdream.lib.android.mvc.samples.note.model.NoteDetailModel;
import com.shipdream.lib.android.mvc.samples.note.model.dto.Note;

public interface NoteDetailController extends BaseController<NoteDetailModel> {
/**
* Updates the note being viewed if there is one. Will raise {@link EventC2V.OnNoteUpdated} if
* the viewing note is updated.
* @param title The title
* @param content The content
*/
void updateViewingNote(Object sender, String title, String content);

/**
* Get the note being viewed.
* @return
*/
Note getViewingNote();

interface EventC2V {
/**
* Raises when a new note is updated
*/
class OnNoteUpdated extends BaseEventV {
public OnNoteUpdated(Object sender) {
super(sender);
}
}
}

}
Loading

0 comments on commit 74682de

Please sign in to comment.