Skip to content

Commit

Permalink
IDE-271 Improved backpack UI
Browse files Browse the repository at this point in the history
IDE-271 Fix copyright and warnings
  • Loading branch information
Frajhamster committed Dec 12, 2024
1 parent 11ad848 commit 55ff0f2
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 53 deletions.
34 changes: 33 additions & 1 deletion catroid/src/main/java/org/catrobat/catroid/ui/UiUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -27,6 +27,7 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -164,4 +165,35 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
}
};
}

public static ArrayAdapter getAlertDialogAdapterForTextWithIcons(Activity activity, Context context, List<Pair<String, Integer>> items) {
return new ArrayAdapter<Pair<String, Integer>>(context, android.R.layout.simple_list_item_1, items) {
TextView item;

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = inflater.inflate(R.layout.dialog_item_with_icon, parent, false);
item = (TextView) convertView.findViewById(R.id.item_text);
convertView.setTag(item);
} else {
item = (TextView) convertView.getTag();
}

item.setText(items.get(position).first);

if (activity.getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
item.setCompoundDrawablesWithIntrinsicBounds(items.get(position).second, 0, 0,
0);
} else {
item.setCompoundDrawablesWithIntrinsicBounds(0, 0, items.get(position).second, 0);
}

return convertView;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -26,14 +26,17 @@
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Pair;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;

import org.catrobat.catroid.R;
import org.catrobat.catroid.ui.UiUtils;
Expand All @@ -47,8 +50,10 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import androidx.annotation.IntDef;
import androidx.annotation.PluralsRes;
Expand All @@ -57,6 +62,8 @@
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons;

public abstract class BackpackRecyclerViewFragment<T> extends Fragment implements
ActionMode.Callback,
RVAdapter.SelectionListener,
Expand Down Expand Up @@ -292,28 +299,41 @@ public void onItemClick(final T item, MultiSelectionManager selectionManager) {
return;
}

List<Integer> options = new ArrayList<>();
options.add(R.string.unpack);
options.add(R.string.delete);
List<String> names = new ArrayList<>();
for (Integer option: options) {
names.add(getString(option));
}
ListAdapter arrayAdapter = UiUtils.getAlertDialogAdapterForMenuIcons(options,
names, requireContext(), requireActivity());

new AlertDialog.Builder(requireContext())
.setTitle(getItemName(item))
.setAdapter(arrayAdapter, (dialog, which) -> {
switch (which) {
case 0:
unpackItems(new ArrayList<>(Collections.singletonList(item)));
break;
case 1:
showDeleteAlert(new ArrayList<>(Collections.singletonList(item)));
}
})
.show();
List<Pair<String, Integer>> items = Arrays.asList(
new Pair<>(getString(R.string.unpack), R.drawable.ic_logout),
new Pair<>(getString(R.string.delete), R.drawable.ic_delete)
);

LayoutInflater inflater = LayoutInflater.from(requireContext());
View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null);
ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(),
requireContext(), items);

AlertDialog dialog = new AlertDialog.Builder(requireContext())
.setView(customDialogView)
.create();
Objects.requireNonNull(dialog.getWindow())
.setBackgroundDrawableResource(R.drawable.backpack_background_round);

TextView textView = customDialogView.findViewById(R.id.backpack_dialog_title);
textView.setText(getItemName(item));

ListView listView = customDialogView.findViewById(R.id.backpack_item_list);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
switch (position) {
case 0:
unpackItems(new ArrayList<>(Collections.singletonList(item)));
break;
case 1:
showDeleteAlert(new ArrayList<>(Collections.singletonList(item)));
break;
}

dialog.dismiss();
});

dialog.show();
}

public void setShowProgressBar(boolean show) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -27,12 +27,15 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.Pair;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.catrobat.catroid.R;
Expand All @@ -54,7 +57,9 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
Expand All @@ -66,6 +71,7 @@
import androidx.recyclerview.widget.RecyclerView;

import static org.catrobat.catroid.common.SharedPreferenceKeys.SORT_PROJECTS_PREFERENCE_KEY;
import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons;

public abstract class RecyclerViewFragment<T extends Nameable> extends Fragment implements
ActionMode.Callback,
Expand Down Expand Up @@ -477,19 +483,38 @@ public void notifyDataSetChanged() {
}

