Skip to content

Commit

Permalink
CATROID-1168 Refactor UserDefinedBrickListFragment and PrototypeBric…
Browse files Browse the repository at this point in the history
…kAdapter to kotlin
  • Loading branch information
khushal1707 committed Aug 10, 2021
1 parent f878a53 commit 5c04586
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 199 deletions.
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-2018 The Catrobat Team
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -20,41 +20,20 @@
* 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/>.
*/
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<Brick>) : 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<Brick> brickList;

public PrototypeBrickAdapter(List<Brick> 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)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 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/>.
*/
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
}
}
}

0 comments on commit 5c04586

Please sign in to comment.