Skip to content

Commit

Permalink
Implement report
Browse files Browse the repository at this point in the history
  • Loading branch information
aqua-ix committed Nov 15, 2024
1 parent 1dd843b commit 484304c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 14 deletions.
76 changes: 63 additions & 13 deletions app/src/main/java/com/aqua_ix/youbimiku/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.*
import android.view.ViewGroup.LayoutParams.*
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.FrameLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
Expand All @@ -21,9 +25,28 @@ import com.aallam.openai.api.core.FinishReason
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIConfig
import com.aqua_ix.youbimiku.BuildConfig.*
import com.aqua_ix.youbimiku.config.*
import com.aqua_ix.youbimiku.database.*
import com.aqua_ix.youbimiku.BuildConfig.BUILD_TYPE
import com.aqua_ix.youbimiku.BuildConfig.FLAVOR
import com.aqua_ix.youbimiku.BuildConfig.IMOBILE_BANNER_SID
import com.aqua_ix.youbimiku.BuildConfig.IMOBILE_INTERSTITIAL_SID
import com.aqua_ix.youbimiku.BuildConfig.IMOBILE_MID
import com.aqua_ix.youbimiku.BuildConfig.IMOBILE_PID
import com.aqua_ix.youbimiku.BuildConfig.IRONSOURCE_APP_KEY
import com.aqua_ix.youbimiku.config.AIModelConfig
import com.aqua_ix.youbimiku.config.FontSizeConfig
import com.aqua_ix.youbimiku.config.Key
import com.aqua_ix.youbimiku.config.LanguageConfig
import com.aqua_ix.youbimiku.config.SharedPreferenceManager
import com.aqua_ix.youbimiku.config.getAIModel
import com.aqua_ix.youbimiku.config.getFontSizeType
import com.aqua_ix.youbimiku.config.getLanguage
import com.aqua_ix.youbimiku.config.getOpenAIRequestCount
import com.aqua_ix.youbimiku.config.setAIModel
import com.aqua_ix.youbimiku.config.setFontSize
import com.aqua_ix.youbimiku.config.setOpenAIRequestCount
import com.aqua_ix.youbimiku.database.AppDatabase
import com.aqua_ix.youbimiku.database.entityToMessage
import com.aqua_ix.youbimiku.database.messageToEntity
import com.aqua_ix.youbimiku.databinding.ActivityMainBinding
import com.github.bassaer.chatmessageview.model.Message
import com.google.android.play.core.review.ReviewManagerFactory
Expand All @@ -35,7 +58,9 @@ import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import com.ironsource.mediationsdk.*
import com.ironsource.mediationsdk.ISBannerSize
import com.ironsource.mediationsdk.IronSource
import com.ironsource.mediationsdk.IronSourceBannerLayout
import com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo
import com.ironsource.mediationsdk.integration.IntegrationHelper
import com.ironsource.mediationsdk.logger.IronSourceError
Expand All @@ -44,7 +69,15 @@ import com.ironsource.mediationsdk.sdk.LevelPlayInterstitialListener
import jp.co.imobile.sdkads.android.FailNotificationReason
import jp.co.imobile.sdkads.android.ImobileSdkAd
import jp.co.imobile.sdkads.android.ImobileSdkAdListener
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
Expand Down Expand Up @@ -134,20 +167,37 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
binding.chatView.setDateSeparatorFontSize(0F)
binding.chatView.setInputTextHint(getString(R.string.input_text_hint))
binding.chatView.setOnClickSendButtonListener(this)
binding.chatView.setOnBubbleLongClickListener(object : Message.OnBubbleLongClickListener {
override fun onLongClick(message: Message) {
showActionSheet(message)
}
})
binding.chatView.setMessageMaxWidth(640)

