Skip to content

Commit

Permalink
feat: 自动使用上次选择的源
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jul 22, 2023
1 parent 216d674 commit fd3a1ba
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
package com.github.jing332.text_searcher.const

import androidx.compose.ui.text.font.FontWeight

object ConfigConst {
const val KEY_LAST_SOURCE_ID = "last_source_id"
const val KEY_FONT_DIR = "font_dir"
const val KEY_GPT_TITLE_FONT_SIZE = "gpt_title_font_size"
const val KEY_GPT_TITLE_FONT_WEIGHT = "gpt_title_font_weight"
const val KEY_GPT_TITLE_LINE_HEIGHT = "gpt_title_line_height"

const val KEY_GPT_FONT_SIZE = "gpt_font_size"
const val KEY_GPT_FONT_WEIGHT = "gpt_font_weight"
const val KEY_GPT_LINE_HEIGHT_SCALE = "gpt_line_height_scale"

const val KEY_OPEN_AI_MODEL = "open_ai_model"
const val KEY_OPEN_AI_API_KEY = "open_ai_api_key"
const val KEY_MSG_TEMPLATE = "message_template"
const val KEY_SYSTEM_PROMPT = "system_prompts"

const val KEY_TEST_TEXT = "test_text"

const val VALUE_OPEN_AI_MODEL = "gpt-3.5-turbo"
const val VALUE_GPT_FONT_SIZE = 16
const val VALUE_GPT_TITLE_FONT_SIZE = 18

/* 👇 默认值 */
const val VALUE_GPT_LINE_HEIGHT_SCALE = 1.2f
const val VALUE_GPT_TITLE_LINE_HEIGHT = 1f
val VALUE_GPT_TITLE_FONT_WEIGHT = FontWeight.Normal.weight
val VALUE_GPT_FONT_WEIGHT = FontWeight.Normal.weight
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.github.jing332.text_searcher.R
import com.github.jing332.text_searcher.app
import com.github.jing332.text_searcher.data.dao.SearchSourceDao
import com.github.jing332.text_searcher.data.entites.SearchSource
import com.github.jing332.text_searcher.model.source.ChatGptSourceEntity

val appDb by lazy { AppDatabase.createDatabase(app) }
val appDb: AppDatabase by lazy { AppDatabase.createDatabase(app) }

@Database(
version = 2,
Expand All @@ -27,24 +24,13 @@ abstract class AppDatabase : RoomDatabase() {
companion object {
private const val DATABASE_NAME = "text_searcher.db"

fun createDatabase(context: Context) = Room
.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.allowMainThreadQueries()
.addCallback(object : RoomDatabase.Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)

// if (appDb.searchSource.count == 0) {
// appDb.searchSource.insert(
// SearchSource(
// name = getString(R.string.chatgpt_search_source_name),
// sourceEntity = ChatGptSourceEntity()
// )
// )
// }
}
})
.build()
fun createDatabase(context: Context): AppDatabase {
return Room
.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.allowMainThreadQueries()
.build()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,18 @@ import com.github.jing332.text_searcher.const.ConfigConst
object AppConfig {
val dataSaverPref = DataSaverPreferences(app.getSharedPreferences("app", 0))

// val openAiApiKey = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_OPEN_AI_API_KEY,
// initialValue = ""
// )
//
// val openAiModel = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_OPEN_AI_MODEL,
// initialValue = ConfigConst.VALUE_OPEN_AI_MODEL
// )
//
// val msgTemplate = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_MSG_TEMPLATE,
// initialValue = ""
// )
//
// val systemPrompt = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_SYSTEM_PROMPT,
// initialValue = ""
// )
//
var lastSourceId = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = ConfigConst.KEY_LAST_SOURCE_ID,
initialValue = 0L
)

var fontDir = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = ConfigConst.KEY_FONT_DIR,
initialValue = ""
)
//
// var gptTitleFontSize = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_TITLE_FONT_SIZE,
// initialValue = ConfigConst.VALUE_GPT_TITLE_FONT_SIZE
// )
//
// var gptTitleFontWeight = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_TITLE_FONT_WEIGHT,
// initialValue = ConfigConst.VALUE_GPT_TITLE_FONT_WEIGHT
// )
//
// var gptTitleLineHeight = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_TITLE_LINE_HEIGHT,
// initialValue = ConfigConst.VALUE_GPT_TITLE_LINE_HEIGHT
// )
//
// var gptFontSize = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_FONT_SIZE,
// initialValue = ConfigConst.VALUE_GPT_FONT_SIZE
// )
//
// var gptFontWeight = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_FONT_WEIGHT,
// initialValue = ConfigConst.VALUE_GPT_FONT_WEIGHT
// )
//
// var gptLineHeightScale = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_GPT_LINE_HEIGHT_SCALE,
// initialValue = ConfigConst.VALUE_GPT_LINE_HEIGHT_SCALE
// )
//
// val testText = mutableDataSaverStateOf(
// dataSaverInterface = dataSaverPref,
// key = ConfigConst.KEY_TEST_TEXT,
// initialValue = ""
// )


fun fillDefaultValues(context: Context) {
// if (systemPrompt.value.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.funny.data_saver.core.LocalDataSaver
import com.github.jing332.text_searcher.R
import com.github.jing332.text_searcher.app
import com.github.jing332.text_searcher.data.appDb
import com.github.jing332.text_searcher.data.entites.SearchSource
import com.github.jing332.text_searcher.help.AppConfig
import com.github.jing332.text_searcher.model.source.ChatGptSourceEntity
import com.github.jing332.text_searcher.model.source.WebSiteSourceEntity
import com.github.jing332.text_searcher.ui.theme.TxtSearcherTheme
import com.github.jing332.text_searcher.ui.widgets.TransparentSystemBars

Expand All @@ -36,7 +38,21 @@ class MainActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
AppConfig.fillDefaultValues(this)

if (appDb.searchSource.count == 0) {
appDb.searchSource.insert(
SearchSource(
name = getString(R.string.chatgpt_search_source_name),
sourceEntity = ChatGptSourceEntity()
)
)

appDb.searchSource.insert(
SearchSource(
name = getString(R.string.bing_search),
sourceEntity = WebSiteSourceEntity(url = "https://www.bing.com/search?q=\${text}")
)
)
}

setContent {
CompositionLocalProvider(
Expand Down Expand Up @@ -78,7 +94,7 @@ class MainActivity : ComponentActivity() {
navController.popBackStack()
return@composable
} else {
var vSrc by remember{ mutableStateOf(src) }
var vSrc by remember { mutableStateOf(src) }
vSrc.sourceEntity.EditScreen(src = vSrc, onChanged = { changedSrc ->
vSrc = changedSrc
appDb.searchSource.insert(changedSrc)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.jing332.text_searcher.ui.search

import android.annotation.SuppressLint
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
Expand All @@ -12,12 +11,13 @@ import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Surface
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -33,10 +33,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.github.jing332.text_searcher.R
import com.github.jing332.text_searcher.data.appDb
import com.google.accompanist.web.AccompanistWebChromeClient
import com.google.accompanist.web.AccompanistWebViewClient
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewState
import com.github.jing332.text_searcher.help.AppConfig
import kotlinx.coroutines.launch


Expand Down Expand Up @@ -69,6 +66,12 @@ fun SearcherDialog(onDismissRequest: () -> Unit, inputText: String) {
val sourceList = rememberSaveable { appDb.searchSource.all }
val pages = rememberSaveable { sourceList.map { it.name } }
val scope = rememberCoroutineScope()
val initialPage =
remember {
sourceList.indexOfFirst { AppConfig.lastSourceId.value == it.id }
.run { if (this > -1) this else 0 }
}

if (pages.isEmpty())
Text(
stringResource(R.string.please_add_search_source),
Expand All @@ -79,17 +82,33 @@ fun SearcherDialog(onDismissRequest: () -> Unit, inputText: String) {
)
else
Column {
val pagerState = rememberPagerState { pages.size }
TabRow(selectedTabIndex = pagerState.currentPage, indicator = { tabPositions ->
TabIndicator(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.tabIndicatorOffset(tabPositions[pagerState.currentPage])
)
}) {
var lastId by remember { AppConfig.lastSourceId }
val pagerState = rememberPagerState(initialPage) { pages.size }

DisposableEffect(key1 = pagerState.hashCode()) {
onDispose {
sourceList.getOrNull(pagerState.currentPage)?.let { lastId = it.id }
}
}
ScrollableTabRow(
selectedTabIndex = pagerState.currentPage,
indicator = { tabPositions ->
TabIndicator(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.tabIndicatorOffset(tabPositions[pagerState.currentPage])
)
}
) {
pages.forEachIndexed { index, title ->
val selected = index == pagerState.currentPage
Tab(
text = { Text(title) },
selected = index == pagerState.currentPage,
text = {
Text(
title,
fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal
)
},
selected = selected,
onClick = {
scope.launch {
pagerState.animateScrollToPage(index)
Expand All @@ -108,16 +127,6 @@ fun SearcherDialog(onDismissRequest: () -> Unit, inputText: String) {
}
}


@Preview
@Composable
fun PreviewChatGPTSettingsDialog() {
var show by remember { mutableStateOf(true) }
// if (show) {
// ChatGPTAppearanceSettingsDialog(onDismissRequest = { show = false })
// }
}

@Preview
@Composable
private fun PreviewSearcherDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ private fun WebViewScreen(modifier: Modifier, url: String) {
progress = process
)

var lastTitle by remember { mutableStateOf("") }
Text(
modifier = Modifier.align(Alignment.CenterHorizontally),
text = state.pageTitle ?: url,
text = state.pageTitle?.apply { lastTitle = this } ?: lastTitle,
maxLines = 1,
style = MaterialTheme.typography.titleMedium,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ fun ExpandableText(
lineHeight: TextUnit = LocalTextStyle.current.lineHeight,
text: String,
collapsedMaxLine: Int = 2,
showMoreText: String = stringResource(R.string.expandble_text_more),
showMoreText: String = stringResource(R.string.expandable_text_more),
showMoreStyle: SpanStyle = SpanStyle(fontWeight = FontWeight.ExtraBold, color = MaterialTheme.colorScheme.primary),
showLessText: String = stringResource(R.string.expandble_text_less),
showLessText: String = stringResource(R.string.expandable_text_less),
showLessStyle: SpanStyle = showMoreStyle,
textAlign: TextAlign? = null,
) {
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<resources>
<string name="app_name">ChatGPT文本搜索器</string>
<string name="app_name">文本搜索器</string>

<string name="qrcode">二维码</string>
<string name="text_searcher_label">ChatGPT·文本搜索</string>
<string name="text_searcher_label">文本搜索</string>
<string name="gpt_system_prompt">根据用户输入来输出其意义或字面意思</string>
<string name="message_template">$text</string>
<string name="more_options">更多选项</string>
Expand All @@ -21,8 +21,8 @@
<string name="appearance_settings">外观设置</string>
<string name="font_size">字体大小:%s</string>
<string name="line_height">行间隔:%s</string>
<string name="expandble_text_more">" 展开"</string>
<string name="expandble_text_less">" 收起"</string>
<string name="expandable_text_more">" 展开"</string>
<string name="expandable_text_less">" 收起"</string>
<string name="font_weight">字体粗细:%s</string>
<string name="font_weight_bold">粗体</string>
<string name="font_weight_normal">正常</string>
Expand All @@ -42,7 +42,7 @@
<string name="font">字体</string>
<string name="choose_font">选择字体</string>
<string name="system_default_font">系统默认字体</string>
<string name="chatgpt_search_source_name">ChatGPT搜索源</string>
<string name="chatgpt_search_source_name">ChatGPT搜索</string>
<string name="error_open_ai_api_key_empty">错误: 请填写OpenAI API 密钥</string>
<string name="up_move">上移</string>
<string name="down_move">下移</string>
Expand All @@ -69,4 +69,5 @@
<string name="external_browser">外部浏览器</string>
<string name="select_browser">选择浏览器</string>
<string name="search_engine">搜索引擎</string>
<string name="bing_search">Bing搜索</string>
</resources>

0 comments on commit fd3a1ba

Please sign in to comment.