Search in sources :

Example 1 with ActivitySummariesAdapter

use of nodomain.freeyourgadget.gadgetbridge.adapter.ActivitySummariesAdapter in project Gadgetbridge by Freeyourgadget.

the class ActivitySummariesActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
    } else {
        throw new IllegalArgumentException("Must provide a device when invoking this activity");
    }
    deviceFilter = getDeviceId(mGBDevice);
    IntentFilter filterLocal = new IntentFilter();
    filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
    super.onCreate(savedInstanceState);
    ActivitySummariesAdapter activitySummariesAdapter = new ActivitySummariesAdapter(this, mGBDevice, activityFilter, dateFromFilter, dateToFilter, nameContainsFilter, deviceFilter, itemsFilter);
    int backgroundColor = getBackgroundColor(ActivitySummariesActivity.this);
    activitySummariesAdapter.setBackgroundColor(backgroundColor);
    activitySummariesAdapter.setShowTime(false);
    setItemAdapter(activitySummariesAdapter);
    getItemListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // item 0 is empty for dashboard
            if (position == 0)
                return;
            Object item = parent.getItemAtPosition(position);
            if (item != null) {
                ActivitySummary summary = (ActivitySummary) item;
                try {
                    showActivityDetail(position);
                } catch (Exception e) {
                    GB.toast(getApplicationContext(), "Unable to display Activity Detail, maybe the activity is not available yet: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
                }
            }
        }
    });
    getItemListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getItemListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
            if (position == 0 && checked)
                subtrackDashboard = 1;
            if (position == 0 && !checked)
                subtrackDashboard = 0;
            final int selectedItems = getItemListView().getCheckedItemCount() - subtrackDashboard;
            actionMode.setTitle(selectedItems + " selected");
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            getMenuInflater().inflate(R.menu.activity_list_context_menu, menu);
            findViewById(R.id.fab).setVisibility(View.INVISIBLE);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            boolean processed = false;
            SparseBooleanArray checked = getItemListView().getCheckedItemPositions();
            switch(menuItem.getItemId()) {
                case R.id.activity_action_delete:
                    List<BaseActivitySummary> toDelete = new ArrayList<>();
                    for (int i = 0; i < checked.size(); i++) {
                        if (checked.valueAt(i)) {
                            toDelete.add(getItemAdapter().getItem(checked.keyAt(i)));
                        }
                    }
                    deleteItems(toDelete);
                    processed = true;
                    break;
                case R.id.activity_action_export:
                    List<String> paths = new ArrayList<>();
                    for (int i = 0; i < checked.size(); i++) {
                        if (checked.valueAt(i)) {
                            BaseActivitySummary item = getItemAdapter().getItem(checked.keyAt(i));
                            if (item != null) {
                                ActivitySummary summary = item;
                                String gpxTrack = summary.getGpxTrack();
                                if (gpxTrack != null) {
                                    paths.add(gpxTrack);
                                }
                            }
                        }
                    }
                    shareMultiple(paths);
                    processed = true;
                    break;
                case R.id.activity_action_select_all:
                    for (int i = 0; i < getItemListView().getCount(); i++) {
                        getItemListView().setItemChecked(i, true);
                    }
                    // don't finish actionmode in this case!
                    return true;
                case R.id.activity_action_addto_filter:
                    List<Long> toFilter = new ArrayList<>();
                    for (int i = 0; i < checked.size(); i++) {
                        if (checked.valueAt(i)) {
                            BaseActivitySummary item = getItemAdapter().getItem(checked.keyAt(i));
                            if (item != null && item.getId() != null) {
                                ActivitySummary summary = item;
                                Long id = summary.getId();
                                toFilter.add(id);
                            }
                        }
                    }
                    itemsFilter = toFilter;
                    setItemsFilter(itemsFilter);
                    refresh();
                    processed = true;
                    break;
                default:
                    break;
            }
            actionMode.finish();
            return processed;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            findViewById(R.id.fab).setVisibility(View.VISIBLE);
        }
    });
    swipeLayout = findViewById(R.id.list_activity_swipe_layout);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            fetchTrackData();
        }
    });
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            fetchTrackData();
        }
    });
    activityKindMap = fillKindMap();
}
Also used : BaseActivitySummary(nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary) ActivitySummary(nodomain.freeyourgadget.gadgetbridge.model.ActivitySummary) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) SparseBooleanArray(android.util.SparseBooleanArray) BaseActivitySummary(nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) ArrayList(java.util.ArrayList) List(java.util.List) Menu(android.view.Menu) ActivitySummariesAdapter(nodomain.freeyourgadget.gadgetbridge.adapter.ActivitySummariesAdapter) IntentFilter(android.content.IntentFilter) Bundle(android.os.Bundle) AbsListView(android.widget.AbsListView) MenuItem(android.view.MenuItem) View(android.view.View) AdapterView(android.widget.AdapterView) AbsListView(android.widget.AbsListView) ListView(android.widget.ListView) ActionMode(android.view.ActionMode) AdapterView(android.widget.AdapterView)

Aggregations

IntentFilter (android.content.IntentFilter)1 Bundle (android.os.Bundle)1 SparseBooleanArray (android.util.SparseBooleanArray)1 ActionMode (android.view.ActionMode)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 FloatingActionButton (com.google.android.material.floatingactionbutton.FloatingActionButton)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ActivitySummariesAdapter (nodomain.freeyourgadget.gadgetbridge.adapter.ActivitySummariesAdapter)1 BaseActivitySummary (nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary)1 ActivitySummary (nodomain.freeyourgadget.gadgetbridge.model.ActivitySummary)1