CoroutineScope(Dispatchers.Main).launch {
delay(500)
binding.chatView.setOnBubbleLongClickListener(object :
Message.OnBubbleLongClickListener {
override fun onLongClick(message: Message) {
Log.d(TAG, "onLongClick: ${message.text}")
showActionSheet(message)
}
})
}
}

private fun showActionSheet(message: Message) {
val options = arrayOf(getString(R.string.copy_message))
val options = arrayOf(
getString(R.string.copy_message),
getString(R.string.report_message)
)
AlertDialog.Builder(this)
.setItems(options) { _, which ->
when (which) {
0 -> message.text?.let { copyMessageToClipboard(it) }
1 -> message.text?.let {
ReportUtil.showReportReasonDialog(
this,
it,
userAccount.getName() ?: "",
scope
)
}
}
}
.show()
Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/com/aqua_ix/youbimiku/ReportUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.aqua_ix.youbimiku

import android.content.Context
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.HttpURLConnection
import java.net.URL

object ReportUtil {

fun showReportReasonDialog(
context: Context,
text: String,
userName: String,
scope: CoroutineScope
) {
val editText = EditText(context).apply {
hint = context.getString(R.string.report_message_reason)
setPadding(64, 32, 64, 32)
}

AlertDialog.Builder(context)
.setTitle(context.getString(R.string.report_message))
.setView(editText)
.setPositiveButton(context.getString(R.string.report_message_send)) { _, _ -> }
.setNegativeButton(context.getString(R.string.report_message_cancel), null)
.create().apply {
show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val reason = editText.text.toString()
if (reason.isNotBlank()) {
reportMessage(context, userName, text, reason, scope)
dismiss()
} else {
editText.error = context.getString(R.string.report_message_reason_required)
}
}
}
}

private fun reportMessage(
context: Context,
userName: String,
text: String,
reason: String,
scope: CoroutineScope
) {
scope.launch {
try {
val url = URL(BuildConfig.REPORT_END_POINT)
val urlConnection = url.openConnection() as HttpURLConnection
urlConnection.setRequestProperty("Content-Type", "application/json")
urlConnection.doOutput = true

val data =
"""{"timestamp": "${System.currentTimeMillis()}", "userName": "$userName", "text": "$text", "reason": "$reason"}"""
urlConnection.outputStream.use { it.write(data.toByteArray()) }

val responseCode = urlConnection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
withContext(Dispatchers.Main) {
Toast.makeText(
context,
context.getString(R.string.message_reported),
Toast.LENGTH_SHORT
).show()
}
} else {
withContext(Dispatchers.Main) {
Toast.makeText(
context,
context.getString(R.string.message_reported_error),
Toast.LENGTH_SHORT
).show()
}
}
} catch (ex: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(
context,
context.getString(R.string.message_reported_error),
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@

<string name="copy_message">コピー</string>
<string name="message_copied">コピーしました</string>
<string name="report_message">問題を報告</string>
<string name="report_message_reason">理由</string>
<string name="report_message_reason_required">理由を入力してください</string>
<string name="report_message_send">送信</string>
<string name="report_message_cancel">キャンセル</string>
<string name="message_reported">問題を報告しました</string>
<string name="message_reported_error">報告に失敗しました</string>
</resources>
9 changes: 8 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<string name="clear_message_history">Clear Chat History</string>

<string name="official_account">Twitter Account</string>
<string name="official_account">Official Account</string>
<string name="official_account_error">App not found</string>

<string name="setting_submit_review">Submit Review</string>
Expand All @@ -48,4 +48,11 @@

<string name="copy_message">Copy</string>
<string name="message_copied">Copied</string>
<string name="report_message">Report</string>
<string name="report_message_reason">Reason</string>
<string name="report_message_reason_required">Please enter a reason</string>
<string name="report_message_send">Send</string>
<string name="report_message_cancel">Cancel</string>
<string name="message_reported">Reported</string>
<string name="message_reported_error">Report failed</string>
</resources>

0 comments on commit 484304c

Please sign in to comment.