Skip to content

Commit

Permalink
Merge pull request #706 from LoxiaLiSA/master
Browse files Browse the repository at this point in the history
aa
  • Loading branch information
CeuiLiSA authored Feb 11, 2025
2 parents 7f9f213 + c0af745 commit 6c5400c
Show file tree
Hide file tree
Showing 28 changed files with 414 additions and 78 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/ceui/lisa/database/AppDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL(
"CREATE TABLE IF NOT EXISTS general_table (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + // ✅ 兼容 Long
"id INTEGER NOT NULL, " + // id 字段,非空
"recordType INTEGER NOT NULL, " + // recordType 字段,非空
"json TEXT NOT NULL, " +
"entityType INTEGER NOT NULL, " +
"recordType INTEGER NOT NULL, " +
"updatedTime INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000))"
"updatedTime INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000), " +
"PRIMARY KEY(id, recordType)" + // 复合主键
")"
);
}
};
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/ceui/lisa/utils/ShareIllust.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public abstract class ShareIllust implements IExecutor {

public static final String URL_Head = "https://www.pixiv.net/artworks/";
public static final String USER_URL_Head = "https://www.pixiv.net/users/";
private final IllustsBean mIllustsBean;
private final Context mContext;

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/ceui/loxia/PixivHttpError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ceui.loxia

data class PixivHttpError(
val user_message: String? = null,
val message: String? = null,
val reason: String? = null,
)

data class ErrorResp(
val error: PixivHttpError? = null
)

16 changes: 15 additions & 1 deletion app/src/main/java/ceui/loxia/RefreshState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import ceui.lisa.R
import ceui.lisa.activities.Shaft
import ceui.lisa.databinding.ItemLoadingBinding
import ceui.pixiv.utils.setOnClick
import retrofit2.HttpException
import timber.log.Timber
import java.io.Serializable
import java.lang.Exception
import java.net.SocketTimeoutException
Expand Down Expand Up @@ -72,7 +75,18 @@ fun Throwable.getHumanReadableMessage(context: Context): String {
val title = titleAfter.substringBefore("</title>")
title
} else {
"${lc}: ${this.javaClass.simpleName}"
if (this is HttpException) {
val errorBody = this.response()?.errorBody()?.string()
try {
val obj = Shaft.sGson.fromJson(errorBody, ErrorResp::class.java)
obj.error?.user_message ?: errorBody ?: ""
} catch (ex: kotlin.Exception) {
Timber.e(ex)
errorBody ?: ""
}
} else {
"${lc}: ${this.javaClass.simpleName}"
}
}
}
}
103 changes: 76 additions & 27 deletions app/src/main/java/ceui/pixiv/db/EntityWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,93 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import timber.log.Timber


