Search in sources :

Example 1 with NavigationItem

use of it.niedermann.owncloud.notes.main.navigation.NavigationItem in project nextcloud-notes by stefan-niedermann.

the class NoteListWidgetConfigurationActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setResult(RESULT_CANCELED);
    repo = NotesRepository.getInstance(this);
    final var args = getIntent().getExtras();
    if (args != null) {
        appWidgetId = args.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
        Log.d(TAG, "INVALID_APPWIDGET_ID");
        finish();
    }
    viewModel = new ViewModelProvider(this).get(NoteListViewModel.class);
    binding = ActivityNoteListConfigurationBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    adapterCategories = new NavigationAdapter(this, new NavigationClickListener() {

        @Override
        public void onItemClick(NavigationItem item) {
            final var data = new NotesListWidgetData();
            data.setId(appWidgetId);
            if (item.type != null) {
                switch(item.type) {
                    case RECENT:
                        {
                            data.setMode(MODE_DISPLAY_ALL);
                            break;
                        }
                    case FAVORITES:
                        {
                            data.setMode(MODE_DISPLAY_STARRED);
                            break;
                        }
                    case UNCATEGORIZED:
                        {
                            data.setMode(MODE_DISPLAY_CATEGORY);
                            data.setCategory(null);
                        }
                    case DEFAULT_CATEGORY:
                    default:
                        {
                            if (item.getClass() == NavigationItem.CategoryNavigationItem.class) {
                                data.setMode(MODE_DISPLAY_CATEGORY);
                                data.setCategory(((NavigationItem.CategoryNavigationItem) item).category);
                            } else {
                                data.setMode(MODE_DISPLAY_ALL);
                                Log.e(TAG, "Unknown item navigation type. Fallback to show " + RECENT);
                            }
                        }
                }
            } else {
                data.setMode(MODE_DISPLAY_ALL);
                Log.e(TAG, "Unknown item navigation type. Fallback to show " + RECENT);
            }
            data.setAccountId(localAccount.getId());
            data.setThemeMode(NotesApplication.getAppTheme(getApplicationContext()).getModeId());
            executor.submit(() -> {
                repo.createOrUpdateNoteListWidgetData(data);
                final var updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), NoteListWidget.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                setResult(RESULT_OK, updateIntent);
                getApplicationContext().sendBroadcast(updateIntent);
                finish();
            });
        }

        public void onIconClick(NavigationItem item) {
            onItemClick(item);
        }
    });
    binding.recyclerView.setAdapter(adapterCategories);
    executor.submit(() -> {
        try {
            this.localAccount = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(this).name);
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.widget_not_logged_in, Toast.LENGTH_LONG).show();
            // TODO Present user with app login screen
            Log.w(TAG, "onCreate: user not logged in");
            finish();
        }
        runOnUiThread(() -> viewModel.getAdapterCategories(localAccount.getId()).observe(this, (navigationItems) -> adapterCategories.setItems(navigationItems)));
    });
}
Also used : NavigationAdapter(it.niedermann.owncloud.notes.main.navigation.NavigationAdapter) Bundle(android.os.Bundle) LockedActivity(it.niedermann.owncloud.notes.LockedActivity) NavigationClickListener(it.niedermann.owncloud.notes.main.navigation.NavigationClickListener) Intent(android.content.Intent) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) MODE_DISPLAY_STARRED(it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED) MODE_DISPLAY_CATEGORY(it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY) NotesApplication(it.niedermann.owncloud.notes.NotesApplication) Toast(android.widget.Toast) Log(android.util.Log) ExecutorService(java.util.concurrent.ExecutorService) NavigationAdapter(it.niedermann.owncloud.notes.main.navigation.NavigationAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider) R(it.niedermann.owncloud.notes.R) NavigationItem(it.niedermann.owncloud.notes.main.navigation.NavigationItem) MODE_DISPLAY_ALL(it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL) Executors(java.util.concurrent.Executors) AppWidgetManager(android.appwidget.AppWidgetManager) NotesRepository(it.niedermann.owncloud.notes.persistence.NotesRepository) NoCurrentAccountSelectedException(com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException) ActivityNoteListConfigurationBinding(it.niedermann.owncloud.notes.databinding.ActivityNoteListConfigurationBinding) Nullable(androidx.annotation.Nullable) SingleAccountHelper(com.nextcloud.android.sso.helper.SingleAccountHelper) RECENT(it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.RECENT) Activity(android.app.Activity) Account(it.niedermann.owncloud.notes.persistence.entity.Account) NotesListWidgetData(it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData) Intent(android.content.Intent) NoCurrentAccountSelectedException(com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException) NotesListWidgetData(it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData) NavigationItem(it.niedermann.owncloud.notes.main.navigation.NavigationItem) NavigationClickListener(it.niedermann.owncloud.notes.main.navigation.NavigationClickListener) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Example 2 with NavigationItem

