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)));
});
}
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);
}
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;
}
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();
}
Aggregations