回答/文章评论增加评论草稿功能 https://gitlab.ghzs.com/pm/halo-app-issues/-/issues/872 (7)

This commit is contained in:
kehaoyuan
2020-05-25 10:28:30 +08:00
parent 3bc65f42c7
commit d7d55b7341
5 changed files with 143 additions and 14 deletions

View File

@ -0,0 +1,10 @@
package com.gh.gamecenter.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
class CommentDraft(
@PrimaryKey
var id: String,
var draft: String)

View File

@ -30,9 +30,12 @@ import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.OnCommentCallBackListener
import com.gh.gamecenter.baselist.ListAdapter
import com.gh.gamecenter.baselist.ListFragment
import com.gh.gamecenter.entity.CommentDraft
import com.gh.gamecenter.entity.CommentEntity
import com.gh.gamecenter.eventbus.EBReuse
import com.gh.gamecenter.qa.answer.detail.AnswerDetailFragment
import com.gh.gamecenter.room.AppDatabase
import com.gh.gamecenter.room.dao.CommentDraftDao
import com.halo.assistant.HaloApp
import com.lightgame.utils.Util_System_Keyboard
import org.greenrobot.eventbus.EventBus
@ -155,7 +158,14 @@ open class NewCommentFragment : ListFragment<CommentEntity, NewCommentViewModel>
}
}
commentEt.filters = arrayOf(TextHelper.getFilter(140, "评论不能多于140字"))
commentEt.setText(mCommentDraft)
if (!mCommentDraft.isNullOrEmpty()) {
commentEt.setText(mCommentDraft)
} else {
mViewModel.getCommentDraft(null)?.let {
commentEt.setText(it.draft)
}
}
commentEt.setSelection(commentEt.text.length)
if (mShowSoftKeyboardOnStartUp) {
commentEt.postDelayed({
@ -210,6 +220,8 @@ open class NewCommentFragment : ListFragment<CommentEntity, NewCommentViewModel>
override fun onDestroy() {
super.onDestroy()
mKeyboardHeightProvider?.close()
mViewModel.saveCommentDraft(mCommentEntity, commentEt.text.toString())
}
private fun updateCommentCount() {
@ -259,6 +271,9 @@ open class NewCommentFragment : ListFragment<CommentEntity, NewCommentViewModel>
override fun onCommentCallback(entity: CommentEntity) {
mCommentEntity = entity
setSoftInput(true)
mViewModel.getCommentDraft(entity)?.let {
commentEt.setText(it.draft)
}
}
@Optional

View File

@ -7,9 +7,12 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.createRequestBody
import com.gh.gamecenter.baselist.ListViewModel
import com.gh.gamecenter.entity.CommentDraft
import com.gh.gamecenter.entity.CommentEntity
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.room.AppDatabase
import com.gh.gamecenter.room.dao.CommentDraftDao
import com.gh.gamecenter.user.ApiResponse
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
@ -29,9 +32,21 @@ class NewCommentViewModel(application: Application,
private val mPostCommentLiveData = MutableLiveData<ApiResponse<JSONObject>>()
var commentDraftDao: CommentDraftDao? = null
val postCommentLiveData: LiveData<ApiResponse<JSONObject>>
get() = mPostCommentLiveData
init {
// 暂时只需要保存回答/文章的评论草稿
if (commentType == CommentType.ANSWER
|| commentType == CommentType.ANSWER_CONVERSATION
|| commentType == CommentType.COMMUNITY_ARTICLE
|| commentType == CommentType.COMMUNITY_ARTICLE_CONVERSATION) {
commentDraftDao = AppDatabase.getInstance(getApplication()).commentDraftDao()
}
}
override fun provideDataObservable(page: Int): Observable<List<CommentEntity>> {
val api = RetrofitManager.getInstance(getApplication()).api
@ -89,6 +104,8 @@ class NewCommentViewModel(application: Application,
val apiResponse = ApiResponse<JSONObject>()
apiResponse.data = JSONObject()
mPostCommentLiveData.postValue(apiResponse)
deleteCommentDraft(commentEntity)
}
override fun onFailure(e: HttpException?) {
@ -107,6 +124,60 @@ class NewCommentViewModel(application: Application,
mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
}
fun getCommentDraft(commentEntity: CommentEntity?): CommentDraft? {
return when {
commentEntity != null -> {
commentDraftDao?.getDraftById(commentEntity.id ?: "")
}
commentType == CommentType.ANSWER || commentType == CommentType.ANSWER_CONVERSATION -> {
commentDraftDao?.getDraftById(answerId)
}
commentType == CommentType.COMMUNITY_ARTICLE || commentType == CommentType.COMMUNITY_ARTICLE_CONVERSATION -> {
commentDraftDao?.getDraftById(articleId)
}
else -> return null
}
}
fun saveCommentDraft(commentEntity: CommentEntity?, draftContent: String) {
// 保存/删除评论草稿
commentDraftDao?.let {
var id = commentEntity?.id ?: ""
if (id.isEmpty()) {
if (commentType == CommentType.COMMUNITY_ARTICLE || commentType == CommentType.COMMUNITY_ARTICLE_CONVERSATION) {
id = articleId
}
if (commentType == CommentType.ANSWER || commentType == CommentType.ANSWER_CONVERSATION) {
id = answerId
}
}
if (draftContent.isNotEmpty()) {
val draft = CommentDraft(id = id, draft = draftContent)
if (it.updateDraft(draft) <= 0) {
it.addDraft(draft)
}
} else {
it.deleteDraftById(id)
}
}
}
fun deleteCommentDraft(commentEntity: CommentEntity?) {
var id = commentEntity?.id ?: ""
if (id.isEmpty()) {
if (commentType == CommentType.COMMUNITY_ARTICLE || commentType == CommentType.COMMUNITY_ARTICLE_CONVERSATION) {
id = articleId
}
if (commentType == CommentType.ANSWER || commentType == CommentType.ANSWER_CONVERSATION) {
id = answerId
}
}
commentDraftDao?.deleteDraftById(id)
}
class Factory(private val application: Application,
private val answerId: String,
private val commentId: String,

View File

@ -2,17 +2,6 @@ package com.gh.gamecenter.room;
import android.content.Context;
import com.gh.gamecenter.entity.LoginTokenEntity;
import com.gh.gamecenter.entity.SignEntity;
import com.gh.gamecenter.entity.UserInfoEntity;
import com.gh.gamecenter.qa.entity.AnswerEntity;
import com.gh.gamecenter.room.dao.AnswerDao;
import com.gh.gamecenter.room.dao.SignDao;
import com.gh.gamecenter.room.dao.UploadDao;
import com.gh.gamecenter.user.LoginTokenDao;
import com.gh.gamecenter.user.UserInfoDao;
import com.gh.gamecenter.video.upload.UploadEntity;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
@ -20,6 +9,19 @@ import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
import com.gh.gamecenter.entity.CommentDraft;
import com.gh.gamecenter.entity.LoginTokenEntity;
import com.gh.gamecenter.entity.SignEntity;
import com.gh.gamecenter.entity.UserInfoEntity;
import com.gh.gamecenter.qa.entity.AnswerEntity;
import com.gh.gamecenter.room.dao.AnswerDao;
import com.gh.gamecenter.room.dao.CommentDraftDao;
import com.gh.gamecenter.room.dao.SignDao;
import com.gh.gamecenter.room.dao.UploadDao;
import com.gh.gamecenter.user.LoginTokenDao;
import com.gh.gamecenter.user.UserInfoDao;
import com.gh.gamecenter.video.upload.UploadEntity;
/**
* Created by khy on 28/11/17.
*/
@ -27,7 +29,8 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
UserInfoEntity.class,
SignEntity.class,
AnswerEntity.class,
UploadEntity.class}, version = 12, exportSchema = false)
UploadEntity.class,
CommentDraft.class}, version = 13, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
public abstract AnswerDao answerDao();
@ -40,6 +43,8 @@ public abstract class AppDatabase extends RoomDatabase {
public abstract UploadDao uploadDao();
public abstract CommentDraftDao commentDraftDao();
private static AppDatabase sInstance;
private static final String DATABASE_NAME = "gh-db";
@ -126,6 +131,13 @@ public abstract class AppDatabase extends RoomDatabase {
}
};
static final Migration MIGRATION_12_13 = new Migration(12, 13) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE CommentDraft(id TEXT NOT NULL PRIMARY KEY, draft TEXT NOT NULL)");
}
};
private static AppDatabase buildDatabase(Context context) {
return Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
.addMigrations(
@ -137,7 +149,8 @@ public abstract class AppDatabase extends RoomDatabase {
MIGRATION_8_9,
MIGRATION_9_10,
MIGRATION_10_11,
MIGRATION_11_12)
MIGRATION_11_12,
MIGRATION_12_13)
// 不允许主线程查询
.allowMainThreadQueries()
// // 提供db升级的策略而不是强行销毁

View File

@ -0,0 +1,20 @@
package com.gh.gamecenter.room.dao
import androidx.room.*
import com.gh.gamecenter.entity.CommentDraft
@Dao
interface CommentDraftDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addDraft(draft: CommentDraft)
@Update(onConflict = OnConflictStrategy.REPLACE)
fun updateDraft(draft: CommentDraft): Int
@Query("select * from CommentDraft where id = :id")
fun getDraftById(id: String): CommentDraft
@Query("delete from CommentDraft WHERE id = :id")
fun deleteDraftById(id: String)
}