Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cfd0902a authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

NoteListWidget: Move create button to scrollable entry, remove header

parent 6cbbab72
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -20,9 +20,9 @@ Please use **[GitHub reactions](https://blog.github.com/2016-03-10-add-reactions

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
1. Go to ''
2. Click on ''
3. Scroll down to ''
4. See error


+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ Guidelines for submitting issues:
Please use **[GitHub reactions](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/)** 👍 to show that you are affected by the same issue. Please don't comment if you have no relevant information to add!

**Is your feature request related to a problem? Please describe.**
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [] -->

**Describe the solution you'd like**
<!-- A clear and concise description of what you want to happen. -->
+4 −4
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ If you find a bug, feel free to [open an issue](https://github.com/stefan-nieder

**Device**: e. g. Motorola Moto G 2015

**System language**: English (US), German, ...
**System language**: English (US), German, 

**App version:** e. g. v0.8.1

@@ -18,14 +18,14 @@ If you find a bug, feel free to [open an issue](https://github.com/stefan-nieder
  1. open the app
  2. click on a note
  3. use the top left back-arrow
  4. ...
  4. 

### Copy & Paste
    **Android version:** e. g. 6.0.1 Marshmallow
    
    **Device**: e. g. Motorola Moto G 2015
    
    **System language**: English (US), German, ...
    **System language**: English (US), German, 
    
    **App version:** e. g. v0.8.1
    
@@ -35,7 +35,7 @@ If you find a bug, feel free to [open an issue](https://github.com/stefan-nieder
      1. open the app
      2. click on a note
      3. use the top left back-arrow
      4. ...
      4. 

## Adding new features

+1 −71
Original line number Diff line number Diff line
@@ -6,29 +6,15 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.core.content.ContextCompat;

import java.util.NoSuchElementException;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.preferences.DarkModeSetting;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.main.MainActivity;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.shared.model.Category;
import it.niedermann.owncloud.notes.shared.model.LocalAccount;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.NotesApplication;

import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_ALL;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_CATEGORY;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_STARRED;

public class NoteListWidget extends AppWidgetProvider {
    private static final String TAG = NoteListWidget.class.getSimpleName();
@@ -41,39 +27,15 @@ public class NoteListWidget extends AppWidgetProvider {
        final NotesDatabase db = NotesDatabase.getInstance(context);

        RemoteViews views;
        DarkModeSetting darkTheme;

        for (int appWidgetId : appWidgetIds) {
            try {
                final NoteListsWidgetData data = db.getNoteListWidgetData(appWidgetId);
                final LocalAccount localAccount = db.getAccount(data.getAccountId());

                String category = null;
                if (data.getCategoryId() != null) {
                    category = db.getCategoryTitleById(data.getAccountId(), data.getCategoryId());
                }

                darkTheme = DarkModeSetting.fromModeID(data.getThemeMode());

                Intent serviceIntent = new Intent(context, NoteListWidgetService.class);
                serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));

                // Launch application when user taps the header icon or app title
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName(context.getPackageName(),
                        MainActivity.class.getName()));

                // Open the main app if the user taps the widget header
                PendingIntent openAppI = PendingIntent.getActivity(context, PENDING_INTENT_OPEN_APP_RQ,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                // Launch create note activity if user taps "+" icon on header
                PendingIntent newNoteI = PendingIntent.getActivity(context, (PENDING_INTENT_NEW_NOTE_RQ + appWidgetId),
                        new Intent(context, EditNoteActivity.class).putExtra(PARAM_CATEGORY, new Category(category, data.getMode() == MODE_DISPLAY_STARRED)),
                        PendingIntent.FLAG_UPDATE_CURRENT);

                PendingIntent templatePI = PendingIntent.getActivity(context, PENDING_INTENT_EDIT_NOTE_RQ,
                        new Intent(context, EditNoteActivity.class),
                        PendingIntent.FLAG_UPDATE_CURRENT);
@@ -81,26 +43,11 @@ public class NoteListWidget extends AppWidgetProvider {
                Log.v(TAG, "-- data - " + data);

                views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list);
                views.setTextViewText(R.id.widget_note_list_title_tv, getWidgetTitle(context, data.getMode(), category));
                views.setOnClickPendingIntent(R.id.widget_note_header_icon, openAppI);
                views.setOnClickPendingIntent(R.id.widget_note_list_title_tv, openAppI);
                views.setOnClickPendingIntent(R.id.widget_note_list_create_icon, newNoteI);
                views.setPendingIntentTemplate(R.id.note_list_widget_lv, templatePI);
                views.setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent);
                views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv);
                awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv);
                if (BrandingUtil.isBrandingEnabled(context)) {
                    views.setInt(R.id.widget_note_header, "setBackgroundColor", localAccount.getColor());
                    views.setInt(R.id.widget_note_header_icon, "setColorFilter", localAccount.getTextColor());
                    views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", localAccount.getTextColor());
                    views.setTextColor(R.id.widget_note_list_title_tv, localAccount.getTextColor());
                } else {
                    views.setInt(R.id.widget_note_header, "setBackgroundColor", ContextCompat.getColor(context, R.color.defaultBrand));
                    views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.WHITE);
                    views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", Color.WHITE);
                    views.setTextColor(R.id.widget_note_list_title_tv, Color.WHITE);
                }

                awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv);
                awm.updateAppWidget(appWidgetId, views);
            } catch (NoSuchElementException e) {
                Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget");
@@ -146,23 +93,6 @@ public class NoteListWidget extends AppWidgetProvider {
        }
    }

    private static String getWidgetTitle(Context context, int displayMode, String category) {
        switch (displayMode) {
            case MODE_DISPLAY_ALL:
                return context.getString(R.string.app_name);
            case MODE_DISPLAY_STARRED:
                return context.getString(R.string.label_favorites);
            case MODE_DISPLAY_CATEGORY:
                if ("".equals(category)) {
                    return context.getString(R.string.action_uncategorized);
                } else {
                    return category;
                }
            default:
                return null;
        }
    }

    /**
     * Update note list widgets, if the note data was changed.
     */
