Skip to content

Commit

Permalink
Add feedsCount api
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Jun 9, 2024
1 parent 5cbbfb1 commit 9c7976c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import kotlinx.serialization.Serializable
import java.io.File

@Serializable
data class DownloadFileItem(val path: String, val name: String)
data class DownloadFileItem(val path: String, val name: String = "")
data class DownloadFileItemWrap(val file: File, val name: String)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class BaseContentHelper {
}?.toSet() ?: emptySet()
}

open fun deleteByIdsAsync(context: Context, ids: Set<String>) {
fun deleteByIdsAsync(context: Context, ids: Set<String>) {
ids.chunked(500).forEach { chunk ->
val selection = "${BaseColumns._ID} IN (${StringHelper.getQuestionMarks(chunk.size)})"
val selectionArgs = chunk.map { it }.toTypedArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ abstract class BaseMediaContentHelper {
context: Context,
query: String,
): Set<String> {
return getSearchCursorAsync(context, query)?.map { cursor, cache ->
return context.contentResolver.getSearchCursor(
uriExternal, arrayOf(BaseColumns._ID), buildWhere(query)
)?.map { cursor, cache ->
cursor.getStringValue(BaseColumns._ID, cache)
}?.toSet() ?: emptySet()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.content.ContentProviderOperation
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.provider.BaseColumns
import android.provider.ContactsContract
import com.ismartcoding.lib.content.ContentWhere
import com.ismartcoding.lib.data.SortBy
import com.ismartcoding.lib.data.enums.SortDirection
import com.ismartcoding.lib.extensions.count
import com.ismartcoding.lib.extensions.getIntValue
import com.ismartcoding.lib.extensions.getPagingCursor
import com.ismartcoding.lib.extensions.getSearchCursor
import com.ismartcoding.lib.extensions.getStringValue
import com.ismartcoding.lib.extensions.getTimeValue
import com.ismartcoding.lib.extensions.map
Expand All @@ -23,10 +26,10 @@ import com.ismartcoding.plain.features.contact.SourceHelper
import com.ismartcoding.plain.helpers.QueryHelper
import com.ismartcoding.plain.web.models.ContactInput

object ContactMediaStoreHelper : BaseContentHelper() {
override val uriExternal: Uri = ContactsContract.Data.CONTENT_URI
object ContactMediaStoreHelper {
private val uriExternal: Uri = ContactsContract.Data.CONTENT_URI

override fun getProjection(): Array<String> {
private fun getProjection(): Array<String> {
return arrayOf(
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.RAW_CONTACT_ID,
Expand All @@ -45,7 +48,7 @@ object ContactMediaStoreHelper : BaseContentHelper() {
)
}

override suspend fun buildWhereAsync(query: String): ContentWhere {
private suspend fun buildWhereAsync(query: String): ContentWhere {
val where = ContentWhere()
where.add("${ContactsContract.Data.MIMETYPE} = ?", ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
if (query.isNotEmpty()) {
Expand All @@ -54,9 +57,11 @@ object ContactMediaStoreHelper : BaseContentHelper() {
"text" -> {
where.add("${ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME} LIKE ?", "%${it.value}%")
}

"ids" -> {
where.addIn(ContactsContract.Data.RAW_CONTACT_ID, it.value.split(","))
}

"id" -> {
where.addEqual(ContactsContract.Data.RAW_CONTACT_ID, it.value)
}
Expand All @@ -67,7 +72,11 @@ object ContactMediaStoreHelper : BaseContentHelper() {
return where
}

override fun deleteByIdsAsync(
suspend fun countAsync(context: Context, query: String): Int {
return context.contentResolver.count(uriExternal, buildWhereAsync(query))
}

fun deleteByIdsAsync(
context: Context,
ids: Set<String>,
) {
Expand All @@ -85,6 +94,13 @@ object ContactMediaStoreHelper : BaseContentHelper() {
return searchAsync(context, "id=$id", 1, 0).firstOrNull()
}

suspend fun getIdsAsync(context: Context, query: String): Set<String> {
val where = buildWhereAsync(query)
return context.contentResolver.getSearchCursor(uriExternal, arrayOf(ContactsContract.Data.RAW_CONTACT_ID), where)?.map { cursor, cache ->
cursor.getStringValue(ContactsContract.Data.RAW_CONTACT_ID, cache)
}?.toSet() ?: emptySet()
}

fun updateAsync(
id: String,
contact: ContactInput,
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/ismartcoding/plain/web/SXGraphQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ class SXGraphQL(val schema: Schema) {
items.map { it.toModel() }
}
}
query("feedsCount") {
resolver { ->
FeedHelper.getFeedCounts().map { it.toModel() }
}
}
query("feedEntries") {
configure {
executor = Executor.DataLoaderPrepared
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ismartcoding.plain.web.models

import com.ismartcoding.plain.db.DFeedCount

data class FeedCount(val id: String, val count: Int)

fun DFeedCount.toModel(): FeedCount {
return FeedCount(id, count)
}

0 comments on commit 9c7976c

Please sign in to comment.