Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Update appcompat, fragment libraries and fix findViewById() in Fragme…
Browse files Browse the repository at this point in the history
…nts (#1434)

* Update appcompat, fragment and fix findViewById() in Fragments

* fix formatting

Co-authored-by: Kordian Bruck <[email protected]>
  • Loading branch information
jfoerste and kordianbruck authored May 12, 2022
1 parent fc1c941 commit c5d8968
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 48 deletions.
7 changes: 2 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'android.arch.lifecycle:reactivestreams:1.1.1'
implementation 'android.arch.work:work-runtime-ktx:1.0.1'
// Note: Appcompat 1.4 breaks sandwich menu injected into toolbar by DrawerLayout
// upgrading this and or androidx.fragment:fragment-ktx will break the toolbar
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
kapt 'androidx.lifecycle:lifecycle-compiler:2.4.1'
kapt "androidx.room:room-compiler:$room_version"

Expand Down Expand Up @@ -142,8 +140,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.anko:anko:0.10.8"
implementation 'androidx.core:core-ktx:1.7.0'
// Note: upgrading this and or androidx.appcompat:appcompat will break the toolbar
implementation 'androidx.fragment:fragment-ktx:1.2.5'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
// Note: fix for internal androidx libraries using outdated WorkManager causing a crash
implementation "androidx.work:work-runtime-ktx:2.7.1"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BaseNavigationActivity : BaseActivity(

private val fragmentCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentStarted(fm: FragmentManager, f: Fragment) {
initDrawerToggle()
initDrawerToggle(f)
updateDrawer()
}
}
Expand Down Expand Up @@ -97,9 +97,9 @@ class BaseNavigationActivity : BaseActivity(
}
}

