Skip to content

Commit

Permalink
Highlighted news management (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaraDimension committed Nov 26, 2024
1 parent 2958960 commit 2addb86
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ android {
buildConfigField("String", "ISSUER_URL", "\"https://auth.opendatahub.testingmachine.eu/auth/realms/noi/\"")
buildConfigField("String", "SIGNUP_URL", "\"https://auth.opendatahub.testingmachine.eu/auth/realms/noi/protocol/openid-connect/registrations?client_id=it.bz.noi.community&redirect_uri=https://noi.bz.it&response_type=code&scope=openid\"")
buildConfigField("String", "COMMUNITY_API_URL", "\"https://api.community.noi.testingmachine.eu\"")
buildConfigField("String", "OPENDATAHUB_API_BASE_URL", "\"https://api.tourism.testingmachine.eu/\"")
dimension = 'environment'
}
production {
buildConfigField("String", "ISSUER_URL", "\"https://auth.opendatahub.com/auth/realms/noi/\"")
buildConfigField("String", "SIGNUP_URL", "\"https://auth.opendatahub.com/auth/realms/noi/protocol/openid-connect/registrations?client_id=it.bz.noi.community&redirect_uri=https://noi.bz.it&response_type=code&scope=openid\"")
buildConfigField("String", "COMMUNITY_API_URL", "\"https://api.community.noi.opendatahub.com\"")
buildConfigField("String", "OPENDATAHUB_API_BASE_URL", "\"https://tourism.opendatahub.com/\"")
dimension = 'environment'
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/it/bz/noi/community/data/api/ApiHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ApiHelper(
startDate = newsParams.startDate,
pageNumber = newsParams.pageNumber,
pageSize = newsParams.pageSize,
language = newsParams.language
language = newsParams.language,
rawFilter = newsParams.getRawFilter()
)

suspend fun getNewsDetails(newsId: String, language: String?) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ interface OpendatahubApiService {
suspend fun getNews(
@Query("removenullvalues") removeNullValues: Boolean = true,
@Query("articletype") endDate: String = "newsfeednoi",
@Query("rawsort") rawFilter: String = "-ArticleDate",
@Query("fields") fields: String = "Id,ArticleDate,Detail,ContactInfos,ImageGallery,ODHTags",
@Query("rawsort") rawSort: String = "-ArticleDate",
@Query("fields") fields: String = "Id,ArticleDate,Detail,ContactInfos,ImageGallery,ODHTags,Highlight",
@Query("pagesize") pageSize: Int,
@Query("pagenumber") pageNumber: Int,
@Query("startdate") startDate: String,
@Query("language") language: String?,
@Query("publishedon") publishedOn: String? = "noi-communityapp",
@Query("rawfilter") rawFilter: String?,
): NewsResponse

@GET("v1/Article/{id}")
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/it/bz/noi/community/data/models/News.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ data class News(
@SerializedName("Detail")
val detail: Map<String, Detail>,
@SerializedName("ContactInfos")
val contactInfo: Map<String, ContactInfo>,
val contactInfo: Map<String, ContactInfo>? = null,
@SerializedName("ImageGallery")
val images: List<NewsImage>? = null,
@SerializedName("ODHTags")
val tags: List<Tag>? = null
val tags: List<Tag>? = null,
@SerializedName("Highlight")
val highlighted: Boolean = false,
) : Parcelable

@Keep
Expand Down Expand Up @@ -71,11 +73,15 @@ fun News.getDetail(): Detail? {
}

fun News.getContactInfo(): ContactInfo? {
return contactInfo[Utils.getAppLanguage()]
return contactInfo?.let {
it[Utils.getAppLanguage()]
}
}

// FIXME -> WIP
fun News.hasImportantFlag(): Boolean {
return tags != null && tags.filter { it.id == "important" }.isNotEmpty()
val isImportant = tags != null && tags.filter { it.id == "important" }.isNotEmpty()
return isImportant || highlighted
}

data class NewsResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ data class NewsParams(
var startDate: String,
var pageSize: Int = 10,
var pageNumber: Int,
var language: String?
var language: String?,
var highlight: Boolean = false
)

fun NewsParams.getRawFilter(): String {
if (highlight)
return "eq(Highlight,\"true\")"
return "or(eq(Highlight,\"false\"),isnull(Highlight))"
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ class NewsDetailsFragment: Fragment() {

binding.title.text = detail.title
binding.shortText.text = detail.abstract
binding.longText.text = Html.fromHtml(detail.text, Html.FROM_HTML_MODE_LEGACY)
binding.longText.text = detail.text?.let{ Html.fromHtml(it, Html.FROM_HTML_MODE_LEGACY) }
binding.longText.movementMethod = LinkMovementMethod.getInstance()
}

var isExternalLink = false
var isEmail = false
news.getContactInfo()?.let { contactInfo ->
val contactInfo = news.getContactInfo()
if (contactInfo == null) {
binding.publisher.text = "N/D"
} else {
binding.publisher.text = contactInfo.publisher

binding.logo.isVisible = true
Glide
.with(binding.root.context)
.load(contactInfo.logo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,22 @@ class NewsVH(private val binding: VhNewsBinding, detailListener: NewsDetailListe
this.news = news
binding.date.text = df.format(news.date)
binding.importantTag.isVisible = news.hasImportantFlag()
if (news.highlighted) // FIXME -> WIP
binding.importantTag.text = "TMP PINNATO"
news.getDetail()?.let { detail ->
binding.title.text = detail.title
binding.shortText.text = detail.abstract
}
news.getContactInfo()?.let { contactInfo ->
val contactInfo = news.getContactInfo()
if (contactInfo == null) {
binding.publisher.text = "N/D"
} else {
binding.publisher.text = contactInfo.publisher
Glide
.with(binding.root.context)
.load(contactInfo.logo)
.centerCrop()
.into(binding.logo)

}

setTransitionNames(news.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,35 @@ class NewsPagingSource(private val mainRepository: MainRepository) :
}

private val startDate = Date()
private var moreHighlight = true

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, News> {
// Start refresh at page 1 if undefined.
val nextPageNumber = params.key ?: 1

val newsParams = NewsParams(
startDate = DateUtils.parameterDateFormatter().format(startDate),
pageSize = PAGE_ITEMS,
pageNumber = nextPageNumber,
language = Utils.getAppLanguage()
)
val newsParams = getNewsParams(nextPageNumber)

return try {
val newsResponse = mainRepository.getNews(newsParams)

val news = mutableListOf<News>()
news.addAll(newsResponse.news)

var nextKey = if (newsResponse.nextPage != null) newsResponse.currentPage + 1 else null
if (moreHighlight && nextKey == null) {
moreHighlight = false

val notHighlightedNewsParams = getNewsParams(nextPageNumber)
val notHighlightedNewsResponse = mainRepository.getNews(notHighlightedNewsParams)
news.addAll(notHighlightedNewsResponse.news)

nextKey = if (notHighlightedNewsResponse.nextPage != null) notHighlightedNewsResponse.currentPage + 1 else null
}

LoadResult.Page(
data = newsResponse.news,
data = news,
prevKey = null, // Only paging forward.
nextKey = if (newsResponse.nextPage != null) newsResponse.currentPage + 1 else null
nextKey = nextKey
)
} catch (ex: Exception) {
Log.e(TAG, "Error loading news", ex)
Expand All @@ -52,6 +62,14 @@ class NewsPagingSource(private val mainRepository: MainRepository) :

}

private fun getNewsParams(nextPageNumber: Int) = NewsParams(
startDate = DateUtils.parameterDateFormatter().format(startDate),
pageSize = PAGE_ITEMS,
pageNumber = nextPageNumber,
language = Utils.getAppLanguage(),
highlight = moreHighlight
)

override fun getRefreshKey(state: PagingState<Int, News>): Int? {
// Try to find the page key of the closest page to anchorPosition, from
// either the prevKey or the nextKey, but you need to handle nullability
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/fragment_news_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ SPDX-License-Identifier: CC0-1.0
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/black_circle"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_noi"
Expand Down

0 comments on commit 2addb86

Please sign in to comment.