protected void showBackpackModeChooser() {
CharSequence[] items = new CharSequence[] {getString(R.string.pack), getString(R.string.unpack)};
new AlertDialog.Builder(requireContext())
.setTitle(R.string.backpack_title)
.setItems(items, (dialog, which) -> {
switch (which) {
case 0:
startActionMode(BACKPACK);
break;
case 1:
switchToBackpack();
}
})
.show();
List<Pair<String, Integer>> items = Arrays.asList(
new Pair<>(getString(R.string.pack), R.drawable.ic_login),
new Pair<>(getString(R.string.unpack), R.drawable.ic_logout)
);

LayoutInflater inflater = LayoutInflater.from(requireContext());
View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null);
ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(),
requireContext(), items);

AlertDialog dialog = new AlertDialog.Builder(requireContext())
.setView(customDialogView)
.create();
Objects.requireNonNull(dialog.getWindow())
.setBackgroundDrawableResource(R.drawable.backpack_background_round);

ListView listView = customDialogView.findViewById(R.id.backpack_item_list);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
switch (position) {
case 0:
startActionMode(BACKPACK);
break;
case 1:
switchToBackpack();
break;
}

dialog.dismiss();
});

dialog.show();
}

protected abstract void packItems(List<T> selectedItems);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2023 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -29,6 +29,7 @@
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.util.Pair;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -37,7 +38,10 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.catrobat.catroid.BuildConfig;
import org.catrobat.catroid.ProjectManager;
Expand Down Expand Up @@ -92,7 +96,9 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import androidx.annotation.IntDef;
Expand All @@ -107,6 +113,7 @@

import static org.catrobat.catroid.common.Constants.CODE_XML_FILE_NAME;
import static org.catrobat.catroid.common.Constants.UNDO_CODE_XML_FILE_NAME;
import static org.catrobat.catroid.ui.UiUtils.getAlertDialogAdapterForTextWithIcons;

public class ScriptFragment extends ListFragment implements
ActionMode.Callback,
Expand Down Expand Up @@ -847,19 +854,38 @@ public boolean onBrickLongClick(Brick brick, int position) {
}

private void showBackpackModeChooser() {
CharSequence[] items = new CharSequence[] {getString(R.string.pack), getString(R.string.unpack)};
new AlertDialog.Builder(getContext())
.setTitle(R.string.backpack_title)
.setItems(items, (dialog, which) -> {
switch (which) {
case 0:
startActionMode(BACKPACK);
break;
case 1:
switchToBackpack();
}
})
.show();
List<Pair<String, Integer>> items = Arrays.asList(
new Pair<>(getString(R.string.pack), R.drawable.ic_login),
new Pair<>(getString(R.string.unpack), R.drawable.ic_logout)
);

LayoutInflater inflater = LayoutInflater.from(requireContext());
View customDialogView = inflater.inflate(R.layout.dialog_backpack_custom_alert, null);
ListAdapter listAdapter = getAlertDialogAdapterForTextWithIcons(requireActivity(),
requireContext(), items);

AlertDialog dialog = new AlertDialog.Builder(requireContext())
.setView(customDialogView)
.create();
Objects.requireNonNull(dialog.getWindow())
.setBackgroundDrawableResource(R.drawable.backpack_background_round);

ListView listView = customDialogView.findViewById(R.id.backpack_item_list);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
switch (position) {
case 0:
startActionMode(BACKPACK);
break;
case 1:
switchToBackpack();
break;
}

dialog.dismiss();
});

dialog.show();
}

public void showNewScriptGroupAlert(List<Brick> selectedBricks) {
Expand Down
27 changes: 27 additions & 0 deletions catroid/src/main/res/drawable/backpack_background_round.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Catroid: An on-device visual programming system for Android devices
~ Copyright (C) 2010-2024 The Catrobat Team
~ (<http://developer.catrobat.org/credits>)
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Affero General Public License as
~ published by the Free Software Foundation, either version 3 of the
~ License, or (at your option) any later version.
~
~ An additional term exception under section 7 of the GNU Affero
~ General Public License, version 3, is available at
~ http://developer.catrobat.org/license_additional_term
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Affero General Public License for more details.
~
~ You should have received a copy of the GNU Affero General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<solid android:color="@color/backpack_background" />
</shape>
Loading

0 comments on commit 55ff0f2

Please sign in to comment.