use of com.orgzly.org.datetime.OrgDateTime in project orgzly-android by orgzly.
the class DatabaseMigration method addAndSetCreatedAt.
private static void addAndSetCreatedAt(SQLiteDatabase db, Context context) {
db.execSQL("ALTER TABLE notes ADD COLUMN created_at INTEGER DEFAULT 0");
if (!AppPreferences.createdAt(context)) {
return;
}
String createdAtPropName = AppPreferences.createdAtProperty(context);
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables("note_properties" + " JOIN notes ON (notes._id = note_properties.note_id)" + " JOIN properties ON (properties._id = note_properties.property_id)" + " JOIN property_names ON (property_names._id = properties.name_id)" + " JOIN property_values ON (property_values._id = properties.value_id)");
queryBuilder.setDistinct(true);
try (Cursor cursor = queryBuilder.query(db, new String[] { "notes._id AS note_id", "property_values.value AS value" }, "note_id IS NOT NULL AND name IS NOT NULL AND value IS NOT NULL AND name = ?", new String[] { createdAtPropName }, null, null, null)) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
long noteId = cursor.getLong(0);
String propValue = cursor.getString(1);
OrgDateTime dateTime = OrgDateTime.parseOrNull(propValue);
if (dateTime != null) {
long millis = dateTime.getCalendar().getTimeInMillis();
ContentValues values = new ContentValues();
values.put("created_at", millis);
db.update("notes", values, "_id = " + noteId, null);
}
}
}
}
use of com.orgzly.org.datetime.OrgDateTime in project orgzly-android by orgzly.
the class DatabaseMigration method migrateOrgTimestamps.
private static void migrateOrgTimestamps(SQLiteDatabase db) {
db.execSQL("ALTER TABLE org_timestamps RENAME TO org_timestamps_prev");
for (String sql : DbOrgTimestamp.CREATE_SQL) db.execSQL(sql);
Cursor cursor = db.query("org_timestamps_prev", new String[] { "_id", "string" }, null, null, null, null, null);
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
long id = cursor.getLong(0);
String string = cursor.getString(1);
OrgDateTime orgDateTime = OrgDateTime.parse(string);
ContentValues values = new ContentValues();
values.put("_id", id);
DbOrgTimestamp.toContentValues(values, orgDateTime);
db.insert("org_timestamps", null, values);
}
} finally {
cursor.close();
}
db.execSQL("DROP TABLE org_timestamps_prev");
}
use of com.orgzly.org.datetime.OrgDateTime in project orgzly-android by orgzly.
the class TimestampDialogFragment method onCreateDialog.
/**
* Create a new instance of a dialog.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (BuildConfig.LOG_DEBUG)
LogUtils.d(TAG, savedInstanceState);
mUserTimeFormatter = new UserTimeFormatter(getActivity());
/* This makes sure that the fragment has implemented
* the callback interface. If not, it throws an exception
*/
if (!(getTargetFragment() instanceof OnDateTimeSetListener)) {
throw new IllegalStateException("Fragment " + getTargetFragment() + " must implement " + OnDateTimeSetListener.class);
}
mActivityListener = (OnDateTimeSetListener) getTargetFragment();
mContext = getActivity();
mId = getArguments().getInt(ARG_DIALOG_ID);
mNoteIds = new TreeSet<>();
for (long e : getArguments().getLongArray(ARG_NOTE_IDS)) {
mNoteIds.add(e);
}
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_timestamp, null, false);
mDatePicker = (Button) view.findViewById(R.id.dialog_timestamp_date_picker);
mTimePicker = (Button) view.findViewById(R.id.dialog_timestamp_time_picker);
mIsTimeUsed = (CompoundButton) view.findViewById(R.id.dialog_timestamp_time_check);
mRepeaterPicker = (Button) view.findViewById(R.id.dialog_timestamp_repeater_picker);
mIsRepeaterUsed = (CompoundButton) view.findViewById(R.id.dialog_timestamp_repeater_check);
/* Set before toggle buttons are setup, as they trigger dialog title update .*/
setValues(OrgDateTime.parseOrNull(getArguments().getString(ARG_TIME)));
mDialog = new AlertDialog.Builder(mContext).setTitle(getArguments().getInt(ARG_TITLE)).setView(view).setPositiveButton(R.string.set, (dialog, which) -> {
if (mActivityListener != null) {
OrgDateTime time = getCurrentOrgTime();
mActivityListener.onDateTimeSet(mId, mNoteIds, time);
}
}).setNeutralButton(R.string.clear, (dialog, which) -> {
if (mActivityListener != null) {
mActivityListener.onDateTimeCleared(mId, mNoteIds);
}
}).setNegativeButton(R.string.cancel, (dialog, which) -> {
if (mActivityListener != null) {
mActivityListener.onDateTimeAborted(mId, mNoteIds);
}
}).create();
mDatePicker.setOnClickListener(this);
mTimePicker.setOnClickListener(this);
mRepeaterPicker.setOnClickListener(this);
/*
* These callbacks are called not only on user press, but during initialization as well.
* It's important that they are *after* dialog has been created.
*/
mIsTimeUsed.setOnCheckedChangeListener(this);
mIsRepeaterUsed.setOnCheckedChangeListener(this);
view.findViewById(R.id.dialog_timestamp_today_shortcut).setOnClickListener(this);
view.findViewById(R.id.dialog_timestamp_tomorrow_shortcut).setOnClickListener(this);
view.findViewById(R.id.dialog_timestamp_next_week_shortcut).setOnClickListener(this);
view.findViewById(R.id.dialog_timestamp_time_icon).setOnClickListener(this);
view.findViewById(R.id.dialog_timestamp_repeater_icon).setOnClickListener(this);
restoreState(savedInstanceState);
setViewsFromCurrentValues();
return mDialog;
}
use of com.orgzly.org.datetime.OrgDateTime in project orgzly-android by orgzly.
the class TimestampDialogFragment method getCurrentOrgTime.
private OrgDateTime getCurrentOrgTime() {
OrgDateTime.Builder builder = new OrgDateTime.Builder().setIsActive(true).setYear(mCurrentYear).setMonth(mCurrentMonth).setHasTime(mIsTimeUsed.isChecked()).setDay(mCurrentDay).setHour(mCurrentHour).setMinute(mCurrentMinute);
if (mIsRepeaterUsed.isChecked()) {
OrgRepeater repeater = OrgRepeater.parse(mRepeaterPicker.getText().toString());
builder.setHasRepeater(true);
builder.setRepeater(repeater);
}
return builder.build();
}
use of com.orgzly.org.datetime.OrgDateTime in project orgzly-android by orgzly.
the class TimestampDialogFragment method setViewsFromCurrentValues.
private void setViewsFromCurrentValues() {
/*
* Check if dialog has been created.
* Toggle buttons get fired on initialization, calling this method.
* Should not happen before with the current ordering of setup methods, but just in case.
*/
if (mDialog != null) {
OrgDateTime time = getCurrentOrgTime();
mDatePicker.setText(mUserTimeFormatter.formatDate(time));
mTimePicker.setText(mUserTimeFormatter.formatTime(time));
if (time.hasRepeater()) {
mRepeaterPicker.setText(mUserTimeFormatter.formatRepeater(time));
}
// mEndTimePicker.setText(mOrgTimeFormatter.formatEndTime(time));
mDialog.setTitle(mUserTimeFormatter.formatAll(time));
}
}
Aggregations