Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor home #215

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HomeFragment : MainFragment() {
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
if (tags.size == 1) binding.tabs.isVisible = false
binding.pager.adapter = HomeAdapter(this)
binding.pager.adapter = TagPagerAdapter(this)
TabLayoutMediator(binding.tabs, binding.pager) { tab, position ->
tab.text = tags[position].first
}.attach()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import com.aistra.hail.utils.HPackages.myUserId
import com.google.android.material.color.MaterialColors
import kotlinx.coroutines.Job

class PagerAdapter(private val selectedList: List<AppInfo>) :
ListAdapter<AppInfo, PagerAdapter.ViewHolder>(HomeDiff(selectedList)) {
class TagAppsAdapter(private val selectedList: List<AppInfo>) :
ListAdapter<AppInfo, TagAppsAdapter.ViewHolder>(HomeDiff(selectedList)) {
private var loadIconJob: Job? = null
lateinit var onItemClickListener: OnItemClickListener
lateinit var onItemLongClickListener: OnItemLongClickListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.aistra.hail.app.HailData.tags

class HomeAdapter(fragment: HomeFragment) : FragmentStateAdapter(fragment) {
class TagPagerAdapter(fragment: HomeFragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = tags.size

override fun createFragment(position: Int): Fragment = PagerFragment()
override fun createFragment(position: Int): Fragment = TagPagerFragment()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,38 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONArray

class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAdapter.OnItemLongClickListener,
class TagPagerFragment : MainFragment(), TagAppsAdapter.OnItemClickListener, TagAppsAdapter.OnItemLongClickListener,
MenuProvider {
private var query: String = String()
private var _binding: FragmentPagerBinding? = null
private val binding get() = _binding!!
private lateinit var pagerAdapter: PagerAdapter
private lateinit var tagAppsAdapter: TagAppsAdapter
private var multiselect: Boolean
set(value) {
(parentFragment as HomeFragment).multiselect = value
}
get() = (parentFragment as HomeFragment).multiselect
private val selectedList get() = (parentFragment as HomeFragment).selectedList
private val tabs: TabLayout get() = (parentFragment as HomeFragment).binding.tabs
private val adapter get() = (parentFragment as HomeFragment).binding.pager.adapter as HomeAdapter
private val adapter get() = (parentFragment as HomeFragment).binding.pager.adapter as TagPagerAdapter
private val tag: Pair<String, Int> get() = HailData.tags[tabs.selectedTabPosition]
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
val menuHost = requireActivity() as MenuHost
menuHost.addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
_binding = FragmentPagerBinding.inflate(inflater, container, false)
pagerAdapter = PagerAdapter(selectedList).apply {
onItemClickListener = this@PagerFragment
onItemLongClickListener = this@PagerFragment
tagAppsAdapter = TagAppsAdapter(selectedList).apply {
onItemClickListener = this@TagPagerFragment
onItemLongClickListener = this@TagPagerFragment
}
binding.recyclerView.run {
layoutManager = GridLayoutManager(
activity, resources.getInteger(
if (HailData.compactIcon) R.integer.home_span_compact else R.integer.home_span
)
)
adapter = pagerAdapter
adapter = tagAppsAdapter
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Expand Down Expand Up @@ -103,7 +103,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
true
}
activity.fab.setOnClickListener {
setListFrozen(true, pagerAdapter.currentList.filterNot { it.whitelisted })
setListFrozen(true, tagAppsAdapter.currentList.filterNot { it.whitelisted })
}
activity.fab.setOnLongClickListener {
setListFrozen(true)
Expand All @@ -120,7 +120,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
) || PinyinSearch.searchPinyinAll(it.name.toString(), query))
}.sortedWith(NameComparator).let {
binding.empty.isVisible = it.isEmpty()
pagerAdapter.submitList(it)
tagAppsAdapter.submitList(it)
app.setAutoFreezeService()
}

Expand Down Expand Up @@ -311,7 +311,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
}.setNegativeButton(R.string.action_deselect) { _, _ ->
deselect()
}.setNeutralButton(R.string.action_select_all) { _, _ ->
selectedList.addAll(pagerAdapter.currentList.filterNot { it in selectedList })
selectedList.addAll(tagAppsAdapter.currentList.filterNot { it in selectedList })
updateCurrentList()
updateBarTitle()
}.show()
Expand Down Expand Up @@ -379,7 +379,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
removeAt(position)
add(position, tagName to if (defaultTab) 0 else tagId)
}
if (!defaultTab) pagerAdapter.currentList.forEach { it.tagId = tagId }
if (!defaultTab) tagAppsAdapter.currentList.forEach { it.tagId = tagId }
adapter.notifyItemChanged(position)
}
HailData.saveApps()
Expand All @@ -388,7 +388,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
val position = tabs.selectedTabPosition
if (list != null || position == 0) return@apply
setNeutralButton(R.string.action_tag_remove) { _, _ ->
pagerAdapter.currentList.forEach { it.tagId = 0 }
tagAppsAdapter.currentList.forEach { it.tagId = 0 }
tabs.selectTab(tabs.getTabAt(0))
HailData.tags.removeAt(position)
adapter.notifyItemRemoved(position)
Expand Down Expand Up @@ -462,9 +462,9 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
} else deselect()
}

R.id.action_freeze_current -> setListFrozen(true, pagerAdapter.currentList.filterNot { it.whitelisted })
R.id.action_freeze_current -> setListFrozen(true, tagAppsAdapter.currentList.filterNot { it.whitelisted })

R.id.action_unfreeze_current -> setListFrozen(false, pagerAdapter.currentList)
R.id.action_unfreeze_current -> setListFrozen(false, tagAppsAdapter.currentList)
R.id.action_freeze_all -> setListFrozen(true)
R.id.action_unfreeze_all -> setListFrozen(false)
R.id.action_freeze_non_whitelisted -> setListFrozen(true, HailData.checkedList.filterNot { it.whitelisted })
Expand All @@ -479,7 +479,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
HUI.showToast(getString(R.string.msg_imported, size.toString()))
}

R.id.action_export_current -> exportToClipboard(pagerAdapter.currentList)
R.id.action_export_current -> exportToClipboard(tagAppsAdapter.currentList)
R.id.action_export_all -> exportToClipboard(HailData.checkedList)
}
return false
Expand Down Expand Up @@ -509,7 +509,7 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener, PagerAda
}

override fun onDestroyView() {
pagerAdapter.onDestroy()
tagAppsAdapter.onDestroy()
super.onDestroyView()
_binding = null
}
Expand Down