realm / realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
realm.io
Apache License 2.0
414 stars 134 forks source link

Does this adapter work with the latest version of realm(5.12.0)? #155

Closed dejiapps closed 5 years ago

dejiapps commented 5 years ago

Goal

Create an adapter that auto updates

Expected Results

?

Actual Results

Updates on insert and delete, but not on update.

Steps & Code to Reproduce

Describe your current debugging efforts.

Code Sample

public class TaskAdapter extends OmegaRecyclerView.Adapter

{

OrderedRealmCollection<TaskDB> data;

Context context;
String Quadrant;
private Realm realm;
Long id;
ConstraintLayout parentview;
OnDoneClickListener onDoneClickListener;
OnEditClickListener onEditClickListener;
OnDeleteClickListener onDeleteClickListener;
OrderedRealmCollectionChangeListener listener;

public TaskAdapter(Context context, OrderedRealmCollection<TaskDB> Data, String quadrant, Realm adapterRealm,
                   ConstraintLayout ParentView, OnDoneClickListener onDoneClickListener,
                   OnEditClickListener onEditClickListener, OnDeleteClickListener onDeleteClickListener) {
    this.data = Data;
    this.context = context;
    this.Quadrant = quadrant;
    this.realm = adapterRealm;
    this.parentview = ParentView;
    this.onDoneClickListener = onDoneClickListener;
    this.onEditClickListener = onEditClickListener;
    this.onDeleteClickListener = onDeleteClickListener;
    this.listener = createListener();

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(parent);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.updateView(data.get(position));
    id = data.get(position).getId();
}

@Override
public int getItemCount() {
    return data.size();
}

public class ViewHolder extends SwipeViewHolder implements View.OnClickListener
{

    private LinearLayout doneButton;
    private LinearLayout editButton;
    private LinearLayout moveButton;
    private LinearLayout deleteButton;

    public MaterialCheckBox checkBox;
    public TextView task;
    public TextView taskDueDate;
    public ImageView descriptionIcon;
    public ImageView repeatIcon;
    public ImageView alarmIcon;
    public ConstraintLayout checkBoxTouch;
    public TextView filter;
    public ImageView subIcon;

    public ViewHolder(ViewGroup parent) {
        super(parent, R.layout.list_layout_task, R.layout.item_left_swipe_menu, R.layout.item_right_swipe_menu);

        checkBox = findViewById(R.id.checkBox);
        task = findViewById(R.id.task);
        taskDueDate = findViewById(R.id.task_due_date);
        descriptionIcon = findViewById(R.id.descriptionIcon);
        repeatIcon = findViewById(R.id.repeatIcon);
        alarmIcon = findViewById(R.id.alarmIcon);
        checkBoxTouch = findViewById(R.id.checkBox_frame);
        filter = findViewById(R.id.filter);
        subIcon = findViewById(R.id.subIcon);

        doneButton = findViewById(R.id.done_button);
        doneButton.setOnClickListener(this);
        editButton = findViewById(R.id.edit_button);
        editButton.setOnClickListener(this);
        moveButton = findViewById(R.id.move_button);
        moveButton.setOnClickListener(this);
        deleteButton = findViewById(R.id.delete_button);
        deleteButton.setOnClickListener(this);

    }

    void updateView(TaskDB Task)
    {

        id = Task.getId();

        Animation rotate = AnimationUtils.loadAnimation(context, R.anim.rotate);
        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);

        int thisColor;

        if (Quadrant.equals("tl")) {
            thisColor = Color.parseColor(GoodPrefs.getInstance().getString("IandUcolor", "#F44336"));
        } else if (Quadrant.equals("tr")) {
            thisColor = Color.parseColor(GoodPrefs.getInstance().getString("InotUcolor", "#4caf50"));
        } else if (Quadrant.equals("bl")) {
            thisColor = Color.parseColor(GoodPrefs.getInstance().getString("UnotIcolor", "#ff9800"));
        } else {
            thisColor = Color.parseColor(GoodPrefs.getInstance().getString("notUnorIcolor", "#607d8b"));
        }

