/** Synchronize with server when data changes */ public void pushUpdateOnSave(Update update, ContentValues values) { if (!values.containsKey(Update.MESSAGE.name)) return; ArrayList<Object> params = new ArrayList<Object>(); params.add("message"); params.add(update.getValue(Update.MESSAGE)); if (update.getValue(Update.TAG) > 0) { TagData tagData = tagDataService.fetchById(update.getValue(Update.TAG), TagData.REMOTE_ID); if (tagData == null || tagData.getValue(TagData.REMOTE_ID) == 0) return; params.add("tag_id"); params.add(tagData.getValue(TagData.REMOTE_ID)); } if (update.getValue(Update.TASK) > 0) { params.add("task_id"); params.add(update.getValue(Update.TASK)); } if (!checkForToken()) return; try { params.add("token"); params.add(token); JSONObject result = actFmInvoker.invoke("comment_add", params.toArray(new Object[params.size()])); update.setValue(Update.REMOTE_ID, result.optLong("id")); updateDao.saveExisting(update); } catch (IOException e) { handleException("task-save", e); } }
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); synchronized (this) { if (dataLoaded) return; dataLoaded = true; } String tag = getIntent().getStringExtra(EXTRA_TAG_NAME); long remoteId = getIntent().getLongExtra(EXTRA_TAG_REMOTE_ID, 0); if (tag == null && remoteId == 0) return; TodorooCursor<TagData> cursor = tagDataService.query( Query.select(TagData.PROPERTIES) .where( Criterion.or( TagData.NAME.eq(tag), Criterion.and(TagData.REMOTE_ID.gt(0), TagData.REMOTE_ID.eq(remoteId))))); try { tagData = new TagData(); if (cursor.getCount() == 0) { tagData.setValue(TagData.NAME, tag); tagData.setValue(TagData.REMOTE_ID, remoteId); tagDataService.save(tagData); } else { cursor.moveToFirst(); tagData.readFromCursor(cursor); } } finally { cursor.close(); } String fetchKey = LAST_FETCH_KEY + tagData.getId(); long lastFetchDate = Preferences.getLong(fetchKey, 0); if (DateUtilities.now() > lastFetchDate + 300000L) { refreshData(false, false); Preferences.setLong(fetchKey, DateUtilities.now()); } setUpUpdateList(); setUpMemberPage(); }
@SuppressWarnings("nls") private void readTagData(JSONArray tags) throws JSONException { ArrayList<Metadata> metadata = new ArrayList<Metadata>(); for (int i = 0; i < tags.length(); i++) { JSONObject tagObject = tags.getJSONObject(i); TagData tagData = tagDataService.getTag(tagObject.getString("name"), TagData.ID); if (tagData == null) tagData = new TagData(); ActFmSyncService.JsonHelper.tagFromJson(tagObject, tagData); tagDataService.save(tagData); Metadata tagMeta = new Metadata(); tagMeta.setValue(Metadata.KEY, TagService.KEY); tagMeta.setValue(TagService.TAG, tagData.getValue(TagData.NAME)); tagMeta.setValue(TagService.REMOTE_ID, tagData.getValue(TagData.REMOTE_ID)); metadata.add(tagMeta); } metadataService.synchronizeMetadata( task.getId(), metadata, MetadataCriteria.withKey(TagService.KEY)); }
@Override public void loadTaskListContent(boolean requery) { super.loadTaskListContent(requery); int count = taskAdapter.getCursor().getCount(); if (tagData != null && sortFlags <= SortHelper.FLAG_REVERSE_SORT && count != tagData.getValue(TagData.TASK_COUNT)) { tagData.setValue(TagData.TASK_COUNT, count); tagDataService.save(tagData); } }
private void addTags(ArrayList<FilterListItem> list) { HashSet<String> tagNames = new HashSet<String>(); // active tags Tag[] myTags = tagService.getGroupedTags( TagService.GROUPED_TAGS_BY_SIZE, Criterion.and(TaskCriteria.ownedByMe(), TaskCriteria.activeAndVisible())); for (Tag tag : myTags) tagNames.add(tag.tag); if (myTags.length > 0) list.add(filterFromTags(myTags, R.string.tag_FEx_category_mine)); // find all tag data not in active tag list TodorooCursor<TagData> cursor = tagDataService.query( Query.select(TagData.NAME, TagData.TASK_COUNT, TagData.REMOTE_ID) .where(TagData.DELETION_DATE.eq(0))); ArrayList<Tag> notListed = new ArrayList<Tag>(); try { ArrayList<Tag> sharedTags = new ArrayList<Tag>(); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { String tagName = cursor.get(TagData.NAME); if (tagNames.contains(tagName)) continue; Tag tag = new Tag(tagName, cursor.get(TagData.TASK_COUNT), cursor.get(TagData.REMOTE_ID)); if (tag.count > 0) sharedTags.add(tag); else notListed.add(tag); tagNames.add(tagName); } if (sharedTags.size() > 0) list.add( filterFromTags( sharedTags.toArray(new Tag[sharedTags.size()]), R.string.tag_FEx_category_shared)); } finally { cursor.close(); } // find inactive tags, intersect tag list Tag[] inactiveTags = tagService.getGroupedTags( TagService.GROUPED_TAGS_BY_ALPHA, Criterion.and( TaskCriteria.notDeleted(), Criterion.not(TaskCriteria.activeAndVisible()))); for (Tag tag : inactiveTags) { if (!tagNames.contains(tag.tag) && !TextUtils.isEmpty(tag.tag)) { notListed.add(tag); tag.count = 0; } } if (notListed.size() > 0) list.add( filterFromTags( notListed.toArray(new Tag[notListed.size()]), R.string.tag_FEx_category_inactive)); }
/** * Get details for this tag * * @param tagData * @throws IOException * @throws JSONException */ public void fetchTag(final TagData tagData) throws IOException, JSONException { JSONObject result; if (!checkForToken()) return; if (tagData.getValue(TagData.REMOTE_ID) == 0) result = actFmInvoker.invoke("tag_show", "name", tagData.getValue(TagData.NAME), "token", token); else result = actFmInvoker.invoke( "tag_show", "id", tagData.getValue(TagData.REMOTE_ID), "token", token); JsonHelper.tagFromJson(result, tagData); Flags.set(Flags.SUPPRESS_SYNC); tagDataService.save(tagData); }
protected void setUpUpdateList() { TodorooCursor<Update> currentCursor = tagDataService.getUpdates(tagData); startManagingCursor(currentCursor); updateAdapter = new UpdateAdapter(this, R.layout.update_adapter_row, currentCursor, false, null); ((ListView) findViewById(R.id.tab_updates)).setAdapter(updateAdapter); final ImageButton quickAddButton = (ImageButton) findViewById(R.id.commentButton); addCommentField = (EditText) findViewById(R.id.commentField); addCommentField.setOnEditorActionListener( new OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_NULL && addCommentField.getText().length() > 0) { addComment(); return true; } return false; } }); addCommentField.addTextChangedListener( new TextWatcher() { @Override public void afterTextChanged(Editable s) { quickAddButton.setVisibility((s.length() > 0) ? View.VISIBLE : View.GONE); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // } }); quickAddButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { addComment(); } }); }
private void saveSettings() { String oldName = tagData.getValue(TagData.NAME); String newName = tagName.getText().toString(); if (!oldName.equals(newName)) { tagData.setValue(TagData.NAME, newName); TagService.getInstance().rename(oldName, newName); tagData.setFlag(TagData.FLAGS, TagData.FLAG_EMERGENT, false); } JSONArray members = tagMembers.toJSONArray(); tagData.setValue(TagData.MEMBERS, members.toString()); tagData.setValue(TagData.MEMBER_COUNT, members.length()); Flags.set(Flags.TOAST_ON_SAVE); tagDataService.save(tagData); refreshMembersPage(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.Theme_Dialog); tag = getIntent().getStringExtra(TAG); if (tag == null) { finish(); return; } DependencyInjectionService.getInstance().inject(this); TagData tagData = tagDataService.getTag(tag, TagData.MEMBER_COUNT); if (tagData != null && tagData.getValue(TagData.MEMBER_COUNT) > 0) { DialogUtilities.okDialog( this, getString(R.string.actfm_tag_operation_disabled), getCancelListener()); return; } showDialog(); }
/** * Send tagData changes to server * * @param setValues */ public void pushTagDataOnSave(TagData tagData, ContentValues values) { long remoteId; if (tagData.containsValue(TagData.REMOTE_ID)) remoteId = tagData.getValue(TagData.REMOTE_ID); else { TagData forRemote = tagDataService.fetchById(tagData.getId(), TagData.REMOTE_ID); if (forRemote == null) return; remoteId = forRemote.getValue(TagData.REMOTE_ID); } boolean newlyCreated = remoteId == 0; ArrayList<Object> params = new ArrayList<Object>(); if (values.containsKey(TagData.NAME.name)) { params.add("name"); params.add(tagData.getValue(TagData.NAME)); } if (values.containsKey(TagData.MEMBERS.name)) { params.add("members"); try { JSONArray members = new JSONArray(tagData.getValue(TagData.MEMBERS)); if (members.length() == 0) params.add(""); else { ArrayList<Object> array = new ArrayList<Object>(members.length()); for (int i = 0; i < members.length(); i++) { JSONObject person = members.getJSONObject(i); if (person.has("id")) array.add(person.getLong("id")); else { if (person.has("name")) array.add(person.getString("name") + " <" + person.getString("email") + ">"); else array.add(person.getString("email")); } } params.add(array); } } catch (JSONException e) { throw new RuntimeException(e); } } if (params.size() == 0 || !checkForToken()) return; if (!newlyCreated) { params.add("id"); params.add(remoteId); } boolean success; try { params.add("token"); params.add(token); JSONObject result = actFmInvoker.invoke("tag_save", params.toArray(new Object[params.size()])); if (newlyCreated) { tagData.setValue(TagData.REMOTE_ID, result.optLong("id")); Flags.set(Flags.SUPPRESS_SYNC); tagDataDao.saveExisting(tagData); } success = true; } catch (IOException e) { handleException("tag-save", e); success = false; } if (!Flags.checkAndClear(Flags.TOAST_ON_SAVE)) return; final boolean finalSuccess = success; Handler handler = new Handler(Looper.getMainLooper()); handler.post( new Runnable() { @Override public void run() { if (finalSuccess) Toast.makeText( ContextManager.getContext(), R.string.actfm_toast_success, Toast.LENGTH_LONG) .show(); else Toast.makeText( ContextManager.getContext(), R.string.actfm_toast_error, Toast.LENGTH_LONG) .show(); } }); }