-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Support search feeds or articles
- Loading branch information
Showing
41 changed files
with
981 additions
and
25 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.../anivu/model/preference/BasePreference.kt → ...ava/com/skyd/anivu/base/BasePreference.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
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,23 @@ | ||
package com.skyd.anivu.config | ||
|
||
import com.skyd.anivu.model.bean.ARTICLE_TABLE_NAME | ||
import com.skyd.anivu.model.bean.ArticleBean | ||
import com.skyd.anivu.model.bean.FEED_TABLE_NAME | ||
import com.skyd.anivu.model.bean.FeedBean | ||
|
||
val allSearchDomain: HashMap<String, List<String>> = hashMapOf( | ||
FEED_TABLE_NAME to listOf( | ||
FeedBean.URL_COLUMN, | ||
FeedBean.TITLE_COLUMN, | ||
FeedBean.DESCRIPTION_COLUMN, | ||
FeedBean.LINK_COLUMN, | ||
FeedBean.ICON_COLUMN, | ||
), | ||
ARTICLE_TABLE_NAME to listOf( | ||
ArticleBean.TITLE_COLUMN, | ||
ArticleBean.AUTHOR_COLUMN, | ||
ArticleBean.DESCRIPTION_COLUMN, | ||
ArticleBean.CONTENT_COLUMN, | ||
ArticleBean.LINK_COLUMN, | ||
), | ||
) |
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,9 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import android.os.Bundle | ||
|
||
inline fun <reified T : Enum<T>> Bundle.getEnum(key: String, default: T) = | ||
getInt(key, -1).let { if (it >= 0) enumValues<T>()[it] else default } | ||
|
||
fun <T : Enum<T>> Bundle.putEnum(key: String, value: T?) = | ||
putInt(key, value?.ordinal ?: -1) |
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,57 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.skyd.anivu.base.BasePreference | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
import java.io.IOException | ||
|
||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "App") | ||
|
||
suspend fun <T> DataStore<Preferences>.put(key: Preferences.Key<T>, value: T) { | ||
this.edit { | ||
withContext(Dispatchers.IO) { | ||
it[key] = value | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> DataStore<Preferences>.getOrNull(key: Preferences.Key<T>): T? { | ||
return runBlocking { | ||
this@getOrNull.data.catch { exception -> | ||
if (exception is IOException) { | ||
exception.printStackTrace() | ||
emit(emptyPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
}.map { | ||
it[key] | ||
}.first() as T | ||
} | ||
} | ||
|
||
fun <T> DataStore<Preferences>.getOrDefault(pref: BasePreference<T>): T { | ||
return runBlocking { | ||
this@getOrDefault.data.catch { exception -> | ||
if (exception is IOException) { | ||
exception.printStackTrace() | ||
emit(emptyPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
}.map { | ||
pref.fromPreferences(it) | ||
}.first() | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/skyd/anivu/model/bean/SearchDomainBean.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,29 @@ | ||
package com.skyd.anivu.model.bean | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import com.skyd.anivu.base.BaseBean | ||
import kotlinx.serialization.Serializable | ||
|
||
const val SEARCH_DOMAIN_TABLE_NAME = "SearchDomain" | ||
|
||
@Serializable | ||
@Entity( | ||
tableName = SEARCH_DOMAIN_TABLE_NAME, | ||
primaryKeys = [SearchDomainBean.TABLE_NAME_COLUMN, SearchDomainBean.COLUMN_NAME_COLUMN] | ||
) | ||
data class SearchDomainBean( | ||
@ColumnInfo(name = TABLE_NAME_COLUMN) | ||
var tableName: String, | ||
@ColumnInfo(name = COLUMN_NAME_COLUMN) | ||
var columnName: String, | ||
@ColumnInfo(name = SEARCH_COLUMN) | ||
var search: Boolean, | ||
) : BaseBean { | ||
companion object { | ||
const val TABLE_NAME_COLUMN = "tableName" | ||
const val COLUMN_NAME_COLUMN = "columnName" | ||
const val SEARCH_COLUMN = "search" | ||
} | ||
} | ||
|
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
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/skyd/anivu/model/db/SearchDomainDatabase.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 com.skyd.anivu.model.db | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import androidx.room.migration.Migration | ||
import com.skyd.anivu.model.bean.SearchDomainBean | ||
import com.skyd.anivu.model.db.dao.SearchDomainDao | ||
|
||
const val SEARCH_DOMAIN_DATA_BASE_FILE_NAME = "searchDomain.db" | ||
|
||
@Database( | ||
entities = [ | ||
SearchDomainBean::class, | ||
], | ||
version = 1 | ||
) | ||
@TypeConverters( | ||
value = [] | ||
) | ||
abstract class SearchDomainDatabase : RoomDatabase() { | ||
|
||
abstract fun searchDomainDao(): SearchDomainDao | ||
|
||
companion object { | ||
@Volatile | ||
private var instance: SearchDomainDatabase? = null | ||
|
||
private val migrations = arrayOf<Migration>() | ||
|
||
fun getInstance(context: Context): SearchDomainDatabase { | ||
return if (instance == null) { | ||
synchronized(this) { | ||
if (instance == null) { | ||
Room.databaseBuilder( | ||
context.applicationContext, | ||
SearchDomainDatabase::class.java, | ||
SEARCH_DOMAIN_DATA_BASE_FILE_NAME | ||
) | ||
.addMigrations(*migrations) | ||
.build() | ||
} else { | ||
instance as SearchDomainDatabase | ||
} | ||
} | ||
} else { | ||
instance as SearchDomainDatabase | ||
} | ||
} | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/skyd/anivu/model/db/dao/SearchDomainDao.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,41 @@ | ||
package com.skyd.anivu.model.db.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import com.skyd.anivu.model.bean.SEARCH_DOMAIN_TABLE_NAME | ||
import com.skyd.anivu.model.bean.SearchDomainBean | ||
import com.skyd.anivu.model.bean.SearchDomainBean.Companion.COLUMN_NAME_COLUMN | ||
import com.skyd.anivu.model.bean.SearchDomainBean.Companion.SEARCH_COLUMN | ||
import com.skyd.anivu.model.bean.SearchDomainBean.Companion.TABLE_NAME_COLUMN | ||
|
||
@Dao | ||
interface SearchDomainDao { | ||
@Transaction | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun setSearchDomain(searchDomainBean: SearchDomainBean) | ||
|
||
@Transaction | ||
@Query( | ||
"""SELECT $SEARCH_COLUMN FROM $SEARCH_DOMAIN_TABLE_NAME | ||
WHERE $TABLE_NAME_COLUMN LIKE :tableName AND $COLUMN_NAME_COLUMN LIKE :columnName""" | ||
) | ||
fun getSearchDomainOrNull(tableName: String, columnName: String): Boolean? | ||
|
||
// 被选择的搜索域的个数 | ||
@Transaction | ||
@Query("SELECT COUNT(*) FROM $SEARCH_DOMAIN_TABLE_NAME WHERE $SEARCH_COLUMN = 1") | ||
fun selectedSearchDomainCount(): Int | ||
|
||
@Transaction | ||
fun getSearchDomain(tableName: String, columnName: String): Boolean { | ||
val result = getSearchDomainOrNull(tableName, columnName) | ||
return result == true || selectedSearchDomainCount() == 0 | ||
} | ||
|
||
@Transaction | ||
@Query(value = "SELECT * FROM $SEARCH_DOMAIN_TABLE_NAME") | ||
fun getAllSearchDomain(): List<SearchDomainBean> | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/skyd/anivu/model/preference/search/IntersectSearchBySpacePreference.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,26 @@ | ||
package com.skyd.anivu.model.preference.search | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import com.skyd.anivu.base.BasePreference | ||
import com.skyd.anivu.ext.dataStore | ||
import com.skyd.anivu.ext.put | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
object IntersectSearchBySpacePreference : BasePreference<Boolean> { | ||
private const val INTERSECT_SEARCH_BY_SPACE = "intersectSearchBySpace" | ||
override val default = true | ||
|
||
val key = booleanPreferencesKey(INTERSECT_SEARCH_BY_SPACE) | ||
|
||
fun put(context: Context, scope: CoroutineScope, value: Boolean) { | ||
scope.launch(Dispatchers.IO) { | ||
context.dataStore.put(key, value) | ||
} | ||
} | ||
|
||
override fun fromPreferences(preferences: Preferences): Boolean = preferences[key] ?: default | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/skyd/anivu/model/preference/search/UseRegexSearchPreference.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,26 @@ | ||
package com.skyd.anivu.model.preference.search | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import com.skyd.anivu.base.BasePreference | ||
import com.skyd.anivu.ext.dataStore | ||
import com.skyd.anivu.ext.put | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
object UseRegexSearchPreference : BasePreference<Boolean> { | ||
private const val USE_REGEX_SEARCH = "useRegexSearch" | ||
override val default = false | ||
|
||
val key = booleanPreferencesKey(USE_REGEX_SEARCH) | ||
|
||
fun put(context: Context, scope: CoroutineScope, value: Boolean) { | ||
scope.launch(Dispatchers.IO) { | ||
context.dataStore.put(key, value) | ||
} | ||
} | ||
|
||
override fun fromPreferences(preferences: Preferences): Boolean = preferences[key] ?: default | ||
} |
Oops, something went wrong.