object EntityWrapper {

fun visitIllust(context: Context, illust: Illust) {
// 通用插入方法
private suspend fun insertEntity(context: Context, entity: GeneralEntity) {
try {
AppDatabase.getAppDatabase(context).generalDao().insert(entity)
Timber.d("EntityWrapper insertEntity done ${entity.id}")
} catch (ex: Exception) {
Timber.e(ex, "Error inserting entity: ${entity.id}")
}
}

// 通用删除方法
private suspend fun deleteEntity(context: Context, recordType: Int, id: Long) {
try {
AppDatabase.getAppDatabase(context).generalDao().deleteByRecordTypeAndId(recordType, id)
Timber.d("EntityWrapper deleteEntity done $id")
} catch (ex: Exception) {
Timber.e(ex, "Error deleting entity: $id")
}
}

// 插入访问记录
private fun visit(context: Context, id: Long, entityJson: String, entityType: Int, recordType: Int) {
MainScope().launch(Dispatchers.IO) {
try {
val json = Shaft.sGson.toJson(illust)
val entity = GeneralEntity(illust.id, json, EntityType.ILLUST, RecordType.VIEW_ILLUST_HISTORY)
AppDatabase.getAppDatabase(context).generalDao().insert(entity)
Timber.d("EntityWrapper visitIllust done ${illust.title}")
} catch (ex: Exception) {
Timber.e(ex)
}
val entity = GeneralEntity(id, entityJson, entityType, recordType)
insertEntity(context, entity)
}
}

fun visitNovel(context: Context, novel: Novel) {
// 插入或删除块操作
private fun block(context: Context, id: Long, entityJson: String, entityType: Int, recordType: Int) {
MainScope().launch(Dispatchers.IO) {
try {
val json = Shaft.sGson.toJson(novel)
val entity = GeneralEntity(novel.id, json, EntityType.NOVEL, RecordType.VIEW_NOVEL_HISTORY)
AppDatabase.getAppDatabase(context).generalDao().insert(entity)
Timber.d("EntityWrapper visitNovel done ${novel.title}")
} catch (ex: Exception) {
Timber.e(ex)
}
val entity = GeneralEntity(id, entityJson, entityType, recordType)
insertEntity(context, entity)
}
}

// 调用 `visit` 方法
fun visitIllust(context: Context, illust: Illust) {
val json = Shaft.sGson.toJson(illust)
visit(context, illust.id, json, EntityType.ILLUST, RecordType.VIEW_ILLUST_HISTORY)
}

fun visitNovel(context: Context, novel: Novel) {
val json = Shaft.sGson.toJson(novel)
visit(context, novel.id, json, EntityType.NOVEL, RecordType.VIEW_NOVEL_HISTORY)
}

fun visitUser(context: Context, user: User) {
val json = Shaft.sGson.toJson(user)
visit(context, user.id, json, EntityType.USER, RecordType.VIEW_USER_HISTORY)
}

// 调用 `block` 方法
fun blockIllust(context: Context, illust: Illust) {
val json = Shaft.sGson.toJson(illust)
block(context, illust.id, json, EntityType.ILLUST, RecordType.BLOCK_ILLUST)
}

fun blockNovel(context: Context, novel: Novel) {
val json = Shaft.sGson.toJson(novel)
block(context, novel.id, json, EntityType.NOVEL, RecordType.BLOCK_NOVEL)
}

fun blockUser(context: Context, user: User) {
val json = Shaft.sGson.toJson(user)
block(context, user.id, json, EntityType.USER, RecordType.BLOCK_USER)
}

// 调用删除方法
fun unblockIllust(context: Context, illust: Illust) {
MainScope().launch(Dispatchers.IO) {
deleteEntity(context, RecordType.BLOCK_ILLUST, illust.id)
}
}

fun unblockNovel(context: Context, novel: Novel) {
MainScope().launch(Dispatchers.IO) {
deleteEntity(context, RecordType.BLOCK_NOVEL, novel.id)
}
}

fun unblockUser(context: Context, user: User) {
MainScope().launch(Dispatchers.IO) {
try {
val json = Shaft.sGson.toJson(user)
val entity = GeneralEntity(user.id, json, EntityType.USER, RecordType.VIEW_USER_HISTORY)
AppDatabase.getAppDatabase(context).generalDao().insert(entity)
Timber.d("EntityWrapper visitUser done ${user.name}")
} catch (ex: Exception) {
Timber.e(ex)
}
deleteEntity(context, RecordType.BLOCK_USER, user.id)
}
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/ceui/pixiv/db/GeneralDao.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ceui.pixiv.db

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
Expand All @@ -20,4 +21,12 @@ interface GeneralDao {
// ✅ 根据 entityType 查询数据,并按 updatedTime 排序,支持分页
@Query("SELECT * FROM general_table WHERE recordType = :recordType ORDER BY updatedTime DESC LIMIT :limit OFFSET :offset")
fun getByRecordType(recordType: Int, offset: Int, limit: Int = 30): List<GeneralEntity> // 根据 entityType 返回数据,按 updatedTime 降序排列,支持分页

// ✅ 根据 recordType 和 id 删除记录
@Query("DELETE FROM general_table WHERE recordType = :recordType AND id = :id")
fun deleteByRecordTypeAndId(recordType: Int, id: Long)

// ✅ 根据 entityType 和 id 查询对象是否被屏蔽,返回 LiveData<Boolean>
@Query("SELECT COUNT(*) > 0 FROM general_table WHERE recordType = :recordType AND id = :id")
fun isObjectBlocked(recordType: Int, id: Long): LiveData<Boolean>
}
7 changes: 4 additions & 3 deletions app/src/main/java/ceui/pixiv/db/GeneralEntity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package ceui.pixiv.db

import androidx.room.Entity
import androidx.room.PrimaryKey
import ceui.lisa.activities.Shaft
import ceui.lisa.models.ModelObject
import ceui.loxia.ObjectPool

@Entity(tableName = "general_table")
@Entity(
tableName = "general_table",
primaryKeys = ["id", "recordType"] // 指定复合主键
)
data class GeneralEntity(
@PrimaryKey(autoGenerate = true)
val id: Long,
val json: String,
val entityType: Int,
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/ceui/pixiv/db/RecordType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ object RecordType {
const val VIEW_ILLUST_HISTORY = 1
const val VIEW_NOVEL_HISTORY = 2
const val VIEW_USER_HISTORY = 3

const val BLOCK_ILLUST = 4
const val BLOCK_NOVEL = 5
const val BLOCK_USER = 6
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@ import android.view.View
import ceui.pixiv.ui.common.PixivFragment
import ceui.lisa.databinding.FragmentPixivListBinding
import ceui.lisa.R
import ceui.lisa.database.AppDatabase
import ceui.loxia.launchSuspend
import ceui.loxia.threadSafeArgs
import ceui.pixiv.db.RecordType
import ceui.pixiv.ui.common.ListMode
import ceui.pixiv.ui.common.constructVM
import ceui.pixiv.ui.common.setUpCustomAdapter
import ceui.pixiv.ui.common.setUpRefreshState
import ceui.pixiv.ui.common.viewBinding
import ceui.pixiv.ui.history.HistoryViewModel
import ceui.pixiv.ui.history.ViewHistoryFragmentArgs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.getValue

class BlockedItemListFragment : PixivFragment(R.layout.fragment_pixiv_list) {

private val binding by viewBinding(FragmentPixivListBinding::bind)
private val safeArgs by threadSafeArgs<ViewHistoryFragmentArgs>()
private val viewModel by constructVM({
AppDatabase.getAppDatabase(requireContext()) to safeArgs.recordType
}) { (database, recordType) ->
HistoryViewModel(database, recordType)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = setUpCustomAdapter(binding, ListMode.VERTICAL)
BlockingManager.blockedWorks.observe(viewLifecycleOwner) { list ->
adapter.submitList(list?.map { BlockedItemHolder(it) })
}
setUpRefreshState(
binding, viewModel,
if (safeArgs.recordType == RecordType.BLOCK_ILLUST) {
ListMode.STAGGERED_GRID
} else {
ListMode.VERTICAL
}
)
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/ceui/pixiv/ui/chats/SquareFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ class RedSectionHeaderViewHolder(aa: ItemRedSectionHeaderBinding) :
override fun onBindViewHolder(holder: RedSectionHeaderHolder, position: Int) {
super.onBindViewHolder(holder, position)
binding.title.text = holder.title
binding.seeMore.isVisible = holder.type != 0
binding.seeMore.setOnClick {
binding.seeMoreLayout.isVisible = holder.type != 0
binding.seeMoreLayout.setOnClick {
it.findActionReceiverOrNull<SeeMoreAction>()?.seeMore(holder.type)
}
val liveEndText = holder.liveEndText
Expand All @@ -195,4 +195,5 @@ object SeeMoreType {
const val USER_CREATED_MANGA = 200
const val USER_BOOKMARKED_ILLUST = 201
const val USER_CREATED_NOVEL = 202
const val RELATED_ILLUST = 203
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import ceui.lisa.databinding.FragmentCommonViewpagerBinding
import ceui.lisa.utils.Params
import ceui.pixiv.db.RecordType
import ceui.pixiv.session.SessionManager
import ceui.pixiv.ui.blocking.BlockedItemListFragment
import ceui.pixiv.ui.blocking.BlockedItemListFragmentArgs
import ceui.pixiv.ui.circles.PagedFragmentItem
import ceui.pixiv.ui.circles.SmartFragmentPagerAdapter
import ceui.pixiv.ui.common.ViewPagerContentType.MyBlockingHistory
import ceui.pixiv.ui.common.ViewPagerContentType.MyViewHistory
import ceui.pixiv.ui.history.ViewHistoryFragment
import ceui.pixiv.ui.history.ViewHistoryFragmentArgs
import ceui.pixiv.ui.user.UserBookmarkedIllustsFragment
Expand All @@ -30,6 +34,7 @@ object ViewPagerContentType {
const val MyBookmarkNovel = 2
const val MyFollowingUsers = 3
const val MyViewHistory = 4
const val MyBlockingHistory = 5
}

class CommonViewPagerViewModel : ViewModel() {
Expand Down Expand Up @@ -136,7 +141,7 @@ class CommonViewPagerFragment : TitledViewPagerFragment(R.layout.fragment_common
initialTitle = getString(R.string.string_392)
)
)
} else if (args.contentType == ViewPagerContentType.MyViewHistory) {
} else if (args.contentType == MyViewHistory) {
pagedItems.add(
PagedFragmentItem(
builder = {
Expand Down Expand Up @@ -173,6 +178,43 @@ class CommonViewPagerFragment : TitledViewPagerFragment(R.layout.fragment_common
initialTitle = getString(R.string.type_user)
)
)
} else if (args.contentType == MyBlockingHistory) {
pagedItems.add(
PagedFragmentItem(
builder = {
BlockedItemListFragment().apply {
arguments = BlockedItemListFragmentArgs(
RecordType.BLOCK_ILLUST
).toBundle()
}
},
initialTitle = getString(R.string.string_136)
)
)
pagedItems.add(
PagedFragmentItem(
builder = {
BlockedItemListFragment().apply {
arguments = BlockedItemListFragmentArgs(
RecordType.BLOCK_NOVEL
).toBundle()
}
},
initialTitle = getString(R.string.type_novel)
)
)
pagedItems.add(
PagedFragmentItem(
builder = {
BlockedItemListFragment().apply {
arguments = BlockedItemListFragmentArgs(
RecordType.BLOCK_USER
).toBundle()
}
},
initialTitle = getString(R.string.type_user)
)
)
}
val adapter = SmartFragmentPagerAdapter(pagedItems, this)
binding.commonViewpager.adapter = adapter
Expand Down
Loading

0 comments on commit 6c5400c

Please sign in to comment.