Search in sources :

Example 1 with NavigationClickListener

use of it.niedermann.owncloud.notes.main.navigation.NavigationClickListener 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 NavigationClickListener

use of it.niedermann.owncloud.notes.main.navigation.NavigationClickListener 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)

Aggregations

NavigationAdapter (it.niedermann.owncloud.notes.main.navigation.NavigationAdapter)2 NavigationClickListener (it.niedermann.owncloud.notes.main.navigation.NavigationClickListener)2 NavigationItem (it.niedermann.owncloud.notes.main.navigation.NavigationItem)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