Commits

Jiangge Zhang committed f96822c

保存 & 编辑笔记的表现层(策略应用)

  • Participants
  • Parent commits 509fca4

Comments (0)

Files changed (4)

File Project/src/com/noodle/note/activity/EditNoteActivity.java

  * 
  */
 public class EditNoteActivity extends Activity {
+	public final static String SAVE_BUNDLE = "EDIT_NOTE_ACTIVITY_BUNDLE";
+	
+	/**
+	 * 工作模式
+	 */
+	private EditNoteWorkMode workMode = EditNoteWorkMode.getNewEditMode();
 
-	/** 笔记内容文本框 */
+	/**
+	 * 笔记内容文本框
+	 */
 	private EditText contentTextbox;
-	/** 保存笔记按钮 */
+
+	/**
+	 * 保存笔记按钮
+	 */
 	private Button savingButton;
-	/** 保存笔记按钮监听器 */
+
+	/**
+	 * 保存笔记按钮监听器 (跳转到 SavingNote Activity)
+	 */
 	private OnClickListener savingButtonListener = new OnClickListener() {
-		/**
-		 * 跳转到 SavingNote Activity
-		 */
 		@Override
 		public void onClick(View v) {
-			String content = contentTextbox.getText().toString();
-			Intent intent = new Intent();
-			intent.setClass(EditNoteActivity.this, SavingNoteActivity.class);
-			intent.putExtra(String.valueOf(R.id.editNoteTextbox), content);
+			// 创建跳转 Intent
+			Intent intent = new Intent(EditNoteActivity.this, SavingNoteActivity.class);
+			// 更新状态到 Work Mode
+			workMode.updateArticleContent(contentTextbox);
+			// 保存 Bundle
+			Bundle extras = new Bundle();
+			workMode.saveArticle(extras);
+			// 记录 bundle 到 Intent
+			intent.putExtra(SAVE_BUNDLE, extras);
+			// 跳转 Activity
 			startActivity(intent);
 		}
 	};
 		setContentView(R.layout.edit_note);
 		findViews();
 		setListeners();
+		initWorkMode();
 	}
 
 	/**
 	private void setListeners() {
 		savingButton.setOnClickListener(savingButtonListener);
 	}
+
+	/**
+	 * 初始化工作模式
+	 */
+	private void initWorkMode() {
+		Intent intent = getIntent();
+		Bundle extras = intent.getExtras();
+		Integer savedNoteId = (Integer) extras.get("noteId");
+		
+		// 设置工作模式
+		if (savedNoteId == null) {
+			workMode = EditNoteWorkMode.getNewEditMode();
+		} else {
+			workMode = EditNoteWorkMode.getExistedEditMode(savedNoteId);
+		}
+
+		// 初始化正文内容
+		workMode.displayArticleContent(contentTextbox);
+	}
 }

File Project/src/com/noodle/note/activity/EditNoteWorkMode.java

