diff --git a/app/src/main/java/com/gh/gamecenter/entity/CommentDraft.kt b/app/src/main/java/com/gh/gamecenter/entity/CommentDraft.kt new file mode 100644 index 0000000000..1c8303bbd9 --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/entity/CommentDraft.kt @@ -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) \ No newline at end of file diff --git a/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentFragment.kt b/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentFragment.kt index de98f9458c..4ebb0d90e0 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentFragment.kt +++ b/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentFragment.kt @@ -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 } } 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 override fun onDestroy() { super.onDestroy() mKeyboardHeightProvider?.close() + + mViewModel.saveCommentDraft(mCommentEntity, commentEt.text.toString()) } private fun updateCommentCount() { @@ -259,6 +271,9 @@ open class NewCommentFragment : ListFragment override fun onCommentCallback(entity: CommentEntity) { mCommentEntity = entity setSoftInput(true) + mViewModel.getCommentDraft(entity)?.let { + commentEt.setText(it.draft) + } } @Optional diff --git a/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentViewModel.kt b/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentViewModel.kt index 5f4cdf809d..96e79f536e 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentViewModel.kt +++ b/app/src/main/java/com/gh/gamecenter/qa/comment/NewCommentViewModel.kt @@ -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>() + var commentDraftDao: CommentDraftDao? = null + val postCommentLiveData: LiveData> 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> { val api = RetrofitManager.getInstance(getApplication()).api @@ -89,6 +104,8 @@ class NewCommentViewModel(application: Application, val apiResponse = ApiResponse() 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, diff --git a/app/src/main/java/com/gh/gamecenter/room/AppDatabase.java b/app/src/main/java/com/gh/gamecenter/room/AppDatabase.java index f886fb00f7..e0bd08111e 100644 --- a/app/src/main/java/com/gh/gamecenter/room/AppDatabase.java +++ b/app/src/main/java/com/gh/gamecenter/room/AppDatabase.java @@ -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升级的策略而不是强行销毁 diff --git a/app/src/main/java/com/gh/gamecenter/room/dao/CommentDraftDao.kt b/app/src/main/java/com/gh/gamecenter/room/dao/CommentDraftDao.kt new file mode 100644 index 0000000000..1a0bddc62c --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/room/dao/CommentDraftDao.kt @@ -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) +}