@@ -29,7 +29,6 @@ import com.nextcloud.client.account.User
2929import com.nextcloud.client.account.UserAccountManager
3030import com.nextcloud.client.di.Injectable
3131import com.nextcloud.client.jobs.NotificationWork
32- import com.nextcloud.client.network.ClientFactory
3332import com.nextcloud.client.preferences.AppPreferences
3433import com.nextcloud.common.NextcloudClient
3534import com.nextcloud.utils.BuildHelper
@@ -62,15 +61,12 @@ class NotificationsFragment :
6261 private var binding: NotificationsLayoutBinding ? = null
6362 private var adapter: NotificationListAdapter ? = null
6463 private var snackbar: Snackbar ? = null
65- private var client: NextcloudClient ? = null
6664 private var optionalUser: Optional <User >? = null
6765
6866 @Inject lateinit var viewThemeUtils: ViewThemeUtils
6967
7068 @Inject lateinit var accountManager: UserAccountManager
7169
72- @Inject lateinit var clientFactory: ClientFactory
73-
7470 @Inject lateinit var preferences: AppPreferences
7571
7672 // region Lifecycle
@@ -81,12 +77,23 @@ class NotificationsFragment :
8177
8278 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
8379 super .onViewCreated(view, savedInstanceState)
84- setupMenu()
85- initUser()
86- setupSwipeRefresh()
87- setupPushWarning()
88- setupContent()
89- if (optionalUser?.isPresent == false ) showError()
80+
81+ lifecycleScope.launch {
82+ val baseActivity = getTypedActivity(BaseActivity ::class .java)
83+ val client = baseActivity?.clientRepository?.getNextcloudClient() ? : run {
84+ showError()
85+ return @launch
86+ }
87+
88+ withContext(Dispatchers .Main ) {
89+ setupMenu(client)
90+ initUser()
91+ setupSwipeRefresh(client)
92+ setupPushWarning()
93+ setupContent(client)
94+ if (optionalUser?.isPresent == false ) showError()
95+ }
96+ }
9097 }
9198
9299 override fun onDestroyView () {
@@ -104,32 +111,32 @@ class NotificationsFragment :
104111 }
105112 }
106113
107- private fun setupSwipeRefresh () {
114+ private fun setupSwipeRefresh (client : NextcloudClient ) {
108115 binding?.run {
109116 viewThemeUtils.androidx.themeSwipeRefreshLayout(swipeContainingList)
110117 viewThemeUtils.androidx.themeSwipeRefreshLayout(swipeContainingEmpty)
111118 swipeContainingList.setOnRefreshListener {
112119 setLoadingMessage()
113120 swipeContainingList.isRefreshing = true
114- fetchAndSetData()
121+ fetchAndSetData(client )
115122 }
116123 swipeContainingEmpty.setOnRefreshListener {
117124 setLoadingMessageEmpty()
118- fetchAndSetData()
125+ fetchAndSetData(client )
119126 }
120127 }
121128 }
122129
123- private fun setupContent () {
130+ private fun setupContent (client : NextcloudClient ) {
124131 binding?.run {
125132 emptyList.emptyListIcon.setImageResource(R .drawable.ic_notification)
126133 setLoadingMessageEmpty()
127134 list.layoutManager = LinearLayoutManager (requireContext())
128- fetchAndSetData()
135+ fetchAndSetData(client )
129136 }
130137 }
131138
132- private fun setupMenu () {
139+ private fun setupMenu (client : NextcloudClient ) {
133140 (requireActivity() as MenuHost ).addMenuProvider(
134141 object : MenuProvider {
135142 override fun onCreateMenu (menu : Menu , menuInflater : MenuInflater ) {
@@ -139,7 +146,7 @@ class NotificationsFragment :
139146 override fun onMenuItemSelected (item : MenuItem ): Boolean {
140147 if (item.itemId != R .id.action_empty_notifications) return false
141148 lifecycleScope.launch(Dispatchers .IO ) {
142- val result = DeleteAllNotificationsRemoteOperation ().execute(client!! )
149+ val result = DeleteAllNotificationsRemoteOperation ().execute(client)
143150 withContext(Dispatchers .Main ) { onRemovedAllNotifications(result.isSuccess) }
144151 }
145152 return true
@@ -186,13 +193,13 @@ class NotificationsFragment :
186193 // endregion
187194
188195 // region Data loading
189- private fun fetchAndSetData () {
196+ private fun fetchAndSetData (client : NextcloudClient ) {
190197 lifecycleScope.launch(Dispatchers .IO ) {
191- initializeAdapter()
192- val result = client?. let { GetNotificationsRemoteOperation ().execute(it) }
198+ initializeAdapter(client )
199+ val result = GetNotificationsRemoteOperation ().execute(client)
193200 withContext(Dispatchers .Main ) {
194201 if (result?.isSuccess == true && result.resultData != null ) {
195- populateList(result.resultData ? : listOf ())
202+ populateList(result.resultData ? : listOf (), client )
196203 } else {
197204 try {
198205 Log_OC .d(TAG , result?.logMessage)
@@ -208,17 +215,10 @@ class NotificationsFragment :
208215 }
209216 }
210217
211- private fun initializeAdapter () {
212- lifecycleScope.launch {
213- val baseActivity = getTypedActivity(BaseActivity ::class .java)
214- client = baseActivity?.clientRepository?.getNextcloudClient()
215-
216- withContext(Dispatchers .Main ) {
217- if (adapter == null ) {
218- adapter = NotificationListAdapter (client, this @NotificationsFragment, viewThemeUtils)
219- binding?.list?.adapter = adapter
220- }
221- }
218+ private fun initializeAdapter (client : NextcloudClient ) {
219+ if (adapter == null ) {
220+ adapter = NotificationListAdapter (client, this @NotificationsFragment, viewThemeUtils)
221+ binding?.list?.adapter = adapter
222222 }
223223 }
224224
@@ -229,9 +229,8 @@ class NotificationsFragment :
229229 // endregion
230230
231231 // region View state
232- @VisibleForTesting
233- fun populateList (notifications : List <Notification >) {
234- initializeAdapter()
232+ fun populateList (notifications : List <Notification >, client : NextcloudClient ) {
233+ initializeAdapter(client)
235234 adapter?.setNotificationItems(notifications)
236235 binding?.run {
237236 loadingContent.visibility = View .GONE
@@ -289,10 +288,10 @@ class NotificationsFragment :
289288 // endregion
290289
291290 // region callbacks
292- override fun onRemovedNotification (isSuccess : Boolean ) {
291+ override fun onRemovedNotification (isSuccess : Boolean , client : NextcloudClient ) {
293292 if (! isSuccess) {
294293 DisplayUtils .showSnackMessage(requireActivity(), getString(R .string.remove_notification_failed))
295- fetchAndSetData()
294+ fetchAndSetData(client )
296295 }
297296 }
298297
0 commit comments