ui-笔记项数据模型类

pull/3/head
weichunyi 2 months ago
parent 35994ace47
commit 67e5726260

@ -1,17 +1,15 @@
/* /*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Apache 2.0"许可证"
* 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 * 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 net.micode.notes.ui; package net.micode.notes.ui;
@ -26,57 +24,73 @@ import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils; import net.micode.notes.tool.DataUtils;
/**
*
*
* 访
*/
public class NoteItemData { public class NoteItemData {
static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 数据库查询投影(指定要查询的列)
NoteColumns.ALERTED_DATE, static final String[] PROJECTION = new String[]{
NoteColumns.BG_COLOR_ID, NoteColumns.ID, // 笔记/文件夹ID
NoteColumns.CREATED_DATE, NoteColumns.ALERTED_DATE, // 提醒时间
NoteColumns.HAS_ATTACHMENT, NoteColumns.BG_COLOR_ID, // 背景颜色ID
NoteColumns.MODIFIED_DATE, NoteColumns.CREATED_DATE, // 创建时间
NoteColumns.NOTES_COUNT, NoteColumns.HAS_ATTACHMENT, // 是否有附件0/1
NoteColumns.PARENT_ID, NoteColumns.MODIFIED_DATE, // 修改时间
NoteColumns.SNIPPET, NoteColumns.NOTES_COUNT, // 子笔记数量(仅文件夹有效)
NoteColumns.TYPE, NoteColumns.PARENT_ID, // 父文件夹ID
NoteColumns.WIDGET_ID, NoteColumns.SNIPPET, // 笔记摘要
NoteColumns.WIDGET_TYPE, NoteColumns.TYPE, // 类型(笔记/文件夹/系统类型等)
NoteColumns.WIDGET_ID, // 小部件ID
NoteColumns.WIDGET_TYPE, // 小部件类型
}; };
private static final int ID_COLUMN = 0; // 投影列索引常量(提高代码可读性)
private static final int ALERTED_DATE_COLUMN = 1; private static final int ID_COLUMN = 0;
private static final int BG_COLOR_ID_COLUMN = 2; private static final int ALERTED_DATE_COLUMN = 1;
private static final int CREATED_DATE_COLUMN = 3; private static final int BG_COLOR_ID_COLUMN = 2;
private static final int HAS_ATTACHMENT_COLUMN = 4; private static final int CREATED_DATE_COLUMN = 3;
private static final int MODIFIED_DATE_COLUMN = 5; private static final int HAS_ATTACHMENT_COLUMN = 4;
private static final int NOTES_COUNT_COLUMN = 6; private static final int MODIFIED_DATE_COLUMN = 5;
private static final int PARENT_ID_COLUMN = 7; private static final int NOTES_COUNT_COLUMN = 6;
private static final int SNIPPET_COLUMN = 8; private static final int PARENT_ID_COLUMN = 7;
private static final int TYPE_COLUMN = 9; private static final int SNIPPET_COLUMN = 8;
private static final int WIDGET_ID_COLUMN = 10; private static final int TYPE_COLUMN = 9;
private static final int WIDGET_TYPE_COLUMN = 11; private static final int WIDGET_ID_COLUMN = 10;
private static final int WIDGET_TYPE_COLUMN = 11;
private long mId;
private long mAlertDate; // 数据字段(对应数据库列)
private int mBgColorId; private long mId; // 笔记/文件夹ID
private long mCreatedDate; private long mAlertDate; // 提醒时间
private boolean mHasAttachment; private int mBgColorId; // 背景颜色ID
private long mModifiedDate; private long mCreatedDate; // 创建时间
private int mNotesCount; private boolean mHasAttachment; // 是否有附件
private long mParentId; private long mModifiedDate; // 修改时间
private String mSnippet; private int mNotesCount; // 子笔记数量
private int mType; private long mParentId; // 父文件夹ID
private int mWidgetId; private String mSnippet; // 笔记摘要(去除勾选标记)
private int mWidgetType; private int mType; // 类型Notes.TYPE_*常量)
private String mName; private int mWidgetId; // 小部件ID
private String mPhoneNumber; private int mWidgetType; // 小部件类型2x或4x
private String mName; // 联系人姓名(通话记录笔记专用)
private boolean mIsLastItem; private String mPhoneNumber; // 电话号码(通话记录笔记专用)
private boolean mIsFirstItem;
private boolean mIsOnlyOneItem; // 位置相关标志位(用于界面显示逻辑)
private boolean mIsOneNoteFollowingFolder; private boolean mIsLastItem; // 是否是列表中的最后一项
private boolean mIsMultiNotesFollowingFolder; private boolean mIsFirstItem; // 是否是列表中的第一项
private boolean mIsOnlyOneItem; // 是否是列表中唯一一项
private boolean mIsOneNoteFollowingFolder;// 是否是文件夹后的单个笔记
private boolean mIsMultiNotesFollowingFolder;// 是否是文件夹后的多个笔记
/**
* Cursor
* @param context
* @param cursor Cursor
*/
public NoteItemData(Context context, Cursor cursor) { public NoteItemData(Context context, Cursor cursor) {
// 从Cursor中读取各列数据并初始化字段
mId = cursor.getLong(ID_COLUMN); mId = cursor.getLong(ID_COLUMN);
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
@ -86,6 +100,7 @@ public class NoteItemData {
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
mParentId = cursor.getLong(PARENT_ID_COLUMN); mParentId = cursor.getLong(PARENT_ID_COLUMN);
mSnippet = cursor.getString(SNIPPET_COLUMN); mSnippet = cursor.getString(SNIPPET_COLUMN);
// 去除摘要中的勾选标记(适配列表模式)
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
NoteEditActivity.TAG_UNCHECKED, ""); NoteEditActivity.TAG_UNCHECKED, "");
mType = cursor.getInt(TYPE_COLUMN); mType = cursor.getInt(TYPE_COLUMN);
@ -93,12 +108,13 @@ public class NoteItemData {
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
mPhoneNumber = ""; mPhoneNumber = "";
// 处理通话记录笔记:获取电话号码和联系人姓名
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
if (!TextUtils.isEmpty(mPhoneNumber)) { if (!TextUtils.isEmpty(mPhoneNumber)) {
mName = Contact.getContact(context, mPhoneNumber); mName = Contact.getContact(context, mPhoneNumber); // 从联系人获取姓名
if (mName == null) { if (mName == null) {
mName = mPhoneNumber; mName = mPhoneNumber; // 无姓名时使用电话号码
} }
} }
} }
@ -106,27 +122,35 @@ public class NoteItemData {
if (mName == null) { if (mName == null) {
mName = ""; mName = "";
} }
checkPostion(cursor); checkPostion(cursor); // 检查当前项在列表中的位置状态
} }
/**
* Cursor//
* @param cursor Cursor
*/
private void checkPostion(Cursor cursor) { private void checkPostion(Cursor cursor) {
mIsLastItem = cursor.isLast() ? true : false; mIsLastItem = cursor.isLast() ? true : false;
mIsFirstItem = cursor.isFirst() ? true : false; mIsFirstItem = cursor.isFirst() ? true : false;
mIsOnlyOneItem = (cursor.getCount() == 1); mIsOnlyOneItem = (cursor.getCount() == 1); // 是否仅有一条数据
mIsMultiNotesFollowingFolder = false; mIsMultiNotesFollowingFolder = false;
mIsOneNoteFollowingFolder = false; mIsOneNoteFollowingFolder = false;
// 处理笔记类型且非首项的情况:判断是否跟随在文件夹之后
if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition(); int position = cursor.getPosition();
if (cursor.moveToPrevious()) { if (cursor.moveToPrevious()) { // 移动到前一项
// 前一项是文件夹或系统类型
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) { || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
// 判断后续是否有多个笔记
if (cursor.getCount() > (position + 1)) { if (cursor.getCount() > (position + 1)) {
mIsMultiNotesFollowingFolder = true; mIsMultiNotesFollowingFolder = true;
} else { } else {
mIsOneNoteFollowingFolder = true; mIsOneNoteFollowingFolder = true;
} }
} }
// 恢复Cursor位置
if (!cursor.moveToNext()) { if (!cursor.moveToNext()) {
throw new IllegalStateException("cursor move to previous but can't move back"); throw new IllegalStateException("cursor move to previous but can't move back");
} }
@ -134,91 +158,99 @@ public class NoteItemData {
} }
} }
// 以下为各数据字段的访问方法getter和状态判断方法
public boolean isOneFollowingFolder() { public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder; return mIsOneNoteFollowingFolder; // 是否是文件夹后的单个笔记
} }
public boolean isMultiFollowingFolder() { public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder; return mIsMultiNotesFollowingFolder; // 是否是文件夹后的多个笔记
} }
public boolean isLast() { public boolean isLast() {
return mIsLastItem; return mIsLastItem; // 是否是列表最后一项
} }
public String getCallName() { public String getCallName() {
return mName; return mName; // 获取通话记录的联系人姓名
} }
public boolean isFirst() { public boolean isFirst() {
return mIsFirstItem; return mIsFirstItem; // 是否是列表首项
} }
public boolean isSingle() { public boolean isSingle() {
return mIsOnlyOneItem; return mIsOnlyOneItem; // 是否是列表唯一项
} }
public long getId() { public long getId() {
return mId; return mId; // 获取ID
} }
public long getAlertDate() { public long getAlertDate() {
return mAlertDate; return mAlertDate; // 获取提醒时间
} }
public long getCreatedDate() { public long getCreatedDate() {
return mCreatedDate; return mCreatedDate; // 获取创建时间
} }
public boolean hasAttachment() { public boolean hasAttachment() {
return mHasAttachment; return mHasAttachment; // 是否有附件
} }
public long getModifiedDate() { public long getModifiedDate() {
return mModifiedDate; return mModifiedDate; // 获取修改时间
} }
public int getBgColorId() { public int getBgColorId() {
return mBgColorId; return mBgColorId; // 获取背景颜色ID
} }
public long getParentId() { public long getParentId() {
return mParentId; return mParentId; // 获取父文件夹ID
} }
public int getNotesCount() { public int getNotesCount() {
return mNotesCount; return mNotesCount; // 获取子笔记数量(文件夹专用)
} }
public long getFolderId () { public long getFolderId() {
return mParentId; return mParentId; // 获取文件夹ID与父ID一致
} }
public int getType() { public int getType() {
return mType; return mType; // 获取类型(笔记/文件夹等)
} }
public int getWidgetType() { public int getWidgetType() {
return mWidgetType; return mWidgetType; // 获取小部件类型
} }
public int getWidgetId() { public int getWidgetId() {
return mWidgetId; return mWidgetId; // 获取小部件ID
} }
public String getSnippet() { public String getSnippet() {
return mSnippet; return mSnippet; // 获取笔记摘要
} }
public boolean hasAlert() { public boolean hasAlert() {
return (mAlertDate > 0); return (mAlertDate > 0); // 是否设置了提醒
} }
public boolean isCallRecord() { public boolean isCallRecord() {
// 是否是通话记录笔记(父文件夹为通话记录文件夹且有电话号码)
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
} }
/**
* Cursor
* @param cursor Cursor
* @return Notes.TYPE_*
*/
public static int getNoteType(Cursor cursor) { public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN); return cursor.getInt(TYPE_COLUMN);
} }
} }
Loading…
Cancel
Save