use of it.niedermann.owncloud.notes.main.navigation.NavigationItem in project nextcloud-notes by stefan-niedermann.

the class MainActivity method setupNavigationList.

private void setupNavigationList() {
    adapterCategories = new NavigationAdapter(this, new NavigationClickListener() {

        @Override
        public void onItemClick(NavigationItem item) {
            selectItem(item, true);
        }

        private void selectItem(NavigationItem item, boolean closeNavigation) {
            adapterCategories.setSelectedItem(item.id);
            // update current selection
            if (item.type != null) {
                switch(item.type) {
                    case RECENT:
                        {
                            mainViewModel.postSelectedCategory(new NavigationCategory(RECENT));
                            break;
                        }
                    case FAVORITES:
                        {
                            mainViewModel.postSelectedCategory(new NavigationCategory(FAVORITES));
                            break;
                        }
                    case UNCATEGORIZED:
                        {
                            mainViewModel.postSelectedCategory(new NavigationCategory(UNCATEGORIZED));
                            break;
                        }
                    default:
                        {
                            if (item.getClass() == NavigationItem.CategoryNavigationItem.class) {
                                mainViewModel.postSelectedCategory(new NavigationCategory(((NavigationItem.CategoryNavigationItem) item).accountId, ((NavigationItem.CategoryNavigationItem) item).category));
                            } else {
                                throw new IllegalStateException(NavigationItem.class.getSimpleName() + " type is " + DEFAULT_CATEGORY + ", but item is not of type " + NavigationItem.CategoryNavigationItem.class.getSimpleName() + ".");
                            }
                        }
                }
            } else {
                Log.e(TAG, "Unknown item navigation type. Fallback to show " + RECENT);
                mainViewModel.postSelectedCategory(new NavigationCategory(RECENT));
            }
            if (closeNavigation) {
                binding.drawerLayout.closeDrawer(GravityCompat.START);
            }
        }

        @Override
        public void onIconClick(NavigationItem item) {
            final var expandedCategoryLiveData = mainViewModel.getExpandedCategory();
            expandedCategoryLiveData.observe(MainActivity.this, expandedCategory -> {
                if (item.icon == NavigationAdapter.ICON_MULTIPLE && !item.label.equals(expandedCategory)) {
                    mainViewModel.postExpandedCategory(item.label);
                    selectItem(item, false);
                } else if (item.icon == NavigationAdapter.ICON_MULTIPLE || item.icon == NavigationAdapter.ICON_MULTIPLE_OPEN && item.label.equals(expandedCategory)) {
                    mainViewModel.postExpandedCategory(null);
                } else {
                    onItemClick(item);
                }
                expandedCategoryLiveData.removeObservers(MainActivity.this);
            });
        }
    });
    adapterCategories.setSelectedItem(ADAPTER_KEY_RECENT);
    binding.navigationList.setAdapter(adapterCategories);
}
Also used : NavigationAdapter(it.niedermann.owncloud.notes.main.navigation.NavigationAdapter) NavigationItem(it.niedermann.owncloud.notes.main.navigation.NavigationItem) NavigationClickListener(it.niedermann.owncloud.notes.main.navigation.NavigationClickListener) NavigationCategory(it.niedermann.owncloud.notes.shared.model.NavigationCategory)

Example 3 with NavigationItem

use of it.niedermann.owncloud.notes.main.navigation.NavigationItem in project nextcloud-notes by stefan-niedermann.

the class MainViewModel method fromCategoriesWithNotesCount.

