Skip to content

Commit

Permalink
添加一个非常简单的历史记录保存与读取
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSkidder committed Jun 21, 2023
1 parent 4f3dd0a commit fe99950
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
implementation 'androidx.core:core-ktx:+'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/me/superskidder/watchgpt/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.superskidder.watchgpt

import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -12,12 +11,14 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.google.gson.GsonBuilder
import me.superskidder.watchgpt.R
import me.superskidder.watchgpt.config.SimpleConfig
import me.superskidder.watchgpt.data.ChatCompletionMessage
import me.superskidder.watchgpt.data.ChatGPTApi
import me.superskidder.watchgpt.data.ChatGPTRequest
import me.superskidder.watchgpt.data.ChatGPTResponse
import me.superskidder.watchgpt.history.messages
import me.superskidder.watchgpt.history.readHistory
import me.superskidder.watchgpt.history.saveHistory
import me.superskidder.watchgpt.speaking.BaiduTranslator
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -26,7 +27,6 @@ import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.Locale
import java.util.concurrent.TimeUnit


Expand All @@ -42,14 +42,12 @@ class MainFragment : Fragment() {
private var apiKey = ""
var systemPrompt = "You are a helpful assistant."

private var messages: List<ChatCompletionMessage> =
listOf(ChatCompletionMessage("system", systemPrompt))


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

return inflater.inflate(R.layout.fragment_main, container, false)
}

Expand Down Expand Up @@ -100,6 +98,8 @@ class MainFragment : Fragment() {
}

baiduTranslator = BaiduTranslator()
readHistory(requireContext())
update()
}

fun msgBox(title: String, content: String, btn: String) {
Expand All @@ -113,6 +113,11 @@ class MainFragment : Fragment() {
dialog.show()
}

override fun onDestroy() {
super.onDestroy()
saveHistory(requireContext())
}


private fun sendMessage(message: String) {
val configManager = SimpleConfig(requireContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.superskidder.watchgpt.adapters
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import me.superskidder.watchgpt.MainFragment
import me.superskidder.watchgpt.SettingsFragment

class MainFragmentAdapter(fragmentActivity: FragmentActivity) :
Expand Down

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/java/me/superskidder/watchgpt/history/HistoryUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.superskidder.watchgpt.history

import android.content.Context
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import me.superskidder.watchgpt.MainFragment
import me.superskidder.watchgpt.config.SimpleConfig
import me.superskidder.watchgpt.data.ChatCompletionMessage

var messages: List<ChatCompletionMessage> = ArrayList()
fun saveHistory(context: Context) {
val configManager = SimpleConfig(context)
val gson = GsonBuilder().create()
configManager.setString("history", gson.toJson(messages))
}

fun readHistory(context: Context) {
val configManager = SimpleConfig(context)
val gson = GsonBuilder().create()
val config = configManager.getString("history", gson.toJson(messages))
messages = gson.fromJson(config, ArrayList<ChatCompletionMessage>().javaClass)
println(messages.forEach(::println))
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0-RC' apply false
}

0 comments on commit fe99950

Please sign in to comment.