+61 −25
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.widget.notelist;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
@@ -10,16 +9,17 @@ import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.preferences.DarkModeSetting;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.shared.model.Category;
import it.niedermann.owncloud.notes.shared.model.DBNote;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.shared.model.LocalAccount;

import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_ALL;
@@ -45,6 +45,7 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact

    @Override
    public void onCreate() {
        // Nothing to do here…
    }

    @Override
@@ -82,29 +83,37 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
     */
    @Override
    public int getCount() {
        return dbNotes.size();
        return dbNotes.size() + 1;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        final RemoteViews note_content;

//        if(position == 0) {
//            final Intent fillInIntent = new Intent();
//            final Bundle extras = new Bundle();
//            extras.putExtra(PARAM_CATEGORY, new Category(db.getCategoryTitleById(data.getAccountId(), data.getCategoryId()), data.getMode() == MODE_DISPLAY_STARRED)),
//            extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
//
//            fillInIntent.putExtras(extras);
//            fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
//
//            fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
//
//            note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry);
//            note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent);
//            note_content.setTextViewText(R.id.widget_entry_content_tv, "Add new note in this category");
//            note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
//        } else {
        if (position == 0) {
            final LocalAccount localAccount = db.getAccount(data.getAccountId());
            final Intent fillInIntent = new Intent();
            final Bundle extras = new Bundle();

            String category = null;
            Long categoryId = data.getCategoryId();
            if (categoryId != null) {
                category = db.getCategoryTitleById(data.getAccountId(), categoryId);
            }

            extras.putSerializable(PARAM_CATEGORY, new Category(category, data.getMode() == MODE_DISPLAY_STARRED));
            extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());

            fillInIntent.putExtras(extras);
            fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));

            note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add);
            note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent);
            note_content.setTextViewText(R.id.widget_entry_content_tv, getAddButtonText(context, data.getMode(), category));
            note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
            note_content.setInt(R.id.widget_entry_fav_icon, "setColorFilter", localAccount.getColor());
        } else {
            position--;
            if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) {
                Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.");
                return null;
@@ -125,12 +134,29 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
            note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.isFavorite()
                    ? R.drawable.ic_star_yellow_24dp
                    : R.drawable.ic_star_grey_ccc_24dp);
//        }
        }

        return note_content;

    }

    @NonNull
    private static String getAddButtonText(@NonNull Context context, int displayMode, String category) {
        switch (displayMode) {
            case MODE_DISPLAY_STARRED:
                return context.getString(R.string.widget_note_list_add_favorite);
            case MODE_DISPLAY_CATEGORY:
                if ("".equals(category)) {
                    return context.getString(R.string.widget_note_list_add);
                } else {
                    return context.getString(R.string.widget_note_list_add_to_category, category);
                }
            case MODE_DISPLAY_ALL:
            default:
                return context.getString(R.string.widget_note_list_add);
        }
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
@@ -138,12 +164,22 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact

    @Override
    public int getViewTypeCount() {
        return 1;
        return 2;
    }

    @Override
    public long getItemId(int i) {
        return i;
    public long getItemId(int position) {
        if (position == 0) {
            return -1;
        } else {
            position--;
            if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) {
                Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.");
                return -2;
            }
            DBNote note = dbNotes.get(position);
            return note.getId();
        }
    }

    @Override
Loading