        //Checkbox color
        ColorStateList boxColorState = new ColorStateList
                (
                        new int[][]{
                                new int[]{android.R.attr.state_pressed},
                                new int[]{android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_focused, android.R.attr.state_pressed},
                                new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_active},
                                new int[]{}// this should be empty to make default color as we want
                        },

                        new int[]{thisColor, thisColor, thisColor, thisColor, thisColor, thisColor});

        checkBox.setButtonTintList(boxColorState);
        checkBoxTouch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBox.setChecked(true);
                onDoneClickListener.onDoneClick(Task.getId(), getAdapterPosition());
            }
        });

        contentView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v)
            {
                onEditClickListener.onEditClick(Task.getId(), getAdapterPosition(), Task.getCategory());
                return true;
            }
        });

        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEditClickListener.onEditClick(Task.getId(), getAdapterPosition(), Task.getCategory());
            }
        });

        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBox.setChecked(true);
                onDoneClickListener.onDoneClick(Task.getId(), getAdapterPosition());
                //LetsDo.INSTANCE.markDone(context, realm, Task.getId());

            }
        });

        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEditClickListener.onEditClick(Task.getId(), getAdapterPosition(), Task.getCategory());
                smoothCloseMenu();
                //RxBus.INSTANCE.publish(new MessageEvent(Task.getId(), "edit task"));

            }
        });

        moveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LetsDo.INSTANCE.move(realm, context, Task.getId(), parentview);
                smoothCloseMenu();

            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onDeleteClickListener.onDeleteClick(Task.getId(), getAdapterPosition());
            }
        });

        RealmChangeListener<TaskDB> listener2 = new RealmChangeListener<TaskDB>() {
            @Override
            public void onChange(TaskDB thisTask) {
                Log.e("TaskAdapter", "change listener called");

                {
                    Log.e("TaskAdapter", "delete listener called");
                    notifyItemRemoved(getAdapterPosition());
                    tryNotifyDataSetChanged();

                }
                else {
                    tryNotifyDataSetChanged();
                    Log.e("TaskAdapter", "change listener called");
                }*/
                // changes have been made to Account table
            }

        };

        if (Task.getComplete()) {
            if (task != null) {
                task.setText(Task.getTask());
                task.setPaintFlags(task.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                task.setAlpha(0.5F);
                checkBox.setChecked(true);
                filter.setVisibility(View.GONE);
                taskDueDate.setVisibility(View.GONE);
                descriptionIcon.setVisibility(View.GONE);
                repeatIcon.setVisibility(View.GONE);
                alarmIcon.setVisibility(View.GONE);
                subIcon.setVisibility(View.GONE);
            }
        }
        else {

            if (task != null) {
                task.setText(Task.getTask());
                task.setPaintFlags(0);
                checkBox.setChecked(false);
            }

            ColorStateList oldColors = task.getTextColors();

            boolean DarkMode = GoodPrefs.getInstance().getBoolean("darkMode", true); //for now
            if (DarkMode) {
                task.setAlpha(1);
                taskDueDate.setAlpha(.7f);
                alarmIcon.setAlpha(.7f);
                descriptionIcon.setAlpha(.7f);
                repeatIcon.setAlpha(.7f);
                subIcon.setAlpha(.7f);
                filter.setAlpha(.7f);
            } else {
                task.setAlpha(0.7f);
                taskDueDate.setAlpha(.64f);
                alarmIcon.setAlpha(.72f);
                descriptionIcon.setAlpha(.72f);
                repeatIcon.setAlpha(.72f);
                subIcon.setAlpha(.72f);
                filter.setAlpha(.64f);
            }

            if (Task.getDueDate() != null) {
                taskDueDate.setVisibility(View.VISIBLE);
                Date due_date = Task.getDueDate();
                long epoch = due_date.getTime();
                taskDueDate.setText(TimeAgo.using(epoch));

                if (due_date.before(new Date())) {
                    taskDueDate.setTextColor(ContextCompat.getColor(context, R.color.overdue));
                } else {
                    if (DarkMode) {
                        //taskDueDate.setTextColor(ContextCompat.getColor(context, R.color.txt_light));
                        taskDueDate.setTextColor(oldColors);
                    } else {
                        //taskDueDate.setTextColor(ContextCompat.getColor(context, R.color.txt_dark));
                        taskDueDate.setTextColor(oldColors);
                    }
                    //taskDueDate.setTextColor(ContextCompat.getColor(context, R.color.textColorPrimary)); todo
                }
            } else {
                taskDueDate.setVisibility(View.GONE);
            }

            if (!Task.getFilter().equals(" ")) {
                filter.setText(Task.getFilter());

                new Handler().postDelayed(() ->
                {

                    filter.setText(Task.getFilter());
                    Technique.SLIDE_IN_UP.getComposer().duration(800).delay(0).playOn(filter);
                    filter.setTextColor(thisColor);
                    filter.setVisibility(View.VISIBLE);
                }, 600);
            } else {
                filter.setVisibility(View.GONE);
            }

            if (Task.getDetails() != null) {
                descriptionIcon.setVisibility(View.VISIBLE);
                Technique.PULSE.getComposer().duration(1200).delay(600).playOn(descriptionIcon);
            } else {
                descriptionIcon.setVisibility(View.GONE);
            }
            if (Task.getFirstReminder() != null || Task.getSecondReminder() != null) {
                alarmIcon.setVisibility(View.VISIBLE);
                alarmIcon.startAnimation(shake);
                //Technique.TADA.getComposer().duration(1200).delay(600).playOn(alarmIcon);
            } else {
                alarmIcon.setVisibility(View.GONE);
            }
            if (Task.getRepeat_array() != null) {
                repeatIcon.setVisibility(View.VISIBLE);
                repeatIcon.startAnimation(rotate);
                //Technique.SWING.getComposer().duration(1200).delay(0).playOn(repeatIcon);
            } else {
                repeatIcon.setVisibility(View.GONE);
            }
            if (Task.getSubtasks().size() > 0) {
                subIcon.setVisibility(View.VISIBLE);
            } else {
                subIcon.setVisibility(View.GONE);
            }

    /*if (pref.getBoolean("checkbox", true))
    {
        checkBox.setChecked(false);
        checkBoxTouch.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                checkBox.toggle();
            }
        });
    }
    else
    {
        checkBoxTouch.setVisibility(View.GONE);
    }*/

            checkBox.setChecked(false);

    /*if (!Task.getFilter().equalsIgnoreCase(" ") )
    {
        filter.setText(Task.getFilter());

        new Handler().postDelayed(() ->
        {

            filter.setText(Task.getFilter());
            Technique.SLIDE_IN_UP.getComposer().duration(800).delay(0).playOn(filter);
            filter.setVisibility(View.VISIBLE);
        }, 600);

    }
    else
    {
        filter.setVisibility(View.GONE);
    }

    if (Task.getHasSubs() == null || !Task.getHasSubs())
    {
        subIcon.setVisibility(View.GONE);
    }
    else
    {
        subIcon.setVisibility(View.VISIBLE);
    }*/
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId()) {
            case R.id.done_button:
                showToast("Delete");
                break;
            case R.id.move_button:
                showToast("Voice mail");
                break;
            case R.id.delete_button:
                showToast("Bubble");
                Log.e("adapter", "delete clicked");
                break;
        }
    }

    private void showToast(String message) {
        Toast.makeText(itemView.getContext(), message, Toast.LENGTH_SHORT).show();
    }

    public void swapItems(OrderedRealmCollection<TaskDB> Data)
    {
        data = Data;
        notifyDataSetChanged();
    }

}

}

Version of Realm and tooling

Realm version(s): 5.12.0

Android Studio version: 3.4.2

Which Android version and device: Oneplus 7 Pro on android P

realm-probot[bot] commented 5 years ago

Hey - looks like you forgot to add a T:* label - could you please add one?