Commit f8d29aa4 authored by Aayush Gupta's avatar Aayush Gupta
Browse files

Apps: Switch to require API instead of get API



AndroidX added new "require____()" versions of common "get___()" APIs, such as getContext/getActivity/getArguments/etc.
Rather than wrap these in something like requireNotNull(), using these APIs will allow the underlying component to try
to tell you _why_ it was null, and thus yield a better error message.
Signed-off-by: Aayush Gupta's avatarAayush Gupta <theimpulson@e.email>
parent aa98cc89
......@@ -43,13 +43,13 @@ class ApplicationsFragment() : Fragment() {
val view = inflater.inflate(R.layout.fragment_application_categories, container, false)
view.categories_list.layoutManager = LinearLayoutManager(context)
color = arguments!!.getInt("color",0)
color = requireArguments().getInt("color",0)
view.categories_list.visibility = View.GONE
view.progress_bar.visibility = View.VISIBLE
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
view.error_resolve.setTextColor(Color.parseColor("#ffffff"))
......@@ -72,7 +72,7 @@ class ApplicationsFragment() : Fragment() {
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = activity!!.getString(it.description)
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
} else {
......@@ -81,7 +81,7 @@ class ApplicationsFragment() : Fragment() {
})
if (categoriesViewModel.getApplicationsCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
return view
}
......
......@@ -36,8 +36,8 @@ class CategoriesFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_categories, container, false)
val tabLayout = view.findViewById<TabLayout>(R.id.tab_layout)
val viewPager = view.findViewById<ViewPager>(R.id.view_pager)
var color = getAccentColor(activity!!);
viewPager.adapter = CategoriesViewPagerAdapter(activity!!.supportFragmentManager, tabLayout.tabCount, color)
var color = getAccentColor(requireActivity());
viewPager.adapter = CategoriesViewPagerAdapter(requireActivity().supportFragmentManager, tabLayout.tabCount, color)
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
if (color != null) {
......
......@@ -42,13 +42,13 @@ class GamesFragment() : Fragment() {
val view = inflater.inflate(R.layout.fragment_games_categories, container, false)
view.categories_list.layoutManager = LinearLayoutManager(context)
color = arguments!!.getInt("color",0)
color = requireArguments().getInt("color",0)
view.categories_list.visibility = View.GONE
view.progress_bar.visibility = View.VISIBLE
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
view.error_resolve.setTextColor(Color.parseColor("#ffffff"))
if (color != null) {
......@@ -68,7 +68,7 @@ class GamesFragment() : Fragment() {
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = activity!!.getString(it.description)
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
} else {
......@@ -77,7 +77,7 @@ class GamesFragment() : Fragment() {
})
if (categoriesViewModel.getGamesCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
return view
}
......
......@@ -45,7 +45,7 @@ class PwasFragment : Fragment() {
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
// Bind to the list of pwas categories
......@@ -60,7 +60,7 @@ class PwasFragment : Fragment() {
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = activity!!.getString(it.description)
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
} else {
......@@ -69,7 +69,7 @@ class PwasFragment : Fragment() {
})
if (categoriesViewModel.getPwasCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(context!!)
categoriesViewModel.loadCategories(requireContext())
}
return view
}
......
......@@ -89,13 +89,13 @@ class HomeFragment : Fragment() {
errorContainer.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
progressBar.visibility = View.VISIBLE
homeViewModel.loadCategories(context!!)
homeViewModel.loadCategories(requireContext())
}
// Bind image carousel adapter to banner images in view model
homeViewModel.getBannerApplications().observe(viewLifecycleOwner, Observer {
if (homeViewModel.getBannerApplications().value!!.isNotEmpty()) {
imageCarousel.adapter = ImageCarouselAdapter(activity!!, homeViewModel.getBannerApplications().value!!)
imageCarousel.adapter = ImageCarouselAdapter(requireActivity(), homeViewModel.getBannerApplications().value!!)
imageCarousel.clipToPadding = false;
imageCarousel.setPadding(170, 10, 170, 10);
imageCarousel.pageMargin =50
......@@ -119,7 +119,7 @@ class HomeFragment : Fragment() {
// Bind to the screen error
homeViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
errorDescription.text = activity!!.getString(it.description)
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
progressBar.visibility = View.GONE
} else {
......@@ -129,7 +129,7 @@ class HomeFragment : Fragment() {
if (homeViewModel.getBannerApplications().value!!.isEmpty() ||
homeViewModel.getCategories().value!!.isEmpty()) {
homeViewModel.loadCategories(context!!)
homeViewModel.loadCategories(requireContext())
}
return view
}
......@@ -137,16 +137,16 @@ class HomeFragment : Fragment() {
private fun setCustomScroller() {
val scroller = ViewPager::class.java.getDeclaredField("mScroller")
scroller.isAccessible = true
scroller.set(imageCarousel, ImageCarouselScroller(context!!))
scroller.set(imageCarousel, ImageCarouselScroller(requireContext()))
}
private fun showCategories(categories: LinkedHashMap<Category, ArrayList<Application>>) {
categoryList.removeAllViews()
categories.forEach {
val homeCategory = HomeCategory(context!!, it.key)
val homeCategory = HomeCategory(requireContext(), it.key)
val applicationList = homeCategory.findViewById<RecyclerView>(R.id.application_list)
applicationList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
applicationList.adapter = SmallApplicationListAdapter(activity!!, it.value)
applicationList.adapter = SmallApplicationListAdapter(requireActivity(), it.value)
categoryList.addView(homeCategory)
}
}
......@@ -156,7 +156,7 @@ class HomeFragment : Fragment() {
if (::homeViewModel.isInitialized) {
homeViewModel.getCategories().value!!.values.forEach {
it.forEach { application ->
application.checkForStateUpdate(context!!)
application.checkForStateUpdate(requireContext())
}
}
}
......
......@@ -112,7 +112,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
// Initialise recycler view
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = ApplicationListAdapter(activity!!, applicationList, 0)
recyclerView.adapter = ApplicationListAdapter(requireActivity(), applicationList, 0)
loadMoreContainer.visibility = View.GONE
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
......@@ -160,7 +160,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
if (it != null) {
if (!isLoadingMoreApplications) {
applicationList.clear()
errorDescription.text = activity!!.getString(it.description)
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
progressBar.visibility = View.GONE
loadMoreContainer.visibility = View.GONE
......@@ -194,7 +194,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
recyclerView.visibility = View.GONE
progressBar.visibility = View.VISIBLE
splashContainer.visibility = View.GONE
searchViewModel.onSearchQuerySubmitted(context!!,query)
searchViewModel.onSearchQuerySubmitted(requireContext(),query)
}
}
......@@ -206,14 +206,14 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
recyclerView.visibility = View.GONE
progressBar.visibility = View.VISIBLE
splashContainer.visibility = View.GONE
searchViewModel.onSearchQuerySubmitted(context!!, it)
searchViewModel.onSearchQuerySubmitted(requireContext(), it)
}
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
searchView.query?.let {
searchViewModel.onSearchQueryChanged(context!!, it.toString())
searchViewModel.onSearchQueryChanged(requireContext(), it.toString())
}
return true
}
......@@ -265,7 +265,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
if (::searchViewModel.isInitialized) {
searchViewModel.getApplications().value?.let {
it.forEach { application ->
application.checkForStateUpdate(context!!)
application.checkForStateUpdate(requireContext())
}
}
}
......
......@@ -54,7 +54,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val updateCheckInterval =
preferenceManager.findPreference<Preference>(getString(R.string.pref_update_interval_key)) as ListPreference
updateCheckInterval.setOnPreferenceChangeListener { _, newValue ->
UpdatesManager(activity!!.applicationContext).replaceWorker(newValue.toString().toInt())
UpdatesManager(requireActivity().applicationContext).replaceWorker(newValue.toString().toInt())
true
}
......@@ -119,7 +119,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
activity!!.finish()
requireActivity().finish()
}
}
worker.schedule(task, 1, TimeUnit.SECONDS)
......
......@@ -87,7 +87,7 @@ class UpdatesFragment() : Fragment() {
updateAll.setOnClickListener {
applicationList.forEach { application ->
if (application.state == State.NOT_UPDATED) {
application.buttonClicked(context!!, activity!!)
application.buttonClicked(requireContext(), requireActivity())
}
}
}
......@@ -98,13 +98,13 @@ class UpdatesFragment() : Fragment() {
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
updateAll.isEnabled = false
progressBar.visibility = View.VISIBLE
updatesViewModel.loadApplicationList(context!!)
updatesViewModel.loadApplicationList(requireContext())
}
// Initialise recycler view
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = ApplicationListAdapter(activity!!, applicationList, accentColorOS)
recyclerView.adapter = ApplicationListAdapter(requireActivity(), applicationList, accentColorOS)
// Bind recycler view adapter to outdated applications list in view model
updatesViewModel.getApplications().observe(viewLifecycleOwner, Observer {
......@@ -132,7 +132,7 @@ class UpdatesFragment() : Fragment() {
// Bind to the screen error
updatesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
errorDescription.text = activity!!.getString(it.description)
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
updateAll.isEnabled = false
progressBar.visibility = View.GONE
......@@ -144,7 +144,7 @@ class UpdatesFragment() : Fragment() {
errorContainer.visibility = View.GONE
}
})
updatesViewModel.loadApplicationList(context!!)
updatesViewModel.loadApplicationList(requireContext())
return view
......@@ -156,7 +156,7 @@ class UpdatesFragment() : Fragment() {
updatesViewModel.getApplications().value?.let {
it.forEach { application ->
progressBar2.visibility=View.VISIBLE
application.checkForStateUpdate(context!!)
application.checkForStateUpdate(requireContext())
}
val handler = Handler()
handler.postDelayed({
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment