From 5caa0d4c5da4aea7c711567f9e15a47f978a88ee Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sun, 1 Jan 2023 21:25:30 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 +- .../post/adapters/OKSignProgressAdapter.kt | 136 ---------- .../post/components/workers/OKSignWork.kt | 253 ------------------ .../post/fragments/MainForumListFragment.kt | 78 ------ .../tieba/post/utils/TiebaUtil.kt | 5 - 5 files changed, 1 insertion(+), 476 deletions(-) delete mode 100644 app/src/main/java/com/huanchengfly/tieba/post/adapters/OKSignProgressAdapter.kt delete mode 100644 app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWork.kt diff --git a/app/build.gradle b/app/build.gradle index 324eec28..68271f09 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -126,9 +126,6 @@ dependencies { implementation "androidx.navigation:navigation-compose:2.5.3" - def work_version = "2.7.1" - implementation "androidx.work:work-runtime-ktx:$work_version" - api "com.squareup.wire:wire-runtime:$wire_version" def hilt_androidx_version = "1.0.0" @@ -260,7 +257,7 @@ dependencies { transitive = true } - def appCenterSdkVersion = '4.4.5' + def appCenterSdkVersion = '5.0.0' implementation "com.microsoft.appcenter:appcenter-analytics:${appCenterSdkVersion}" implementation "com.microsoft.appcenter:appcenter-crashes:${appCenterSdkVersion}" implementation "com.microsoft.appcenter:appcenter-distribute:${appCenterSdkVersion}" diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/OKSignProgressAdapter.kt b/app/src/main/java/com/huanchengfly/tieba/post/adapters/OKSignProgressAdapter.kt deleted file mode 100644 index 566e4f81..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/adapters/OKSignProgressAdapter.kt +++ /dev/null @@ -1,136 +0,0 @@ -package com.huanchengfly.tieba.post.adapters - -import android.content.Context -import android.util.Log -import android.view.ViewGroup -import androidx.work.Data -import androidx.work.hasKeyWithValueOfType -import com.alibaba.android.vlayout.DelegateAdapter -import com.alibaba.android.vlayout.LayoutHelper -import com.alibaba.android.vlayout.layout.SingleLayoutHelper -import com.huanchengfly.tieba.post.R -import com.huanchengfly.tieba.post.components.MyViewHolder -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_ACCOUNT_NICKNAME -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_CURRENT_POSITION -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_ERROR -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_ERROR_CODE -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_ERROR_MESSAGE -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_STARTED -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_SUCCESS -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_TIMESTAMP -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_TOTAL_COUNT -import com.huanchengfly.tieba.post.ui.widgets.CircleProgressView -import com.huanchengfly.tieba.post.utils.DateTimeUtils - -class OKSignProgressAdapter( - val context: Context, - var data: Data? = null, -) : DelegateAdapter.Adapter() { - var closed = false - set(value) { - field = value - notifyDataSetChanged() - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { - return MyViewHolder(context, R.layout.item_oksign_progress, parent) - } - - override fun getItemCount(): Int = 1 - - override fun onCreateLayoutHelper(): LayoutHelper = SingleLayoutHelper() - - override fun onBindViewHolder(holder: MyViewHolder, position: Int) { - Log.i("OKSignProgressAdapter", "${holder.itemView}") - data.let { - if (it == null || !it.hasKeyWithValueOfType(DATA_SUCCESS) || closed) { - holder.setVisibility(R.id.oksign_progress, false) - return@let - } - holder.setOnClickListener(R.id.oksign_progress_close_btn) { - closed = true - } - holder.setVisibility(R.id.oksign_progress, true) - val success = it.getBoolean(DATA_SUCCESS, false) - val started = it.getBoolean(DATA_STARTED, true) - val error = it.getBoolean(DATA_ERROR, false) - val currentPosition = it.getInt(DATA_CURRENT_POSITION, 0) - val totalCount = it.getInt(DATA_TOTAL_COUNT, 0) - val accountNickname = it.getString(DATA_ACCOUNT_NICKNAME) - val timestamp = it.getLong(DATA_TIMESTAMP, System.currentTimeMillis()) - if (success) { - holder.setVisibility(R.id.oksign_progress_close_btn, true) - holder.setVisibility(R.id.oksign_progress_progress, false) - holder.setVisibility(R.id.oksign_progress_progress_indeterminate, false) - holder.setVisibility(R.id.oksign_progress_icon, true) - holder.setVisibility(R.id.oksign_progress_content, true) - holder.setImageResource(R.id.oksign_progress_icon, R.drawable.ic_round_check) - holder.setText( - R.id.oksign_progress_title, - context.getString( - R.string.title_oksign_finish_time, - DateTimeUtils.getRelativeTimeString(context, timestamp) - ) - ) - holder.setText( - R.id.oksign_progress_content, - if (totalCount > 0) context.getString( - R.string.text_oksign_done, - totalCount - ) else context.getString(R.string.text_oksign_no_signable) - ) - } else if (error) { - val errorCode = it.getInt(DATA_ERROR_CODE, 0) - val errorMessage = it.getString(DATA_ERROR_MESSAGE) - holder.setText( - R.id.oksign_progress_title, - context.getString(R.string.title_oksign_fail) - ) - holder.setText(R.id.oksign_progress_content, errorMessage) - if (totalCount > 0) { - holder.setVisibility(R.id.oksign_progress_close_btn, false) - holder.setVisibility(R.id.oksign_progress_progress, true) - holder.setVisibility(R.id.oksign_progress_progress_indeterminate, false) - holder.setVisibility(R.id.oksign_progress_icon, false) - holder.setVisibility(R.id.oksign_progress_content, true) - holder.getView(R.id.oksign_progress_progress).progress = - currentPosition * 100 / totalCount - } else { - holder.setVisibility(R.id.oksign_progress_close_btn, true) - holder.setVisibility(R.id.oksign_progress_progress, false) - holder.setVisibility(R.id.oksign_progress_progress_indeterminate, false) - holder.setVisibility(R.id.oksign_progress_icon, true) - holder.setVisibility(R.id.oksign_progress_content, false) - holder.setImageResource(R.id.oksign_progress_icon, R.drawable.ic_round_clear_24) - } - } else if (started && totalCount > 0) { - holder.setVisibility(R.id.oksign_progress_close_btn, false) - holder.setVisibility(R.id.oksign_progress_progress, true) - holder.setVisibility(R.id.oksign_progress_progress_indeterminate, false) - holder.setVisibility(R.id.oksign_progress_icon, false) - holder.setVisibility(R.id.oksign_progress_content, false) - holder.getView(R.id.oksign_progress_progress).progress = - currentPosition * 100 / totalCount - holder.setText( - R.id.oksign_progress_title, - context.getString( - R.string.title_signing_progress, - accountNickname, - currentPosition, - totalCount - ) - ) - } else { - holder.setVisibility(R.id.oksign_progress_close_btn, false) - holder.setVisibility(R.id.oksign_progress_progress, false) - holder.setVisibility(R.id.oksign_progress_progress_indeterminate, true) - holder.setVisibility(R.id.oksign_progress_icon, false) - holder.setVisibility(R.id.oksign_progress_content, false) - holder.setText( - R.id.oksign_progress_title, - context.getString(R.string.title_loading_data) - ) - } - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWork.kt b/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWork.kt deleted file mode 100644 index 13d1970c..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWork.kt +++ /dev/null @@ -1,253 +0,0 @@ -package com.huanchengfly.tieba.post.components.workers - -import android.Manifest -import android.app.PendingIntent -import android.content.Context -import android.content.Intent -import android.content.pm.PackageManager -import android.widget.Toast -import androidx.core.app.ActivityCompat -import androidx.core.app.NotificationChannelCompat -import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat -import androidx.work.CoroutineWorker -import androidx.work.Data -import androidx.work.ForegroundInfo -import androidx.work.WorkerParameters -import com.huanchengfly.tieba.post.R -import com.huanchengfly.tieba.post.activities.MainActivity -import com.huanchengfly.tieba.post.api.models.SignResultBean -import com.huanchengfly.tieba.post.models.SignDataBean -import com.huanchengfly.tieba.post.models.database.Account -import com.huanchengfly.tieba.post.pendingIntentFlagImmutable -import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils -import com.huanchengfly.tieba.post.utils.AccountUtil -import com.huanchengfly.tieba.post.utils.ProgressListener -import com.huanchengfly.tieba.post.utils.SingleAccountSigner -import com.huanchengfly.tieba.post.utils.addFlag -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext - -class OKSignWork( - private val context: Context, - workerParams: WorkerParameters -) : - CoroutineWorker(context, workerParams), ProgressListener { - private var currentAccount: Account? = null - - private val notificationManager: NotificationManagerCompat by lazy { - NotificationManagerCompat.from( - context - ) - } - - override suspend fun doWork(): Result { - return withContext(Dispatchers.IO) { - val account = AccountUtil.getLoginInfo().also { currentAccount = it } - ?: return@withContext Result.failure() - updateNotification( - context.getString(R.string.title_loading_data), - context.getString(R.string.text_please_wait) - ) - setProgressAsync(buildData(false, 0, 0, started = false)) - runBlocking { - val success = - SingleAccountSigner(context = context, account = account).setProgressListener( - this@OKSignWork - ).start() - if (success) { - Result.success() - } else { - Result.failure() - } - } - } - } - - override suspend fun getForegroundInfo(): ForegroundInfo { - return ForegroundInfo( - 9, - buildNotification( - context.getString(R.string.title_oksign), - context.getString(R.string.tip_oksign_running) - ).build() - ) - } - - private fun createNotificationChannel() { - notificationManager.createNotificationChannel( - NotificationChannelCompat.Builder( - NOTIFICATION_CHANNEL_ID, - NotificationManagerCompat.IMPORTANCE_LOW - ) - .setName(context.getString(R.string.title_oksign)) - .setLightsEnabled(false) - .setShowBadge(false) - .build() - ) - } - - private fun buildNotification(title: String, text: String?): NotificationCompat.Builder { - createNotificationChannel() - return NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) - .setContentText(text) - .setContentTitle(title) - .setSubText(context.getString(R.string.title_oksign)) - .setSmallIcon(R.drawable.ic_oksign) - .setAutoCancel(true) - .setStyle(NotificationCompat.BigTextStyle()) - .setColor(ThemeUtils.getColorByAttr(context, R.attr.colorPrimary)) - } - - private fun updateNotification(title: String, text: String, intent: Intent) { - if (ActivityCompat.checkSelfPermission( - context, - Manifest.permission.POST_NOTIFICATIONS - ) != PackageManager.PERMISSION_GRANTED - ) { - return - } - notificationManager.notify( - 1, - buildNotification(title, text) - .setContentIntent( - PendingIntent.getActivity( - context, - 0, - intent, - pendingIntentFlagImmutable() - ) - ) - .build() - ) - } - - private fun updateNotification(title: String, text: String?) { - if (ActivityCompat.checkSelfPermission( - context, - Manifest.permission.POST_NOTIFICATIONS - ) != PackageManager.PERMISSION_GRANTED - ) { - return - } - val notification = buildNotification(title, text) - .build() - notification.flags = notification.flags.addFlag(NotificationCompat.FLAG_ONGOING_EVENT) - notificationManager.notify(1, notification) - } - - private fun buildData( - success: Boolean, - current: Int, - total: Int, - errorCode: Int? = null, - errorMessage: String? = null, - started: Boolean = true, - ): Data { - return Data.Builder() - .putBoolean(DATA_STARTED, started) - .putBoolean(DATA_SUCCESS, success) - .putBoolean(DATA_ERROR, errorCode != null) - .apply { - if (errorCode != null) { - putInt(DATA_ERROR_CODE, errorCode) - putString(DATA_ERROR_MESSAGE, errorMessage) - } - currentAccount?.let { - putInt(DATA_ACCOUNT_ID, it.id) - putString(DATA_ACCOUNT_NICKNAME, it.nameShow ?: it.name) - } - } - .putInt(DATA_CURRENT_POSITION, current) - .putInt(DATA_TOTAL_COUNT, total) - .putLong(DATA_TIMESTAMP, System.currentTimeMillis()) - .build() - } - - override fun onStart(total: Int) { - setProgressAsync(buildData(false, 0, total)) - updateNotification(context.getString(R.string.title_start_sign), null) - if (total > 0) Toast.makeText( - context, - R.string.toast_oksign_start, - Toast.LENGTH_SHORT - ).show() - } - - override fun onProgressStart(signDataBean: SignDataBean, current: Int, total: Int) { - setProgressAsync(buildData(false, current, total)) - updateNotification( - context.getString( - R.string.title_signing_progress, - signDataBean.userName, - current, - total - ), - context.getString( - R.string.title_forum_name, - signDataBean.forumName - ) - ) - } - - override fun onProgressFinish( - signDataBean: SignDataBean, - signResultBean: SignResultBean, - current: Int, - total: Int - ) { - setProgressAsync(buildData(false, current + 1, total)) - updateNotification( - context.getString( - R.string.title_signing_progress, - signDataBean.userName, - current + 1, - total - ), - if (signResultBean.userInfo?.signBonusPoint != null) - context.getString( - R.string.text_singing_progress_exp, - signDataBean.forumName, - signResultBean.userInfo.signBonusPoint - ) - else - context.getString(R.string.text_singing_progress, signDataBean.forumName) - ) - } - - override fun onFinish(success: Boolean, signedCount: Int, total: Int) { - setProgressAsync(buildData(true, total, total)) - updateNotification( - context.getString(R.string.title_oksign_finish), - if (total > 0) context.getString( - R.string.text_oksign_done, - signedCount - ) else context.getString(R.string.text_oksign_no_signable), - Intent(context, MainActivity::class.java) - ) - context.sendBroadcast(Intent(ACTION_SIGN_SUCCESS_ALL)) - } - - override fun onFailure(current: Int, total: Int, errorCode: Int, errorMsg: String) { - setProgressAsync(buildData(false, current, total, errorCode, errorMsg)) - updateNotification(context.getString(R.string.title_oksign_fail), errorMsg) - } - - companion object { - const val DATA_ACCOUNT_ID = "account_id" - const val DATA_ACCOUNT_NICKNAME = "account_nickname" - const val DATA_SUCCESS = "success" - const val DATA_STARTED = "started" - const val DATA_ERROR = "error" - const val DATA_ERROR_CODE = "error_code" - const val DATA_ERROR_MESSAGE = "error_message" - const val DATA_CURRENT_POSITION = "current_position" - const val DATA_TOTAL_COUNT = "total_count" - const val DATA_TIMESTAMP = "timestamp" - - const val NOTIFICATION_CHANNEL_ID = "1" - const val ACTION_SIGN_SUCCESS_ALL = - "com.huanchengfly.tieba.post.service.action.SIGN_SUCCESS_ALL" - } -} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/MainForumListFragment.kt b/app/src/main/java/com/huanchengfly/tieba/post/fragments/MainForumListFragment.kt index be202ba6..35f2d072 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/MainForumListFragment.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/MainForumListFragment.kt @@ -10,9 +10,6 @@ import android.widget.Toast import androidx.appcompat.widget.Toolbar import androidx.constraintlayout.motion.widget.MotionLayout import androidx.recyclerview.widget.RecyclerView -import androidx.work.Data -import androidx.work.WorkManager -import androidx.work.hasKeyWithValueOfType import butterknife.BindView import com.alibaba.android.vlayout.DelegateAdapter import com.alibaba.android.vlayout.VirtualLayoutManager @@ -22,7 +19,6 @@ import com.huanchengfly.tieba.post.activities.ForumActivity import com.huanchengfly.tieba.post.activities.NewSearchActivity import com.huanchengfly.tieba.post.adapters.HeaderDelegateAdapter import com.huanchengfly.tieba.post.adapters.MainForumListAdapter -import com.huanchengfly.tieba.post.adapters.OKSignProgressAdapter import com.huanchengfly.tieba.post.adapters.base.OnItemClickListener import com.huanchengfly.tieba.post.adapters.base.OnItemLongClickListener import com.huanchengfly.tieba.post.api.Error @@ -33,8 +29,6 @@ import com.huanchengfly.tieba.post.api.models.ForumRecommend import com.huanchengfly.tieba.post.api.retrofit.exception.TiebaException import com.huanchengfly.tieba.post.api.retrofit.exception.TiebaLocalException import com.huanchengfly.tieba.post.components.MyViewHolder -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_SUCCESS -import com.huanchengfly.tieba.post.components.workers.OKSignWork.Companion.DATA_TIMESTAMP import com.huanchengfly.tieba.post.interfaces.Refreshable import com.huanchengfly.tieba.post.models.database.TopForum import com.huanchengfly.tieba.post.utils.* @@ -43,8 +37,6 @@ import com.huanchengfly.tieba.post.utils.preload.PreloadUtil import com.huanchengfly.tieba.post.utils.preload.loaders.ForumLoader import com.scwang.smart.refresh.header.MaterialHeader import com.scwang.smart.refresh.layout.SmartRefreshLayout -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import org.litepal.LitePal import retrofit2.Call import retrofit2.Callback @@ -92,15 +84,6 @@ class MainForumListFragment : BaseFragment(), Refreshable, Toolbar.OnMenuItemCli var listSingle: Boolean = false - private var okSignProgressData: Data? = null - set(value) { - field = value - okSignProgressAdapter.data = value - okSignProgressAdapter.notifyDataSetChanged() - reloadAdapters() - } - private lateinit var okSignProgressAdapter: OKSignProgressAdapter - fun reset() { if (isFragmentVisible) { mRefreshView.autoRefresh() @@ -118,9 +101,6 @@ class MainForumListFragment : BaseFragment(), Refreshable, Toolbar.OnMenuItemCli mRecyclerView.recycledViewPool.clear() reloadAdapters() } - if (view != null) { - runCatching { refreshOKSignProgress() } - } } override fun onAccountSwitch() { @@ -181,68 +161,11 @@ class MainForumListFragment : BaseFragment(), Refreshable, Toolbar.OnMenuItemCli private fun startSign() { TiebaUtil.startSign(attachContext) - runCatching { refreshOKSignProgress() } - } - - private fun refreshOKSignProgress() { - launchIO { - val workInfos = - withContext(Dispatchers.IO) { - WorkManager.getInstance(attachContext).getWorkInfosForUniqueWork("OKSign").get() - } - withContext(Dispatchers.Main) { - if (workInfos.isNotEmpty()) { - val workInfo = workInfos[0] - val id = workInfo.id - if (appPreferences.oksignWorkId != id.toString()) { - appPreferences.oksignWorkId = id.toString() - okSignProgressAdapter.data = null - okSignProgressAdapter.closed = false - } - val progressData = workInfo.progress - okSignProgressData = - if (progressData.hasKeyWithValueOfType(DATA_SUCCESS)) { - progressData - } else { - null - } - val workInfoLiveData = - WorkManager.getInstance(attachContext).getWorkInfoByIdLiveData(id) - if (view != null) { - runCatching { - workInfoLiveData.observe(viewLifecycleOwner) { - runCatching { - if (it == null) { - workInfoLiveData.removeObservers(viewLifecycleOwner) - return@observe - } - if (it.progress.getBoolean(DATA_SUCCESS, false)) { - workInfoLiveData.removeObservers(viewLifecycleOwner) - } - } - if (DateTimeUtils.isToday( - it.progress.getLong( - DATA_TIMESTAMP, - System.currentTimeMillis() - ) - ) && it.progress.hasKeyWithValueOfType(DATA_SUCCESS) - ) { - okSignProgressData = it.progress - } - } - } - } - } else { - okSignProgressData = null - } - } - } } @SuppressLint("ApplySharedPref") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - okSignProgressAdapter = OKSignProgressAdapter(attachContext, okSignProgressData) topForumListAdapter = MainForumListAdapter( attachContext, spanCount @@ -304,7 +227,6 @@ class MainForumListFragment : BaseFragment(), Refreshable, Toolbar.OnMenuItemCli delegateAdapter.clear() topForumListAdapter.spanCount = spanCount mainForumListAdapter.spanCount = spanCount - delegateAdapter.addAdapter(okSignProgressAdapter) if (topForumItems.isNotEmpty()) { delegateAdapter.addAdapter(HeaderDelegateAdapter( attachContext, diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt index 8cb2434c..23d37212 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt @@ -102,11 +102,6 @@ object TiebaUtil { .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setAction(OKSignService.ACTION_START_SIGN) ) -// val okSignWork = OneTimeWorkRequestBuilder() -// .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) -// .build() -// WorkManager.getInstance(context) -// .enqueueUniqueWork("OKSign", ExistingWorkPolicy.KEEP, okSignWork) } @JvmStatic