-
-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Explore Feed as an Entry point for Places (#4824)
* Add Explore Feed as an Entry point for Places * Add card view * Add w logo with circle * Update asset * Build places card view * Card client * Build card behavior * Update client * Show card properly * Update card content and pick places from 1 to 10 * Add to dev preference * Update card for primary language only * Update click listener * Adjust margin * Update drawable and use proper newIntent * Update pop up menu logic * Add location comparasion * Refresh feed after 500L * Update card title * Only refresh when granting permission * Dont refresh the feed * Fix lint * Code review comments --------- Co-authored-by: Dmitry Brant <[email protected]>
- Loading branch information
Showing
15 changed files
with
518 additions
and
1 deletion.
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
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
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,30 @@ | ||
package org.wikipedia.feed.places | ||
|
||
import org.wikipedia.R | ||
import org.wikipedia.WikipediaApp | ||
import org.wikipedia.dataclient.WikiSite | ||
import org.wikipedia.dataclient.page.NearbyPage | ||
import org.wikipedia.feed.model.CardType | ||
import org.wikipedia.feed.model.WikiSiteCard | ||
import org.wikipedia.util.DateUtil | ||
|
||
class PlacesCard(wiki: WikiSite, | ||
val age: Int, | ||
val nearbyPage: NearbyPage? = null) : WikiSiteCard(wiki) { | ||
|
||
override fun type(): CardType { | ||
return CardType.PLACES | ||
} | ||
|
||
override fun title(): String { | ||
return WikipediaApp.instance.getString(R.string.places_card_title) | ||
} | ||
|
||
override fun subtitle(): String { | ||
return DateUtil.getFeedCardDateString(age) | ||
} | ||
|
||
fun footerActionText(): String { | ||
return WikipediaApp.instance.getString(R.string.places_card_action) | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
app/src/main/java/org/wikipedia/feed/places/PlacesCardView.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,114 @@ | ||
package org.wikipedia.feed.places | ||
|
||
import android.content.Context | ||
import android.location.Location | ||
import android.view.LayoutInflater | ||
import androidx.core.view.isVisible | ||
import org.wikipedia.R | ||
import org.wikipedia.databinding.ViewPlacesCardBinding | ||
import org.wikipedia.feed.view.CardFooterView | ||
import org.wikipedia.feed.view.DefaultFeedCardView | ||
import org.wikipedia.feed.view.FeedAdapter | ||
import org.wikipedia.history.HistoryEntry | ||
import org.wikipedia.page.PageTitle | ||
import org.wikipedia.places.PlacesActivity | ||
import org.wikipedia.readinglist.LongPressMenu | ||
import org.wikipedia.readinglist.database.ReadingListPage | ||
import org.wikipedia.settings.Prefs | ||
import org.wikipedia.util.GeoUtil | ||
import org.wikipedia.util.StringUtil | ||
import org.wikipedia.views.ViewUtil | ||
import java.util.Locale | ||
|
||
class PlacesCardView(context: Context) : DefaultFeedCardView<PlacesCard>(context) { | ||
|
||
private val binding = ViewPlacesCardBinding.inflate(LayoutInflater.from(context), this, true) | ||
|
||
override var card: PlacesCard? = null | ||
set(value) { | ||
field = value | ||
value?.let { | ||
header(it) | ||
footer(it) | ||
updateContents(it) | ||
} | ||
} | ||
|
||
override var callback: FeedAdapter.Callback? = null | ||
set(value) { | ||
field = value | ||
binding.cardHeader.setCallback(value) | ||
} | ||
|
||
private fun updateContents(card: PlacesCard) { | ||
card.nearbyPage?.let { | ||
binding.placesEnableLocationContainer.isVisible = false | ||
binding.placesArticleContainer.isVisible = true | ||
binding.placesCardTitle.text = StringUtil.fromHtml(it.pageTitle.displayText) | ||
binding.placesCardDescription.text = StringUtil.fromHtml(it.pageTitle.description) | ||
binding.placesCardDescription.isVisible = !it.pageTitle.description.isNullOrEmpty() | ||
it.pageTitle.thumbUrl?.let { url -> | ||
ViewUtil.loadImage(binding.placesCardThumbnail, url, circleShape = true) | ||
} | ||
Prefs.placesLastLocationAndZoomLevel?.first?.let { location -> | ||
if (GeoUtil.isSamePlace(location.latitude, it.location.latitude, location.longitude, it.location.longitude)) { | ||
binding.placesCardDistance.text = context.getString(R.string.places_card_distance_unknown) | ||
} else { | ||
val distanceText = GeoUtil.getDistanceWithUnit(location, it.location, Locale.getDefault()) | ||
binding.placesCardDistance.text = context.getString(R.string.places_card_distance_suffix, distanceText) | ||
} | ||
} | ||
binding.placesCardContainer.setOnClickListener { _ -> | ||
goToPlaces(it.pageTitle, it.location) | ||
} | ||
binding.placesCardContainer.setOnLongClickListener { view -> | ||
LongPressMenu(view, openPageInPlaces = true, location = it.location, callback = object : LongPressMenu.Callback { | ||
override fun onOpenInPlaces(entry: HistoryEntry, location: Location) { | ||
goToPlaces(entry.title, location) | ||
} | ||
|
||
override fun onOpenInNewTab(entry: HistoryEntry) { | ||
callback?.onSelectPage(card, entry, true) | ||
} | ||
|
||
override fun onAddRequest(entry: HistoryEntry, addToDefault: Boolean) { | ||
callback?.onAddPageToList(entry, addToDefault) | ||
} | ||
|
||
override fun onMoveRequest(page: ReadingListPage?, entry: HistoryEntry) { | ||
callback?.onMovePageToList(page!!.listId, entry) | ||
} | ||
}).show(HistoryEntry(it.pageTitle, HistoryEntry.SOURCE_FEED_PLACES)) | ||
|
||
false | ||
} | ||
} ?: run { | ||
binding.placesEnableLocationContainer.isVisible = true | ||
binding.placesArticleContainer.isVisible = false | ||
binding.placesCardContainer.setOnClickListener { | ||
goToPlaces() | ||
} | ||
binding.placesEnableLocationButton.setOnClickListener { | ||
goToPlaces() | ||
} | ||
} | ||
} | ||
|
||
private fun header(card: PlacesCard) { | ||
binding.cardHeader.setTitle(card.title()) | ||
.setCard(card) | ||
.setLangCode(null) | ||
.setCallback(callback) | ||
} | ||
|
||
private fun footer(card: PlacesCard) { | ||
binding.cardFooter.callback = CardFooterView.Callback { | ||
goToPlaces() | ||
} | ||
binding.cardFooter.setFooterActionText(card.footerActionText(), card.wikiSite().languageCode) | ||
} | ||
|
||
private fun goToPlaces(pageTitle: PageTitle? = null, location: Location? = null) { | ||
context.startActivity(PlacesActivity.newIntent(context, pageTitle, location)) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/org/wikipedia/feed/places/PlacesFeedClient.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,53 @@ | ||
package org.wikipedia.feed.places | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import org.wikipedia.dataclient.ServiceFactory | ||
import org.wikipedia.dataclient.WikiSite | ||
import org.wikipedia.dataclient.page.NearbyPage | ||
import org.wikipedia.feed.dataclient.FeedClient | ||
import org.wikipedia.page.PageTitle | ||
import org.wikipedia.places.PlacesFragment | ||
import org.wikipedia.settings.Prefs | ||
import org.wikipedia.util.GeoUtil | ||
import org.wikipedia.util.ImageUrlUtil | ||
|
||
class PlacesFeedClient( | ||
private val coroutineScope: CoroutineScope | ||
) : FeedClient { | ||
|
||
private lateinit var cb: FeedClient.Callback | ||
private var age: Int = 0 | ||
private var clientJob: Job? = null | ||
|
||
override fun request(context: Context, wiki: WikiSite, age: Int, cb: FeedClient.Callback) { | ||
this.age = age | ||
this.cb = cb | ||
|
||
Prefs.placesLastLocationAndZoomLevel?.let { | ||
coroutineScope.launch(CoroutineExceptionHandler { _, throwable -> | ||
cb.error(throwable) | ||
}) { | ||
val location = it.first | ||
val response = ServiceFactory.get(wiki).getGeoSearch("${location.latitude}|${location.longitude}", 10000, 10, 10) | ||
val lastPage = response.query?.pages.orEmpty() | ||
.filter { it.coordinates != null && !GeoUtil.isSamePlace(location.latitude, it.coordinates[0].lat, location.longitude, it.coordinates[0].lon) } | ||
.map { | ||
NearbyPage(it.pageId, PageTitle(it.title, wiki, | ||
if (it.thumbUrl().isNullOrEmpty()) null else ImageUrlUtil.getUrlForPreferredSize(it.thumbUrl()!!, PlacesFragment.THUMB_SIZE), | ||
it.description, it.displayTitle(wiki.languageCode)), it.coordinates!![0].lat, it.coordinates[0].lon) | ||
}[age % 10] | ||
cb.success(listOf(PlacesCard(wiki, age, lastPage))) | ||
} | ||
} ?: run { | ||
cb.success(listOf(PlacesCard(wiki, age))) | ||
} | ||
} | ||
|
||
override fun cancel() { | ||
clientJob?.cancel() | ||
} | ||
} |
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
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.