private static List<NavigationItem> fromCategoriesWithNotesCount(@NonNull Context context, @Nullable String expandedCategory, @NonNull List<CategoryWithNotesCount> fromDatabase, int count, int favoritesCount) {
    final var categories = convertToCategoryNavigationItem(context, fromDatabase);
    final var itemRecent = new NavigationItem(ADAPTER_KEY_RECENT, context.getString(R.string.label_all_notes), count, R.drawable.ic_access_time_grey600_24dp, RECENT);
    final var itemFavorites = new NavigationItem(ADAPTER_KEY_STARRED, context.getString(R.string.label_favorites), favoritesCount, R.drawable.ic_star_yellow_24dp, FAVORITES);
    final var items = new ArrayList<NavigationItem>(fromDatabase.size() + 3);
    items.add(itemRecent);
    items.add(itemFavorites);
    NavigationItem lastPrimaryCategory = null;
    NavigationItem lastSecondaryCategory = null;
    for (final var item : categories) {
        final int slashIndex = item.label.indexOf('/');
        final String currentPrimaryCategory = slashIndex < 0 ? item.label : item.label.substring(0, slashIndex);
        final boolean isCategoryOpen = currentPrimaryCategory.equals(expandedCategory);
        String currentSecondaryCategory = null;
        if (isCategoryOpen && !currentPrimaryCategory.equals(item.label)) {
            final String currentCategorySuffix = item.label.substring(expandedCategory.length() + 1);
            final int subSlashIndex = currentCategorySuffix.indexOf('/');
            currentSecondaryCategory = subSlashIndex < 0 ? currentCategorySuffix : currentCategorySuffix.substring(0, subSlashIndex);
        }
        boolean belongsToLastPrimaryCategory = lastPrimaryCategory != null && currentPrimaryCategory.equals(lastPrimaryCategory.label);
        final boolean belongsToLastSecondaryCategory = belongsToLastPrimaryCategory && lastSecondaryCategory != null && lastSecondaryCategory.label.equals(currentSecondaryCategory);
        if (isCategoryOpen && !belongsToLastPrimaryCategory && currentSecondaryCategory != null) {
            lastPrimaryCategory = new NavigationItem("category:" + currentPrimaryCategory, currentPrimaryCategory, 0, NavigationAdapter.ICON_MULTIPLE_OPEN);
            items.add(lastPrimaryCategory);
            belongsToLastPrimaryCategory = true;
        }
        if (belongsToLastPrimaryCategory && belongsToLastSecondaryCategory) {
            lastSecondaryCategory.count += item.count;
            lastSecondaryCategory.icon = NavigationAdapter.ICON_SUB_MULTIPLE;
        } else if (belongsToLastPrimaryCategory) {
            if (isCategoryOpen) {
                if (currentSecondaryCategory == null) {
                    throw new IllegalStateException("Current secondary category is null. Last primary category: " + lastPrimaryCategory);
                }
                item.label = currentSecondaryCategory;
                item.id = "category:" + item.label;
                item.icon = NavigationAdapter.ICON_SUB_FOLDER;
                items.add(item);
                lastSecondaryCategory = item;
            } else {
                lastPrimaryCategory.count += item.count;
                lastPrimaryCategory.icon = NavigationAdapter.ICON_MULTIPLE;
                lastSecondaryCategory = null;
            }
        } else {
            if (isCategoryOpen) {
                item.icon = NavigationAdapter.ICON_MULTIPLE_OPEN;
            } else {
                item.label = currentPrimaryCategory;
                item.id = "category:" + item.label;
            }
            items.add(item);
            lastPrimaryCategory = item;
            lastSecondaryCategory = null;
        }
    }
    return items;
}
Also used : NavigationItem(it.niedermann.owncloud.notes.main.navigation.NavigationItem) DisplayUtils.convertToCategoryNavigationItem(it.niedermann.owncloud.notes.shared.util.DisplayUtils.convertToCategoryNavigationItem) ArrayList(java.util.ArrayList)

Example 4 with NavigationItem

use of it.niedermann.owncloud.notes.main.navigation.NavigationItem in project nextcloud-notes by stefan-niedermann.

the class CategoryAdapter method setCategoryList.

void setCategoryList(List<NavigationItem.CategoryNavigationItem> categories, @Nullable String currentSearchString) {
    this.categories.clear();
    this.categories.addAll(categories);
    final NavigationItem clearItem = new NavigationItem(clearItemId, context.getString(R.string.no_category), 0, R.drawable.ic_clear_grey_24dp);
    this.categories.add(0, clearItem);
    if (currentSearchString != null && currentSearchString.trim().length() > 0) {
        boolean currentSearchStringIsInCategories = false;
        for (final var category : categories) {
            if (currentSearchString.equals(category.label)) {
                currentSearchStringIsInCategories = true;
                break;
            }
        }
        if (!currentSearchStringIsInCategories) {
            final var addItem = new NavigationItem(addItemId, context.getString(R.string.add_category, currentSearchString.trim()), 0, R.drawable.ic_add_blue_24dp);
            this.categories.add(addItem);
        }
    }
    notifyDataSetChanged();
}
Also used : NavigationItem(it.niedermann.owncloud.notes.main.navigation.NavigationItem)

Aggregations

NavigationItem (it.niedermann.owncloud.notes.main.navigation.NavigationItem)4 NavigationAdapter (it.niedermann.owncloud.notes.main.navigation.NavigationAdapter)2 NavigationClickListener (it.niedermann.owncloud.notes.main.navigation.NavigationClickListener)2 Activity (android.app.Activity)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 Toast (android.widget.Toast)1 Nullable (androidx.annotation.Nullable)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)1 NoCurrentAccountSelectedException (com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException)1 SingleAccountHelper (com.nextcloud.android.sso.helper.SingleAccountHelper)1 LockedActivity (it.niedermann.owncloud.notes.LockedActivity)1 NotesApplication (it.niedermann.owncloud.notes.NotesApplication)1 R (it.niedermann.owncloud.notes.R)1 ActivityNoteListConfigurationBinding (it.niedermann.owncloud.notes.databinding.ActivityNoteListConfigurationBinding)1 NotesRepository (it.niedermann.owncloud.notes.persistence.NotesRepository)1 Account (it.niedermann.owncloud.notes.persistence.entity.Account)1