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