Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<provider
android:name=".ui.widget.ArticleWidgetContentProvider"
android:authorities="me.ash.reader.widget.articles"
android:exported="true" />

<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@ class TextToSpeechManager @Inject constructor(
.firstOrNull()?.locale
}

val textSegments = text.split("\n").filterNot { it.isBlank() }
val normalized = text.replace(Regex("\\s+"), " ").trim()
val maxLen = TextToSpeech.getMaxSpeechInputLength()
val textSegments = buildList {
var remaining = normalized
while (remaining.length > maxLen) {
val split = remaining.lastIndexOf(' ', maxLen).takeIf { it > 0 } ?: maxLen
add(remaining.substring(0, split))
remaining = remaining.substring(split).trimStart()
}
if (remaining.isNotBlank()) add(remaining)
}
val total = textSegments.size
state = State.Reading(0, total)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.ash.reader.ui.widget

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking

class ArticleWidgetContentProvider : ContentProvider() {

companion object {
const val AUTHORITY = "me.ash.reader.widget.articles"
val COLUMNS = arrayOf("id", "feed_name", "title", "date")

private const val MATCH_ARTICLES = 1
private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
addURI(AUTHORITY, "#", MATCH_ARTICLES)
}
}

override fun onCreate(): Boolean = true

override fun query(
uri: Uri,
projection: Array<out String>?,
selection: String?,
selectionArgs: Array<out String>?,
sortOrder: String?,
): Cursor? {
if (uriMatcher.match(uri) != MATCH_ARTICLES) return null
val widgetId = uri.lastPathSegment?.toIntOrNull() ?: return null

val repo = WidgetRepository.get(context!!)
val config = runBlocking { repo.getConfig(widgetId) }
val articles = runBlocking { repo.getData(config.dataSource).first() }.articles

val cursor = MatrixCursor(COLUMNS)
articles.take(15).forEach { article ->
cursor.addRow(arrayOf(article.id, article.feedName, article.title, article.date))
}
return cursor
}

override fun getType(uri: Uri): String? = null
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
}
2 changes: 2 additions & 0 deletions app/src/main/java/me/ash/reader/ui/widget/WidgetRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ constructor(
imgUrl = article.img,
feedName = feed.name,
id = article.id,
date = article.date.time,
)
}
}
Expand Down Expand Up @@ -179,4 +180,5 @@ data class Article(
val title: String,
val imgUrl: String? = null,
val feedName: String,
val date: Long = 0L,
)
Loading