Skip to content

Commit

Permalink
v7.9 (#60)
Browse files Browse the repository at this point in the history
* Add firebase database library

* Read openai key from firebase

* Update version
  • Loading branch information
aqua-ix authored Mar 9, 2024
1 parent 5d9a00e commit 4c78e51
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "comviewaquahp.google.sites.youbimiku"
minSdkVersion 21
targetSdkVersion 33
versionCode 28
versionName "7.8"
versionCode 29
versionName "7.9"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
packagingOptions {
Expand Down Expand Up @@ -58,6 +58,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.firebase:firebase-database-ktx:20.3.0'
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
84 changes: 67 additions & 17 deletions app/src/main/java/com/aqua_ix/youbimiku/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import com.aqua_ix.youbimiku.config.*
import com.aqua_ix.youbimiku.databinding.ActivityMainBinding
import com.github.bassaer.chatmessageview.model.Message
import com.google.android.play.core.review.ReviewManagerFactory
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfig
Expand All @@ -40,6 +44,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
private lateinit var detectIntent: DetectIntent
private lateinit var openAI: OpenAI
private lateinit var remoteConfig: FirebaseRemoteConfig
private lateinit var database: FirebaseDatabase
private lateinit var navMenu: Menu

private var openAIPreviousResponse = ""
Expand All @@ -64,11 +69,11 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
initBanner()
initInterstitial()
initRemoteConfig()
initDatabase()
showInAppReviewIfNeeded()

val openAIConfig = OpenAIConfig(token = OPENAI_API_KEY, organization = OPENAI_ORG_ID)
openAI = OpenAI(openAIConfig)
setup()
setupOpenAI()
setupChat()
}

private fun initRemoteConfig() {
Expand All @@ -81,6 +86,24 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
remoteConfig.fetchAndActivate()
}

private fun initDatabase() {
database = FirebaseDatabase.getInstance()
}

private fun onOpenAIError() {
val error = Message.Builder()
.setUser(mikuAccount)
.setRight(false)
.setText(getString(R.string.message_error_openai))
.build()
binding.chatView.receive(error)
mikuAccount.setName(getString(R.string.miku_name))
setAIModel(this, AIModelConfig.DIALOG_FLOW)
if (::navMenu.isInitialized) {
navMenu.findItem(R.id.setting_language).isVisible = true
}
}

private fun initChatView() {
val size = FontSizeConfig.getSize(getFontSizeType(this))
setFontSize(size, binding.chatView)
Expand Down Expand Up @@ -124,6 +147,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
imobileBannerLayout.visibility = View.VISIBLE
mlp.topMargin = imobileBannerLayout.height
}

override fun onFailed(reason: FailNotificationReason) {
Log.d(TAG, "ImobileSdkAd($IMOBILE_BANNER_SID) onFailed: $reason")
imobileBannerLayout.visibility = View.INVISIBLE
Expand All @@ -136,7 +160,12 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
if (FLAVOR == "noAds") {
return
}
ImobileSdkAd.registerSpotFullScreen(this, IMOBILE_PID, IMOBILE_MID, IMOBILE_INTERSTITIAL_SID)
ImobileSdkAd.registerSpotFullScreen(
this,
IMOBILE_PID,
IMOBILE_MID,
IMOBILE_INTERSTITIAL_SID
)
ImobileSdkAd.start(IMOBILE_INTERSTITIAL_SID)
}

Expand All @@ -163,16 +192,8 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {

private fun showOpenAIGreet(userName: String?) {
if (!remoteConfig.getBoolean(RemoteConfigKey.OPENAI_ENABLED)) {
val error = Message.Builder()
.setUser(mikuAccount)
.setRight(false)
.setText(getString(R.string.message_error_openai))
.build()
binding.chatView.receive(error)
setAIModel(this, AIModelConfig.DIALOG_FLOW)
if (::navMenu.isInitialized) {
navMenu.findItem(R.id.setting_language).isVisible = true
}
Log.e("MainActivity", "OpenAI is disabled by remote config.")
onOpenAIError()
return
}
val greeting = resources.getString(R.string.user_nice_to_meet_you, userName)
Expand All @@ -195,7 +216,31 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
}
}

private fun setup() {
private fun setupOpenAI() {
val reference = database.getReference("secrets/openai")

reference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val apiKey = dataSnapshot.child("apiKey").getValue(String::class.java)
val orgId = dataSnapshot.child("orgId").getValue(String::class.java)

apiKey?.let {
val config = OpenAIConfig(token = it, organization = orgId)
openAI = OpenAI(config)
} ?: run {
Log.e("MainActivity", "apiKey is null.")
onOpenAIError()
}
}

override fun onCancelled(databaseError: DatabaseError) {
Log.e("MainActivity", "Database error: ${databaseError.message}")
onOpenAIError()
}
})
}

private fun setupChat() {
if (getUserName(this).equals("")) {
showUserNameDialog(false)
} else {
Expand All @@ -209,7 +254,6 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
if (getAIModel(this).equals("")) {
showAIModelDialog(false)
}

}

private fun showUserNameDialog(cancelable: Boolean = true) {
Expand Down Expand Up @@ -331,22 +375,27 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
showUserNameDialog()
true
}

R.id.setting_ai_model -> {
showAIModelDialog()
true
}

R.id.setting_font_size -> {
showFontSizeDialog()
true
}

R.id.setting_language -> {
showLanguageDialog()
true
}

R.id.setting_official_account -> {
openOfficialAccountIntent()
true
}

else -> super.onOptionsItemSelected(item)
}
}
Expand Down Expand Up @@ -378,14 +427,15 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, DialogListener {
when (getAIModel(this)) {
AIModelConfig.OPEN_AI.name ->
scope.launch {
if(openAITaskJob?.isActive == true) {
if (openAITaskJob?.isActive == true) {
return@launch
}
openAITaskJob = launch {
openAITask(text)
}
setOpenAIRequestCount(applicationContext, ++count)
}

else ->
scope.launch {
dialogFlowTask(text)
Expand Down

0 comments on commit 4c78e51

Please sign in to comment.