Skip to content

Commit c8b3d50

Browse files
committed
fix adapter initialization
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 327eb1c commit c8b3d50

4 files changed

Lines changed: 64 additions & 49 deletions

File tree

app/src/androidTest/java/com/owncloud/android/ui/fragment/NotificationsFragmentIT.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
*/
99
package com.owncloud.android.ui.fragment
1010

11+
import android.net.Uri
1112
import androidx.test.core.app.ActivityScenario
1213
import androidx.test.espresso.Espresso.onView
1314
import androidx.test.espresso.assertion.ViewAssertions.matches
1415
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
1516
import androidx.test.espresso.matcher.ViewMatchers.isRoot
17+
import androidx.test.platform.app.InstrumentationRegistry
18+
import com.nextcloud.common.NextcloudClient
1619
import com.nextcloud.test.GrantStoragePermissionRule.Companion.grant
1720
import com.owncloud.android.AbstractIT
1821
import com.owncloud.android.lib.resources.notifications.models.Action
@@ -39,6 +42,15 @@ class NotificationsFragmentIT : AbstractIT() {
3942
return cal.time
4043
}
4144

45+
private val testClient: NextcloudClient by lazy {
46+
NextcloudClient(
47+
Uri.parse("https://cloud.example.com"),
48+
"testuser",
49+
"Basic dXNlcjpwYXNz",
50+
targetContext
51+
)
52+
}
53+
4254
private fun buildNotificationNoActions(): Notification = Notification(
4355
1,
4456
"files",
@@ -98,7 +110,8 @@ class NotificationsFragmentIT : AbstractIT() {
98110
"Help improve Nextcloud",
99111
"SubjectRich",
100112
HashMap(),
101-
"Do you want to help us to improve Nextcloud by providing some anonymize data about your setup and usage?",
113+
"Do you want to help us to improve Nextcloud by providing some anonymize data about your setup" +
114+
" and usage?",
102115
"MessageRich",
103116
HashMap(),
104117
"link",
@@ -122,9 +135,11 @@ class NotificationsFragmentIT : AbstractIT() {
122135
val intent = NavigatorActivity.intent(targetContext, NavigatorScreen.Notifications)
123136
ActivityScenario.launch<NavigatorActivity>(intent).use { scenario ->
124137
scenario.onActivity { sut ->
125-
findFragment(sut)?.populateList(ArrayList())
138+
findFragment(sut)?.populateList(ArrayList(), testClient)
126139
}
127140

141+
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
142+
128143
val screenShotName = createName(testClassName + "_" + "empty", "")
129144
onView(isRoot()).check(matches(isDisplayed()))
130145

@@ -140,7 +155,7 @@ class NotificationsFragmentIT : AbstractIT() {
140155
val intent = NavigatorActivity.intent(targetContext, NavigatorScreen.Notifications)
141156
ActivityScenario.launch<NavigatorActivity>(intent).use { scenario ->
142157
scenario.onActivity { sut ->
143-
findFragment(sut)?.populateList(buildMockNotifications())
158+
findFragment(sut)?.populateList(buildMockNotifications(), testClient)
144159
}
145160

146161
val screenShotName = createName(testClassName + "_" + "showNotifications", "")

app/src/main/java/com/owncloud/android/ui/adapter/NotificationListAdapter.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import kotlinx.coroutines.withContext
5252

5353
@Suppress("TooManyFunctions")
5454
class NotificationListAdapter(
55-
private val client: NextcloudClient?,
55+
private val client: NextcloudClient,
5656
private val fragment: NotificationsFragment,
5757
private val viewThemeUtils: ViewThemeUtils
5858
) : RecyclerView.Adapter<NotificationListAdapter.NotificationViewHolder>() {
@@ -167,12 +167,12 @@ class NotificationListAdapter(
167167
holder.binding.dismiss.setOnClickListener {
168168
fragment.lifecycleScope.launch(Dispatchers.IO) {
169169
val result =
170-
client?.let { clientValue ->
171-
DeleteNotificationRemoteOperation(notification.notificationId).execute(
172-
clientValue
173-
)
174-
}
175-
withContext(Dispatchers.Main) { fragment.onRemovedNotification(result?.isSuccess == true) }
170+
DeleteNotificationRemoteOperation(notification.notificationId).execute(
171+
client
172+
)
173+
withContext(Dispatchers.Main) {
174+
fragment.onRemovedNotification(result?.isSuccess == true, client)
175+
}
176176
}
177177
}
178178

@@ -272,7 +272,7 @@ class NotificationListAdapter(
272272
Intent(Intent.ACTION_VIEW).apply { data = action.link?.toUri() }
273273
)
274274
} else {
275-
client?.let { NotificationExecuteActionTask(it, holder, notification, fragment) }?.execute(action)
275+
NotificationExecuteActionTask(client, holder, notification, fragment).execute(action)
276276
}
277277
}
278278

app/src/main/java/com/owncloud/android/ui/fragment/notifications/NotificationsFragment.kt

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.nextcloud.client.account.User
2929
import com.nextcloud.client.account.UserAccountManager
3030
import com.nextcloud.client.di.Injectable
3131
import com.nextcloud.client.jobs.NotificationWork
32-
import com.nextcloud.client.network.ClientFactory
3332
import com.nextcloud.client.preferences.AppPreferences
3433
import com.nextcloud.common.NextcloudClient
3534
import 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

app/src/main/java/com/owncloud/android/ui/notifications/NotificationsContract.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*/
88
package com.owncloud.android.ui.notifications
99

10+
import com.nextcloud.common.NextcloudClient
1011
import com.owncloud.android.lib.resources.notifications.models.Notification
1112
import com.owncloud.android.ui.adapter.NotificationListAdapter.NotificationViewHolder
1213

1314
interface NotificationsContract {
1415
interface View {
15-
fun onRemovedNotification(isSuccess: Boolean)
16+
fun onRemovedNotification(isSuccess: Boolean, client: NextcloudClient)
1617

1718
fun removeNotification(holder: NotificationViewHolder)
1819

0 commit comments

Comments
 (0)