forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# This is a combination of 5 commits.
# This is the 1st commit message: View holders and interactors for recently saved bookmarks # This is the commit message #2: Recent bookmark item view holder binding # This is the commit message mozilla-mobile#3: Create adapter for recent bookmarks. Implement controller methods. Implement view holder bindings for items # This is the commit message mozilla-mobile#4: Top level adapter for recent bookmarks section # This is the commit message mozilla-mobile#5: Retrieve list of recent bookmarks on home
- Loading branch information
Elise Richards
committed
Jun 10, 2021
1 parent
17f541a
commit 2b17998
Showing
13 changed files
with
384 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/src/main/java/org/mozilla/fenix/home/sessioncontrol/RecentBookmarksAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.home.sessioncontrol | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import kotlinx.android.synthetic.main.component_bookmark.view.* | ||
import mozilla.components.concept.storage.BookmarkNode | ||
import org.mozilla.fenix.home.sessioncontrol.viewholders.recentbookmarks.RecentBookmarksViewHolder | ||
|
||
class RecentBookmarksAdapter( | ||
private val interactor: SessionControlInteractor | ||
) : ListAdapter<List<BookmarkNode>, RecentBookmarksViewHolder>(RecentBookmarksListDiffCallback) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecentBookmarksViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(RecentBookmarksViewHolder.LAYOUT_ID, parent, false) | ||
return RecentBookmarksViewHolder(view, interactor) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: RecentBookmarksViewHolder, | ||
position: Int, | ||
payloads: MutableList<Any> | ||
) { | ||
if (payloads.isNullOrEmpty()) { | ||
onBindViewHolder(holder, position) | ||
} else { | ||
if (payloads[0] is AdapterItem.RecentBookmarksPayload) { | ||
val adapter = holder.itemView.bookmark_list.adapter as RecentBookmarksItemAdapter | ||
val payload = payloads[0] as AdapterItem.RecentBookmarksPayload | ||
for (item in payload.changed) { | ||
adapter.notifyItemChanged( | ||
item.first % RecentBookmarksViewHolder.MAX_BOOKMARKS, | ||
RecentBookmarksItemAdapter.RecentBookmarkItemPayload(item.second) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecentBookmarksViewHolder, position: Int) { | ||
val adapter = holder.itemView.bookmark_list.adapter as RecentBookmarksItemAdapter | ||
adapter.submitList(getItem(position)) | ||
} | ||
|
||
internal object RecentBookmarksListDiffCallback : DiffUtil.ItemCallback<List<BookmarkNode>>() { | ||
override fun areItemsTheSame( | ||
oldItem: List<BookmarkNode>, newItem: List<BookmarkNode> | ||
): Boolean { | ||
return oldItem.size == newItem.size | ||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: List<BookmarkNode>, newItem: List<BookmarkNode> | ||
): Boolean { | ||
return newItem.zip(oldItem).all { (new, old) -> new == old } | ||
} | ||
|
||
override fun getChangePayload( | ||
oldItem: List<BookmarkNode>, newItem: List<BookmarkNode> | ||
): Any? { | ||
val changed = mutableSetOf<Pair<Int, BookmarkNode>>() | ||
for ((index, item) in newItem.withIndex()) { | ||
if (oldItem.getOrNull(index) != item) { | ||
changed.add(Pair(index, item)) | ||
} | ||
} | ||
return if (changed.isNotEmpty()) AdapterItem.RecentBookmarksPayload(changed) else null | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/java/org/mozilla/fenix/home/sessioncontrol/RecentBookmarksItemAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.home.sessioncontrol | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import mozilla.components.concept.storage.BookmarkNode | ||
import org.mozilla.fenix.home.sessioncontrol.viewholders.recentbookmarks.RecentBookmarkItemViewHolder | ||
|
||
class RecentBookmarksItemAdapter( | ||
private val interactor: SessionControlInteractor | ||
) : ListAdapter<BookmarkNode, RecentBookmarkItemViewHolder>(RecentBookmarksDiffCallback) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecentBookmarkItemViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(RecentBookmarkItemViewHolder.LAYOUT_ID, parent, false) | ||
return RecentBookmarkItemViewHolder(view, interactor) | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecentBookmarkItemViewHolder, position: Int) { | ||
// StartupTimeline.onTopSitesItemBound(holder) | ||
holder.bind(getItem(position)) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: RecentBookmarkItemViewHolder, | ||
position: Int, | ||
payloads: MutableList<Any> | ||
) { | ||
if (payloads.isNullOrEmpty()) { | ||
onBindViewHolder(holder, position) | ||
} else { | ||
when (payloads[0]) { | ||
is BookmarkNode -> { | ||
holder.bind((payloads[0] as BookmarkNode)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
data class RecentBookmarkItemPayload( | ||
val newInstance: BookmarkNode | ||
) | ||
|
||
// object RecentBookmarksHeader : AdapterItem(RecentBookmarksHeaderViewHolder.LAYOUT_ID) | ||
// object RecentBookmarkItem : AdapterItem(.LAYOUT_ID) | ||
|
||
internal object RecentBookmarksDiffCallback : DiffUtil.ItemCallback<BookmarkNode>() { | ||
override fun areItemsTheSame(oldItem: BookmarkNode, newItem: BookmarkNode) = oldItem.guid == newItem.guid | ||
|
||
override fun areContentsTheSame(oldItem: BookmarkNode, newItem: BookmarkNode) = | ||
oldItem.guid == newItem.guid && | ||
oldItem.parentGuid == newItem.parentGuid && | ||
oldItem.title == newItem.title && | ||
oldItem.url == newItem.url && | ||
oldItem.type == newItem.type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.