Skip to content

Commit 260edd4

Browse files
committed
improve performance
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 313fda8 commit 260edd4

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

app/src/main/java/com/owncloud/android/ui/fragment/contactsbackup/BackupListAdapter.kt

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import android.view.LayoutInflater
1818
import android.view.View
1919
import android.view.ViewGroup
2020
import android.widget.AdapterView
21-
import android.widget.CheckedTextView
2221
import android.widget.ImageView
2322
import com.afollestad.sectionedrecyclerview.SectionedRecyclerViewAdapter
2423
import com.afollestad.sectionedrecyclerview.SectionedViewHolder
@@ -57,6 +56,8 @@ class BackupListAdapter(
5756
private val contacts = mutableListOf<VCard>()
5857
private val availableContactAccounts: List<ContactsAccount> = getAccountsForImport()
5958

59+
private var cachedAndroidCalendars: List<AndroidCalendar> = emptyList()
60+
6061
companion object {
6162
const val SECTION_CALENDAR = 0
6263
const val SECTION_CONTACTS = 1
@@ -67,6 +68,7 @@ class BackupListAdapter(
6768
init {
6869
shouldShowHeadersForEmptySections(false)
6970
shouldShowFooters(false)
71+
cachedAndroidCalendars = AndroidCalendar.loadAll(context.contentResolver)
7072
}
7173

7274
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SectionedViewHolder = when (viewType) {
@@ -139,6 +141,7 @@ class BackupListAdapter(
139141

140142
fun hasCalendarEntry(): Boolean = calendarFiles.isNotEmpty()
141143

144+
@SuppressLint("NotifyDataSetChanged")
142145
fun selectAll(selectAll: Boolean) {
143146
if (selectAll) {
144147
checkedVCards.addAll(contacts.indices)
@@ -147,6 +150,7 @@ class BackupListAdapter(
147150
checkedCalendars.clear()
148151
}
149152
showRestoreButton()
153+
notifyDataSetChanged()
150154
}
151155

152156
private fun bindCalendarHeader(holder: BackupListHeaderViewHolder) {
@@ -163,7 +167,6 @@ class BackupListAdapter(
163167
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
164168
backupListFragment.setSelectedAccount(availableContactAccounts[position])
165169
}
166-
167170
override fun onNothingSelected(parent: AdapterView<*>?) {
168171
backupListFragment.setSelectedAccount(null)
169172
}
@@ -179,7 +182,7 @@ class BackupListAdapter(
179182
val rawName = getDisplayName(vCard)
180183
val displayName = rawName?.takeIf { it.isNotBlank() } ?: context.getString(android.R.string.unknownName)
181184

182-
setChecked(checkedVCards.contains(position), holder.binding.name)
185+
holder.binding.name.isChecked = checkedVCards.contains(position)
183186
holder.binding.name.text = displayName
184187
viewThemeUtils.platform.themeCheckedTextView(holder.binding.name)
185188

@@ -196,21 +199,19 @@ class BackupListAdapter(
196199
val ocFile = calendarFiles[position]
197200
val storagePath = ocFile.storagePath
198201

199-
setChecked(checkedCalendars.containsKey(storagePath), holder.binding.name)
200-
202+
holder.binding.name.isChecked = checkedCalendars.containsKey(storagePath)
201203
val fileName = ocFile.fileName
202204
val calendarName = fileName.substringBefore("_")
203205
val date = fileName.substringAfterLast("_").replace(".ics", "").replace("-", ":")
204206

205207
holder.binding.name.text = context.getString(R.string.calendar_name_linewrap, calendarName, date)
206208
viewThemeUtils.platform.themeCheckedTextView(holder.binding.name)
207-
holder.setCalendars(AndroidCalendar.loadAll(context.contentResolver))
209+
holder.setCalendars(cachedAndroidCalendars)
208210

209211
holder.binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
210212
override fun onItemSelected(parent: AdapterView<*>?, view: View?, calendarPosition: Int, id: Long) {
211213
checkedCalendars[storagePath] = calendarPosition
212214
}
213-
214215
override fun onNothingSelected(parent: AdapterView<*>?) {
215216
checkedCalendars[storagePath] = -1
216217
}
@@ -231,24 +232,24 @@ class BackupListAdapter(
231232
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
232233
imageView.setImageDrawable(resource)
233234
}
234-
235235
override fun onLoadCleared(placeholder: Drawable?) {
236236
imageView.setImageDrawable(placeholder)
237237
}
238-
239238
override fun onLoadFailed(errorDrawable: Drawable?) {
240239
super.onLoadFailed(errorDrawable)
241240
imageView.setImageDrawable(errorDrawable)
242241
}
243242
}
244243

245-
GlideHelper.loadIntoTarget(
246-
backupListFragment.requireActivity(),
247-
accountManager.currentOwnCloudAccount,
248-
url,
249-
target,
250-
R.drawable.ic_user_outline
251-
)
244+
backupListFragment.activity?.let {
245+
GlideHelper.loadIntoTarget(
246+
it,
247+
accountManager.currentOwnCloudAccount,
248+
url,
249+
target,
250+
R.drawable.ic_user_outline
251+
)
252+
}
252253
}
253254
}
254255

@@ -269,14 +270,19 @@ class BackupListAdapter(
269270
val isChecked = !holder.binding.name.isChecked
270271
holder.binding.name.isChecked = isChecked
271272

273+
val wasEmpty = checkedVCards.isEmpty()
272274
if (isChecked) {
273275
checkedVCards.add(position)
274276
} else {
275277
checkedVCards.remove(position)
276278
}
277279

278280
showRestoreButton()
279-
notifySectionChanged(SECTION_CONTACTS)
281+
282+
val isNowEmpty = checkedVCards.isEmpty()
283+
if (wasEmpty != isNowEmpty) {
284+
notifySectionChanged(SECTION_CONTACTS)
285+
}
280286
}
281287

282288
private fun toggleCalendar(holder: CalendarItemViewHolder, position: Int) {
@@ -291,19 +297,13 @@ class BackupListAdapter(
291297
} else {
292298
checkedCalendars.remove(storagePath)
293299
}
294-
295300
showRestoreButton()
296301
}
297302

298-
private fun setChecked(checked: Boolean, checkedTextView: CheckedTextView) {
299-
checkedTextView.isChecked = checked
300-
}
301-
302303
private fun showRestoreButton() {
303304
val hasSelection = checkedCalendars.isNotEmpty() || checkedVCards.isNotEmpty()
304305
val hasAvailableCalendar =
305-
checkedCalendars.isEmpty() || AndroidCalendar.loadAll(context.contentResolver).isNotEmpty()
306-
306+
checkedCalendars.isEmpty() || cachedAndroidCalendars.isNotEmpty()
307307
backupListFragment.showRestoreButton(hasSelection && hasAvailableCalendar)
308308
}
309309

@@ -333,6 +333,6 @@ class BackupListAdapter(
333333
Log_OC.d(BackupListFragment.TAG, e.message)
334334
}
335335

336-
return accounts.distinct()
336+
return accounts.distinctBy { it.name to it.type }
337337
}
338338
}

0 commit comments

Comments
 (0)