private fun initDrawerToggle() {
private fun initDrawerToggle(fragment: Fragment) {
val drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
val toolbar = fragment.requireView().findViewById<Toolbar>(R.id.toolbar)

drawerToggle?.let { drawerLayout.removeDrawerListener(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ abstract class BaseFragment<T>(
private var apiCall: Call<T>? = null
private var hadSuccessfulRequest = false

private val toolbar: Toolbar?
get() = requireActivity().findViewById<Toolbar?>(R.id.toolbar)

private val contentView: ViewGroup
get() = requireActivity().findViewById<ViewGroup>(android.R.id.content).getChildAt(0) as ViewGroup

Expand Down Expand Up @@ -80,7 +77,11 @@ abstract class BaseFragment<T>(

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupToolbar()

view.findViewById<Toolbar>(R.id.toolbar)?.let {
setupToolbar(it)
}

// If content is refreshable setup the SwipeRefreshLayout
swipeRefreshLayout?.apply {
setOnRefreshListener(this@BaseFragment)
Expand All @@ -92,13 +93,8 @@ abstract class BaseFragment<T>(
}
}

override fun onResume() {
super.onResume()
toolbar?.setTitle(titleResId)
}

private fun setupToolbar() {
val toolbar = toolbar ?: return
private fun setupToolbar(toolbar: Toolbar) {
toolbar.setTitle(titleResId)
baseActivity.setSupportActionBar(toolbar)

baseActivity.supportActionBar?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat.checkSelfPermission
import com.alamkanak.weekview.DateTimeInterpreter
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.WeekViewDisplayable
import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding
import de.tum.`in`.tumcampusapp.R
Expand Down Expand Up @@ -49,10 +48,6 @@ class CalendarFragment : FragmentForAccessingTumOnline<EventsResponse>(
CalendarController(requireContext())
}

private val weekView: WeekView<CalendarItem> by lazy {
requireActivity().findViewById<WeekView<CalendarItem>>(R.id.weekView)
}

private val showDate: DateTime? by lazy {
val value = arguments?.getLong(Const.EVENT_TIME, -1) ?: -1
if (value != -1L) DateTime(value) else null
Expand Down Expand Up @@ -86,25 +81,28 @@ class CalendarFragment : FragmentForAccessingTumOnline<EventsResponse>(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// The week view adds a horizontal bar below the Toolbar. When refreshing, the refresh
// spinner covers it. Therefore, we adjust the spinner's end position.
swipeRefreshLayout.let {
val startOffset = it.progressViewStartOffset
val endOffset = it.progressViewEndOffset
it.setProgressViewOffset(false, startOffset, endOffset)
}
with(binding) {
// The week view adds a horizontal bar below the Toolbar. When refreshing, the refresh
// spinner covers it. Therefore, we adjust the spinner's end position.
swipeRefreshLayout.let {
val startOffset = it.progressViewStartOffset
val endOffset = it.progressViewEndOffset
it.setProgressViewOffset(false, startOffset, endOffset)
}

weekView.setOnMonthChangeListener { startDate, endDate ->
val begin = DateTime(startDate)
val end = DateTime(endDate)
prepareCalendarItems(begin, end)
}
weekView.setOnMonthChangeListener { startDate, endDate ->
val begin = DateTime(startDate)
val end = DateTime(endDate)
prepareCalendarItems(begin, end)
}

weekView.setOnEventClickListener { data, _ ->
openEvent(data as CalendarItem)
}

weekView.setOnEventClickListener { data, _ ->
openEvent(data)
todayButton.setOnClickListener { weekView.goToToday() }
}

binding.todayButton.setOnClickListener { weekView.goToToday() }
showDate?.let { openEvent(eventId) }

isWeekMode = Utils.getSettingBool(requireContext(), Const.CALENDAR_WEEK_MODE, false)
Expand Down Expand Up @@ -210,7 +208,7 @@ class CalendarFragment : FragmentForAccessingTumOnline<EventsResponse>(
return true
}
R.id.action_create_event -> {
val currentDate = LocalDate(weekView.firstVisibleDate)
val currentDate = LocalDate(binding.weekView.firstVisibleDate)
val intent = Intent(requireContext(), CreateEventActivity::class.java)
intent.putExtra(Const.DATE, currentDate)
startActivityForResult(intent, REQUEST_CREATE)
Expand Down Expand Up @@ -430,23 +428,23 @@ class CalendarFragment : FragmentForAccessingTumOnline<EventsResponse>(

if (isWeekMode) {
icon = R.drawable.ic_outline_calendar_view_day_24px
weekView.numberOfVisibleDays = 5
binding.weekView.numberOfVisibleDays = 5
} else {
icon = R.drawable.ic_outline_view_column_24px
weekView.numberOfVisibleDays = 1
binding.weekView.numberOfVisibleDays = 1
}

// Go to current date or the one given in the intent
showDate?.let {
weekView.goToDate(it.toGregorianCalendar())
weekView.goToHour(it.hourOfDay)
} ?: weekView.goToCurrentTime()
binding.weekView.goToDate(it.toGregorianCalendar())
binding.weekView.goToHour(it.hourOfDay)
} ?: binding.weekView.goToCurrentTime()

menuItemSwitchView?.setIcon(icon)
}

private fun setupDateTimeInterpreter(shortDate: Boolean) {
weekView.dateTimeInterpreter = object : DateTimeInterpreter {
binding.weekView.dateTimeInterpreter = object : DateTimeInterpreter {

private val timeFormat = DateTimeFormat.forPattern("HH:mm").withLocale(Locale.getDefault())

Expand Down Expand Up @@ -492,6 +490,12 @@ class CalendarFragment : FragmentForAccessingTumOnline<EventsResponse>(
.show()
}

override fun onDestroyView() {
super.onDestroyView()
menuItemSwitchView = null
menuItemFilterCanceled = null
}

companion object {

private const val REQUEST_SYNC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onSaveInstanceState(@NotNull Bundle outState) {
public void onViewCreated(@NotNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

listView = requireActivity().findViewById(R.id.listView);
listView = view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
listView.setAdapter(adapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EventsFragment : FragmentForDownloadingExternal(
super.onViewCreated(view, savedInstanceState)
setupViewPager(binding.viewPager)

val eventTab = requireActivity().findViewById<TabLayout>(R.id.event_tab)
val eventTab = view.findViewById<TabLayout>(R.id.event_tab)
eventTab.setupWithViewPager(binding.viewPager)

eventTab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
Expand Down

0 comments on commit c5d8968

Please sign in to comment.