+package com.noodle.note.activity;
+
+import android.os.Bundle;
+import android.widget.TextView;
+
+import com.noodle.note.model.Note;
+import com.noodle.note.persistent.identitymap.Registry;
+import com.noodle.note.persistent.unitofwork.Session;
+
+/**
+ * 编辑笔记时的工作模式
+ * 
+ * @author TonySeek
+ * 
+ */
+abstract public class EditNoteWorkMode {
+	protected final static String BUNDLE_KEY = "EditNoteWorkMode";
+	protected final static String CLASS_TYPE = "EditNoteWorkMode_CLASS_TYPE";
+
+	/**
+	 * 笔记对象
+	 */
+	protected Note note;
+	
+	/**
+	 * 工厂方法
+	 */
+	public static EditNoteWorkMode getNewEditMode() {
+		return new NewEditMode();
+	}
+
+	/**
+	 * 工厂方法
+	 */
+	public static EditNoteWorkMode getExistedEditMode(int id) {
+		return new ExistedEditMode(id);
+	}
+
+	/**
+	 * 工厂方法
+	 */
+	public static EditNoteWorkMode getEditMode(Bundle extras) {
+		String type = extras.getString(CLASS_TYPE);
+		if (type == NewEditMode.class.toString()) {
+			return new NewEditMode(extras);
+		} else if (type == ExistedEditMode.class.toString()) {
+			return new ExistedEditMode(extras);
+		} else {
+			throw new RuntimeException("无效的 EditNoteWorkMode");
+		}
+	}
+
+	/**
+	 * 从 Bundle 构建
+	 */
+	public EditNoteWorkMode(Bundle extras) {
+	}
+
+	/**
+	 * 构造控对象
+	 */
+	public EditNoteWorkMode() {
+	}
+
+	/**
+	 * 获取笔记对象
+	 * 
+	 * @return 笔记对象
+	 */
+	abstract public Note getNote();
+	
+	/**
+	 * 保存笔记到数据库
+	 * 
+	 * @return 状态已改变的 WorkMode
+	 */
+	abstract public EditNoteWorkMode saveNote(Session session);
+	
+	/**
+	 * 更新视图层笔记正文内容到对象
+	 * 
+	 * @param 文本控件
+	 */
+	abstract public void displayArticleContent(TextView textView);
+
+	/**
+	 * 更新视图层显示的笔记正文内容
+	 * 
+	 * @param 文本控件
+	 */
+	abstract public void updateArticleContent(TextView textView);
+
+	/**
+	 * 将正文数据保存到 Boundle
+	 * 
+	 * @param extras
+	 */
+	public void saveArticle(Bundle extras) {
+		extras.putString(CLASS_TYPE, this.getClass().toString());
+	}
+}
+
+/**
+ * 新建笔记模式
+ * 
+ * @author TonySeek
+ * 
+ */
+class NewEditMode extends EditNoteWorkMode {
+	private String articleContent = "";
+
+	public NewEditMode(Bundle extras) {
+		super(extras);
+		articleContent = extras.getString(BUNDLE_KEY); // 从 bundle 中恢复
+	}
+
+	public NewEditMode() {
+		super();
+		this.articleContent = "";
+	}
+
+	@Override
+	public void displayArticleContent(TextView textView) {
+		textView.setText(articleContent);
+	}
+
+	@Override
+	public void updateArticleContent(TextView textView) {
+		this.articleContent = textView.getText().toString();
+	}
+
+	@Override
+	public void saveArticle(Bundle extras) {
+		super.saveArticle(extras);
+		extras.putString(BUNDLE_KEY, articleContent);
+	}
+
+	@Override
+	public Note getNote() {
+		if (null == null) {
+			note = new Note("", articleContent, "");
+		}
+		return note;
+	}
+
+	@Override
+	public EditNoteWorkMode saveNote(Session session) {
+		session.save(note);
+		return new ExistedEditMode(note.getId());
+	}
+}
+
+/**
+ * 已有笔记编辑模式
+ * 
+ * @author TonySeek
+ * 
+ */
+class ExistedEditMode extends EditNoteWorkMode {
+	/**
+	 * 根据 id 创建
+	 */
+	public ExistedEditMode(int id) {
+		Registry registry = Registry.getInstance();
+		this.note = registry.getEntity(Note.class, id);
+	}
+
+	/**
+	 * 根据 bundle 创建
+	 */
+	public ExistedEditMode(Bundle extras) {
+		int noteId = extras.getInt(BUNDLE_KEY);
+		Registry registry = Registry.getInstance();
+		this.note = registry.getEntity(Note.class, noteId);
+	}
+	
+	@Override
+	public Note getNote() {
+		return note;
+	}
+
+	@Override
+	public void displayArticleContent(TextView textView) {
+		textView.setText(note.getArticle());
+	}
+
+	@Override
+	public void updateArticleContent(TextView textView) {
+		String article = textView.getText().toString();
+		this.note.setArticle(article);
+	}
+
+	@Override
+	public void saveArticle(Bundle extras) {
+		super.saveArticle(extras);
+		extras.putInt(BUNDLE_KEY, note.getId());
+	}
+
+	@Override
+	public EditNoteWorkMode saveNote(Session session) {
+		// Do Nothing
+		// 工作单元会跟踪 Existed 笔记
+		return this;
+	}
+}

File Project/src/com/noodle/note/activity/SavingNoteActivity.java

  * 
  */
 public class SavingNoteActivity extends Activity {
+	/**
+	 * Note 笔记模型
+	 */
+	private Note note;
+
+	/**
+	 * 工作模式
+	 */
+	private EditNoteWorkMode workMode;
+
 	// 视图元素
 
 	/**
 	protected OnClickListener quickSaveListener = new OnClickListener() {
 		@Override
 		public void onClick(View v) {
-			save(getArticleText());
-			Toast.makeText(SavingNoteActivity.this, "保存成功", Toast.LENGTH_LONG).show();
+			String title = "快速保存的笔记";
+			String author = "佚名";
+			String[] tags = {};
+
+			saveNote(title, author, tags);
 		}
 	};
 
 	protected OnClickListener commonSaveListener = new OnClickListener() {
 		@Override
 		public void onClick(View v) {
+			final String TAG_SPLIT_PATTERN = "\b|,|,";
+			// 获取输入
 			String title = titleTextbox.getText().toString();
-			String article = getArticleText();
 			String author = authorTextbox.getText().toString();
-			String tagString = tagTextbox.getText().toString();
-			String[] tags = tagString.split("\b|,|,");
-
-			save(title, article, author, tags);
-			Toast.makeText(SavingNoteActivity.this, "保存成功", Toast.LENGTH_LONG).show();
+			String tagText = tagTextbox.getText().toString().trim();
+			String[] tags = tagText.split(TAG_SPLIT_PATTERN);
+			
+			saveNote(title, author, tags);
 		}
 	};
 
 		setContentView(R.layout.saving_note);
 		findViews();
 		setListeners();
+		findModel();
 	}
 
 	/**
 	}
 
 	/**
-	 * 普通保存笔记
-	 * 
-	 * @param title
-	 * @param article
-	 * @param author
-	 * @param tags
+	 * 获取传递来的模型
 	 */
-	private void save(String title, String article, String author, String[] tags) {
-		Note note = new Note(title, article, author);
-		Session session = Session.getInstance(this);
-		session.save(note);
-		session.flush();
+	private void findModel() {
+		Intent intent = getIntent();
+		Bundle extras = intent.getExtras();
+		workMode = EditNoteWorkMode.getEditMode(extras);
+		note = workMode.getNote();
 	}
 
 	/**
-	 * 快速保存笔记
+	 * 保存笔记
 	 */
-	private void save(String article) {
-		Note note = new Note("快速保存的笔记", article, "快速保存笔记的作者");
+	private void saveNote(String title, String author, String[] tags) {
+		Toast tips = new Toast(this);
+
+		// 验证输入
+		if (title == null || title.trim().length() == 0) {
+			tips.setText("请输入标题");
+			tips.show();
+		} else if (author == null || author.trim().length() == 0) {
+			tips.setText("请输入作者");
+			tips.show();
+		}
+
+		// TODO: 处理标签 
+
+		note.setTitle(title.trim());
+		note.setAuthor(author.trim());
+
+		// 同步到数据库
 		Session session = Session.getInstance(this);
-		session.save(note);
+		workMode = workMode.saveNote(session);
 		session.flush();
-		Intent intent = new Intent();
-		int id = note.getId();
-		Bundle bundle = new Bundle();
-		bundle.putInt("id", id);
-		intent.putExtras(bundle);
-		intent.setClass(SavingNoteActivity.this, NoteReadActivity.class);
+
+		// 提示
+		tips.setText("保存成功");
+		tips.show();
+		
+		// 跳转
+		Intent intent = new Intent(this, NoteReadActivity.class);
+		intent.putExtra("id", note.getId());
 		startActivity(intent);
 	}
-
-	/**
-	 * 获取编辑器中的文本正文
-	 * 
-	 * @return 文本正文
-	 */
-	private String getArticleText() {
-		Intent intent = getIntent();
-		Bundle extras = intent.getExtras();
-		String key = String.valueOf(R.id.editNoteTextbox);
-		String article = (String) extras.get(key);
-		return article;
-	}
-}
+}

File Project/src/com/noodle/note/model/Note.java

 	public void addTag(Tag tag) {
 		tags.add(tag);
 	}
+	
+	public void addTags(Set<Tag> tags) {
+		tags.addAll(tags);
+	}
 
 	public void addBookmark(Bookmark bookmark) {
 		bookmarks.add(bookmark);