public ItemTreeItemAdapter(
      final ItemListView itemListView,
      final IOnItemCompleted onItemCompleted,
      final IOnItemNotes onItemNotes,
      final TreeStateManager<Item> treeStateManager,
      final int numberOfLevels) {
    super(itemListView, treeStateManager, numberOfLevels);

    mClient = itemListView.getClient();
    mStorage = mClient.getStorage();

    mOnItemCompleted = onItemCompleted;
    mOnItemNotes = onItemNotes;

    mItemListView = itemListView;
    mItemViewMode = mItemListView.getViewMode();
    mItemViewInQueryMode = mItemListView.getItemViewInQueryMode();
  }
Exemple #2
0
  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mApplication = (TodoistApplication) getApplication();
    mClient = mApplication.getClient();
    mStorage = mClient.getStorage();

    // Initialize the sync service in case it wasn't started in boot (i.e. when application is
    // first installed, and before first boot)
    OnBootReceiver.startRepeatingService(this);

    if ((mClient.hasNeverLoggedIn()) || (mClient.hasNeverSynced())) {
      // Never logged in before - Show a login/register screen
      Intent intent = new Intent(getBaseContext(), LoginOrRegisterView.class);
      startActivity(intent);

    } else {

      if ((mStorage.getSyncOnStartup()) && (!mClient.isCurrentlySyncing())) {
        // Sync on startup (using another thread)
        (new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    try {
                      mClient.login();
                      mClient.syncAll(null);
                    } catch (TodoistServerException e) {
                      // Login/sync failed
                      e.printStackTrace();
                    }
                  }
                }))
            .start();
      }

      // Show the initial view, according to the preferences set by the user

      InitialView initialView = mStorage.getInitialView();
      Intent intent = null;

      if ((initialView == InitialView.LAST_VIEWED_PROJECT)
          && (mStorage.getLastViewedProject() == 0)) {
        // No last viewed project - default to filter by projects view
        initialView = InitialView.FILTER_BY_PROJECTS;
      } else if ((initialView == InitialView.LAST_VIEWED_LABEL)
          && (mStorage.getLastViewedLabel() == 0)) {
        // No last viewed label - default to filter by labels view
        initialView = InitialView.FILTER_BY_LABELS;
      } else if (initialView == InitialView.FILTER_BY_PROJECTS_OR_LABELS_OR_QUERIES) {
        // Use last viewed filter mode (by projects or labels or queries)
        initialView = mStorage.getLastViewedFilter();
      }

      switch (initialView) {
        case FILTER_BY_LABELS:
          intent = new Intent(getBaseContext(), LabelListView.class);
          intent.putExtra(LabelListView.KEY__VIEW_MODE, LabelViewMode.FILTER_BY_LABELS.toString());
          break;

        case FILTER_BY_PROJECTS:
          intent = new Intent(getBaseContext(), ProjectListView.class);
          intent.putExtra(
              ProjectListView.KEY__VIEW_MODE, ProjectViewMode.FILTER_BY_PROJECTS.toString());
          break;

        case FILTER_BY_QUERIES:
          intent = new Intent(getBaseContext(), QueryListView.class);
          intent.putExtra(QueryListView.KEY__VIEW_MODE, QueryViewMode.FILTER_BY_QUERIES.toString());
          break;

        case LAST_VIEWED_LABEL:
        case SPECIFIC_LABEL:
          int labelId = 0;
          if (initialView == InitialView.LAST_VIEWED_LABEL) labelId = mStorage.getLastViewedLabel();
          else if (initialView == InitialView.SPECIFIC_LABEL) labelId = mStorage.getInitialLabel();

          Label label = mStorage.getLabel(labelId);

          if (label == null) {
            // Special case - label was deleted - select the first available label
            ArrayList<Label> labels = mStorage.getLabels();

            if (labels.size() == 0) {
              // No labels available at all - revert to filter by projects
              mStorage.setInitialView(InitialView.FILTER_BY_PROJECTS);

              intent = new Intent(getBaseContext(), ProjectListView.class);
              intent.putExtra(
                  ProjectListView.KEY__VIEW_MODE, ProjectViewMode.FILTER_BY_PROJECTS.toString());
              break;
            }

            label = labels.get(0);

            // Fix this issue in the storage
            if (initialView == InitialView.LAST_VIEWED_LABEL) mStorage.setLastViewedLabel(label.id);
            else if (initialView == InitialView.SPECIFIC_LABEL) mStorage.setInitialLabel(label.id);
          }

          intent = new Intent(getBaseContext(), ItemListView.class);
          intent.putExtra(
              ItemListView.KEY__VIEW_MODE, ItemListView.ItemViewMode.FILTER_BY_LABELS.toString());
          intent.putExtra(ItemListView.KEY__LABEL, (Serializable) label);
          break;

        case LAST_VIEWED_PROJECT:
        case SPECIFIC_PROJECT:
          int projectId = 0;
          if (initialView == InitialView.LAST_VIEWED_PROJECT)
            projectId = mStorage.getLastViewedProject();
          else if (initialView == InitialView.SPECIFIC_PROJECT)
            projectId = mStorage.getInitialProject();

          Project project = mStorage.getProject(projectId);

          if (project == null) {
            // Special case - project was deleted - select the first available project
            ArrayList<Project> projects = mStorage.getProjects();

            if (projects.size() == 0) {
              // No projects available at all - revert to filter by projects
              mStorage.setInitialView(InitialView.FILTER_BY_PROJECTS);

              intent = new Intent(getBaseContext(), ProjectListView.class);
              intent.putExtra(
                  ProjectListView.KEY__VIEW_MODE, ProjectViewMode.FILTER_BY_PROJECTS.toString());
              break;
            }

            project = projects.get(0);

            // Fix this issue in the storage
            if (initialView == InitialView.LAST_VIEWED_PROJECT)
              mStorage.setLastViewedProject(project.id);
            else if (initialView == InitialView.SPECIFIC_PROJECT)
              mStorage.setInitialProject(project.id);
          }

          intent = new Intent(getBaseContext(), ItemListView.class);
          intent.putExtra(
              ItemListView.KEY__VIEW_MODE, ItemListView.ItemViewMode.FILTER_BY_PROJECTS.toString());
          intent.putExtra(ItemListView.KEY__PROJECT, (Serializable) project);
          break;

        case LAST_VIEWED_QUERY:
        case SPECIFIC_QUERY:
          int queryId = 0;
          if (initialView == InitialView.LAST_VIEWED_QUERY) queryId = mStorage.getLastViewedQuery();
          else if (initialView == InitialView.SPECIFIC_QUERY) queryId = mStorage.getInitialQuery();

          Query query = mStorage.getQuery(queryId);

          if (query == null) {
            // Special case - query was deleted - select the first available query
            ArrayList<Query> queries = mStorage.getQueries();

            if (queries.size() == 0) {
              // No queries available at all - revert to filter by projects
              mStorage.setInitialView(InitialView.FILTER_BY_PROJECTS);

              intent = new Intent(getBaseContext(), ProjectListView.class);
              intent.putExtra(
                  ProjectListView.KEY__VIEW_MODE, ProjectViewMode.FILTER_BY_PROJECTS.toString());
              break;
            }

            query = queries.get(0);

            // Fix this issue in the storage
            if (initialView == InitialView.LAST_VIEWED_QUERY) mStorage.setLastViewedQuery(query.id);
            else if (initialView == InitialView.SPECIFIC_QUERY) mStorage.setInitialQuery(query.id);
          }

          intent = new Intent(getBaseContext(), ItemListView.class);
          intent.putExtra(
              ItemListView.KEY__VIEW_MODE, ItemListView.ItemViewMode.FILTER_BY_QUERIES.toString());
          intent.putExtra(ItemListView.KEY__QUERY, (Serializable) query);
          break;
      }

      startActivity(intent);
    }

    // Close this view, since it shouldn't be visible to the user (he shouldn't be able to
    // press back and reach this activity)
    finish();
  }
  @Override
  public View updateView(final View view, final TreeNodeInfo<Item> treeNodeInfo) {
    final Item item = treeNodeInfo.getId();
    LinearLayout viewLayout = (LinearLayout) view;

    TextView itemContent = (TextView) viewLayout.findViewById(R.id.item_list_item_content);
    TextView itemLabels = (TextView) viewLayout.findViewById(R.id.item_list_item_labels);
    ImageView itemNotes = (ImageView) viewLayout.findViewById(R.id.item_list_item_notes);
    TextView itemNoteCount = (TextView) viewLayout.findViewById(R.id.item_list_item_note_count);
    ImageView itemRepeat = (ImageView) viewLayout.findViewById(R.id.item_list_item_repeat);
    TextView itemDueDate = (TextView) viewLayout.findViewById(R.id.item_list_item_due_date);
    LinearLayout itemDateLayout =
        (LinearLayout) viewLayout.findViewById(R.id.item_list_item_date_layout);
    final CheckBox itemCheckbox = (CheckBox) viewLayout.findViewById(R.id.item_list_item_checkbox);

    mTextSize = mStorage.getTextSize();
    mItemViewInQueryMode = mItemListView.getItemViewInQueryMode();

    // Display the formatted text (highlighting, etc)
    itemContent.setText(TodoistTextFormatter.formatText(item.getContent()));

    itemContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
    // Linkify any emails, web addresses and phone numbers in the item's content
    Linkify.addLinks(itemContent, Linkify.ALL);

    itemLabels.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize - 4);

    if ((mItemViewMode == ItemViewMode.FILTER_BY_LABELS)
        || ((mItemViewMode == ItemViewMode.FILTER_BY_QUERIES)
            && (mItemViewInQueryMode == ItemViewInQueryMode.PROJECTS))) {
      // Show item's projects
      Project project = mClient.getProjectById(item.projectId);

      if (project != null) {
        itemLabels.setText(project.getName());
        itemLabels.setBackgroundColor((0xFF << 24) | project.getColor());
      } else {
        // Rare case that shouldn't happen - item is pointing to an old project that no longer
        // exists
        itemLabels.setText("");
        itemLabels.setBackgroundColor((0xFF << 24) | Color.WHITE);
      }

    } else {
      itemLabels.setText("");
      itemLabels.setBackgroundColor((0x00 << 24)); // Transparent background color

      // Show item's labels
      if (item.labelIds != null) {
        // Fill out the labels
        for (int i = 0; i < item.labelIds.size(); i++) {
          int labelId = item.labelIds.get(i);
          Label currentLabel = mIdToLabels.get(labelId);

          if (currentLabel == null) {
            // Weird case when we have an ID but no matching label for it
            // TODO: What should we do other than this?
            continue;
          }

          itemLabels.append(
              Html.fromHtml(
                  String.format(
                      "<font color='#%X'><i>%s</i></font>",
                      currentLabel.getColor(), currentLabel.name)));

          if (i < item.labelIds.size() - 1) {
            itemLabels.append(", ");
          }
        }

        // Since Italic text gets cut off on "wrap_contents" TextView width
        itemLabels.append(" ");
      }
    }

    if (mClient.isPremium()) {
      itemNoteCount.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize - 7);
      if (item.noteCount > 0) {
        itemNotes.setVisibility(View.VISIBLE);
        itemNoteCount.setText(String.valueOf(item.noteCount));
        itemNoteCount.setVisibility(View.VISIBLE);
      } else {
        // No notes (don't show an icon)
        itemNotes.setVisibility(View.GONE);
        itemNoteCount.setVisibility(View.GONE);
      }

      itemNotes.setTag(item);
      itemNotes.setOnClickListener(this);

    } else {
      // Only premium users have task notes
      itemNotes.setVisibility(View.GONE);
      itemNoteCount.setVisibility(View.GONE);
    }

    // Show a "recurring" image for item if necessary
    if (item.isRecurring()) {
      itemRepeat.setVisibility(View.VISIBLE);
    } else {
      itemRepeat.setVisibility(View.GONE);
    }

    itemDueDate.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize - 4);

    if ((item.dueDate == null) || (item.dueDate.getTime() == 0)) {
      itemDateLayout.setVisibility(View.GONE);
    } else {
      itemDateLayout.setVisibility(View.VISIBLE);
      itemDueDate.setText(
          item.getDueDateDescription(
              mClient.getUser().timeFormat, mClient.getUser().timezoneOffsetMinutes));
      itemDateLayout.setBackgroundColor((0xFF << 24) | item.getDueDateColor());
    }

    itemCheckbox.setTag(item);
    itemCheckbox.setOnCheckedChangeListener(null);
    itemCheckbox.setChecked(item.completed);

    // Determine which checkbox to display according to text size
    int checkBoxDrawable;
    int checkBoxHeight;

    if (mTextSize <= 10) {
      checkBoxDrawable = R.drawable.checkbox_selector_small;
      checkBoxHeight = 16; // DP - height and width are the same
    } else if (mTextSize <= 13) {
      checkBoxDrawable = R.drawable.checkbox_selector_medium;
      checkBoxHeight = 24; // DP - height and width are the same
    } else {
      checkBoxDrawable = R.drawable.checkbox_selector_large;
      checkBoxHeight = 32; // DP - height and width are the same
    }

    // Convert from Device-Independent pixels to actual screen pixels
    int dpHeight =
        (int)
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                checkBoxHeight,
                mItemListView.getResources().getDisplayMetrics());

    // Set the checkbox image + dimensions to use
    itemCheckbox.setLayoutParams(new LinearLayout.LayoutParams(dpHeight, dpHeight));
    itemCheckbox.setButtonDrawable(checkBoxDrawable);

    if (item.completed) itemContent.setTextColor(Color.GRAY);
    else itemContent.setTextColor((0xFF << 24) | item.getItemPriorityColor());

    itemCheckbox.setOnCheckedChangeListener(onCheckedChange);

    if (!item.canBeCompleted()) {
      // Some items can be marked as non-completeable
      itemCheckbox.setVisibility(View.INVISIBLE);
    } else {
      itemCheckbox.setVisibility(View.VISIBLE);
    }

    viewLayout.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            if (item.canBeCompleted()) {
              itemCheckbox.setChecked(!itemCheckbox.isChecked());
            }
          }
        });
    viewLayout.setLongClickable(true);

    viewLayout.setTag(item);

    return viewLayout;
  }