From 5c045861c1743e51ea3aa7bdb5bb1d872cd4df5a Mon Sep 17 00:00:00 2001 From: Khushal Uttam Date: Mon, 26 Jul 2021 21:13:58 +0530 Subject: [PATCH] CATROID-1168 Refactor UserDefinedBrickListFragment and PrototypeBrickAdapter to kotlin --- ...kAdapter.java => PrototypeBrickAdapter.kt} | 45 ++--- .../UserDefinedBrickListFragment.java | 166 ------------------ .../fragment/UserDefinedBrickListFragment.kt | 160 +++++++++++++++++ 3 files changed, 172 insertions(+), 199 deletions(-) rename catroid/src/main/java/org/catrobat/catroid/ui/adapter/{PrototypeBrickAdapter.java => PrototypeBrickAdapter.kt} (54%) delete mode 100644 catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.java create mode 100644 catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java b/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.kt similarity index 54% rename from catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java rename to catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.kt index 06f46a470bd..211ddebfbe3 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.kt @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2018 The Catrobat Team + * Copyright (C) 2010-2021 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -20,41 +20,20 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package org.catrobat.catroid.ui.adapter; +package org.catrobat.catroid.ui.adapter -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; +import android.view.View +import android.view.ViewGroup +import android.widget.BaseAdapter +import org.catrobat.catroid.content.bricks.Brick -import org.catrobat.catroid.content.bricks.Brick; +class PrototypeBrickAdapter(private val brickList: List) : BaseAdapter() { + override fun getCount(): Int = brickList.size -import java.util.List; + override fun getItem(position: Int): Brick = brickList[position] -public class PrototypeBrickAdapter extends BaseAdapter { + override fun getItemId(position: Int): Long = position.toLong() - private List brickList; - - public PrototypeBrickAdapter(List brickList) { - this.brickList = brickList; - } - - @Override - public int getCount() { - return brickList.size(); - } - - @Override - public Brick getItem(int position) { - return brickList.get(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return brickList.get(position).getPrototypeView(parent.getContext()); - } + override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? = + brickList[position].getPrototypeView(parent?.context) } diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.java deleted file mode 100644 index dd252bd96dc..00000000000 --- a/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2021 The Catrobat Team - * () - * - * 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 . - */ - -package org.catrobat.catroid.ui.fragment; - -import android.content.Context; -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageButton; - -import org.catrobat.catroid.R; -import org.catrobat.catroid.content.bricks.Brick; -import org.catrobat.catroid.content.bricks.UserDefinedBrick; -import org.catrobat.catroid.ui.adapter.PrototypeBrickAdapter; -import org.catrobat.catroid.utils.ToastUtil; - -import java.util.List; - -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.fragment.app.ListFragment; - -public class UserDefinedBrickListFragment extends ListFragment implements View.OnClickListener { - - public static final String USER_DEFINED_BRICK_LIST_FRAGMENT_TAG = - AddBrickFragment.class.getSimpleName(); - - private AddBrickFragment.OnAddBrickListener addBrickListener; - - private ImageButton addUserDefinedBrickButton; - private PrototypeBrickAdapter adapter; - - public static UserDefinedBrickListFragment newInstance(AddBrickFragment.OnAddBrickListener addBrickListener) { - UserDefinedBrickListFragment fragment = new UserDefinedBrickListFragment(); - - fragment.addBrickListener = addBrickListener; - return fragment; - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - - AppCompatActivity activity = (AppCompatActivity) getActivity(); - if (activity != null) { - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.categories); - } - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_user_defined_brick_list, container, false); - - addUserDefinedBrickButton = view.findViewById(R.id.button_add_user_brick); - addUserDefinedBrickButton.setOnClickListener(this); - - setupUserDefinedBrickListView(); - - AppCompatActivity activity = (AppCompatActivity) getActivity(); - if (activity != null) { - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.category_user_bricks); - } - } - - return view; - } - - @Override - public void onStart() { - super.onStart(); - getListView().setOnItemClickListener((parent, view, position, id) -> addUserDefinedBrickToScript(adapter.getItem(position))); - } - - private void setupUserDefinedBrickListView() { - Context context = getActivity(); - CategoryBricksFactory categoryBricksFactory = new CategoryBricksFactory(); - - if (context != null) { - List brickList = - categoryBricksFactory.getBricks(getString(R.string.category_user_bricks), false, context); - adapter = new PrototypeBrickAdapter(brickList); - setListAdapter(adapter); - } - } - - @Override - public void onClick(View v) { - AddUserDefinedBrickFragment addUserDefinedBrickFragment = - AddUserDefinedBrickFragment.newInstance(addBrickListener); - - UserDefinedBrick userDefinedBrick = new UserDefinedBrick(); - Bundle bundle = new Bundle(); - bundle.putSerializable(UserDefinedBrick.USER_BRICK_BUNDLE_ARGUMENT, userDefinedBrick); - addUserDefinedBrickFragment.setArguments(bundle); - - FragmentManager fragmentManager = getFragmentManager(); - if (fragmentManager != null) { - fragmentManager.beginTransaction() - .add(R.id.fragment_container, addUserDefinedBrickFragment, AddUserDefinedBrickFragment.TAG) - .addToBackStack(AddUserDefinedBrickFragment.TAG) - .commit(); - } - } - - private void addUserDefinedBrickToScript(Brick userDefinedBrickToAdd) { - try { - Brick clonedBrick = userDefinedBrickToAdd.clone(); - if (userDefinedBrickToAdd instanceof UserDefinedBrick) { - ((UserDefinedBrick) clonedBrick).setCallingBrick(true); - } - addBrickListener.addBrick(clonedBrick); - - FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); - Fragment categoryFragment = getFragmentManager() - .findFragmentByTag(BrickCategoryFragment.BRICK_CATEGORY_FRAGMENT_TAG); - - if (categoryFragment != null) { - fragmentTransaction.remove(categoryFragment); - getFragmentManager().popBackStack(); - } - - Fragment addBrickFragment = getFragmentManager().findFragmentByTag(USER_DEFINED_BRICK_LIST_FRAGMENT_TAG); - - if (addBrickFragment != null) { - fragmentTransaction.remove(addBrickFragment); - getFragmentManager().popBackStack(); - } - - fragmentTransaction.commit(); - } catch (CloneNotSupportedException e) { - Log.e(getTag(), e.getLocalizedMessage()); - ToastUtil.showError(getActivity(), R.string.error_adding_brick); - } - } -} diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.kt new file mode 100644 index 00000000000..bbf0a2503cb --- /dev/null +++ b/catroid/src/main/java/org/catrobat/catroid/ui/fragment/UserDefinedBrickListFragment.kt @@ -0,0 +1,160 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2021 The Catrobat Team + * () + * + * 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 . + */ +package org.catrobat.catroid.ui.fragment + +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.ListFragment +import org.catrobat.catroid.R +import org.catrobat.catroid.content.bricks.Brick +import org.catrobat.catroid.content.bricks.UserDefinedBrick +import org.catrobat.catroid.databinding.FragmentUserDefinedBrickListBinding +import org.catrobat.catroid.ui.adapter.PrototypeBrickAdapter +import org.catrobat.catroid.ui.fragment.AddBrickFragment.OnAddBrickListener +import org.catrobat.catroid.utils.ToastUtil + +class UserDefinedBrickListFragment : ListFragment(), View.OnClickListener { + + private var addBrickListener: OnAddBrickListener? = null + private var adapter: PrototypeBrickAdapter? = null + + private var _binding: FragmentUserDefinedBrickListBinding? = null + private val binding get() = _binding!! + + override fun onDestroyView() { + super.onDestroyView() + setActionBar(R.string.categories) + _binding = null + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentUserDefinedBrickListBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.buttonAddUserBrick.setOnClickListener(this) + + setupUserDefinedBrickListView() + setActionBar(R.string.category_user_bricks) + } + + private fun setActionBar(resId: Int) { + val activity = activity as AppCompatActivity? + val actionBar = activity?.supportActionBar + actionBar?.setTitle(resId) + } + + override fun onStart() { + super.onStart() + listView.onItemClickListener = + AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, position: Int, _: Long -> + adapter?.getItem(position)?.let { + addUserDefinedBrickToScript(it) + } + } + } + + private fun setupUserDefinedBrickListView() { + val categoryBricksFactory = CategoryBricksFactory() + requireContext().let { + val brickList = categoryBricksFactory.getBricks( + getString(R.string.category_user_bricks), + false, + it + ) + adapter = PrototypeBrickAdapter(brickList) + listAdapter = adapter + } + } + + override fun onClick(v: View) { + val addUserDefinedBrickFragment = AddUserDefinedBrickFragment.newInstance(addBrickListener) + val userDefinedBrick = UserDefinedBrick() + + val bundle = Bundle() + bundle.putSerializable(UserDefinedBrick.USER_BRICK_BUNDLE_ARGUMENT, userDefinedBrick) + addUserDefinedBrickFragment.arguments = bundle + + val fragmentManager = parentFragmentManager + fragmentManager.beginTransaction() + .add( + R.id.fragment_container, + addUserDefinedBrickFragment, + AddUserDefinedBrickFragment.TAG + ) + .addToBackStack(AddUserDefinedBrickFragment.TAG).commit() + } + + private fun addUserDefinedBrickToScript(userDefinedBrickToAdd: Brick) { + try { + val clonedBrick = userDefinedBrickToAdd.clone() + if (userDefinedBrickToAdd is UserDefinedBrick) { + (clonedBrick as UserDefinedBrick).setCallingBrick(true) + } + addBrickListener?.addBrick(clonedBrick) + + val fragmentTransaction = parentFragmentManager.beginTransaction() + + val categoryFragment = + parentFragmentManager.findFragmentByTag(BrickCategoryFragment.BRICK_CATEGORY_FRAGMENT_TAG) + categoryFragment?.let { + fragmentTransaction.remove(it) + parentFragmentManager.popBackStack() + } + + val addBrickFragment = + parentFragmentManager.findFragmentByTag(USER_DEFINED_BRICK_LIST_FRAGMENT_TAG) + addBrickFragment?.let { + fragmentTransaction.remove(addBrickFragment) + parentFragmentManager.popBackStack() + } + fragmentTransaction.commit() + } catch (e: CloneNotSupportedException) { + Log.e(tag, e.message) + ToastUtil.showError(requireContext(), R.string.error_adding_brick) + } + } + + companion object { + @JvmField + val USER_DEFINED_BRICK_LIST_FRAGMENT_TAG = AddBrickFragment::class.java.simpleName + + @JvmStatic + fun newInstance(addBrickListener: OnAddBrickListener?): UserDefinedBrickListFragment { + val fragment = UserDefinedBrickListFragment() + fragment.addBrickListener = addBrickListener + return fragment + } + } +}