From fd3a1baac6fb55c07fc4de51f3f03f46a3c6fd5b Mon Sep 17 00:00:00 2001 From: Jing <42014615+jing332@users.noreply.github.com> Date: Sat, 22 Jul 2023 10:22:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=B8=8A=E6=AC=A1=E9=80=89=E6=8B=A9=E7=9A=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text_searcher/const/ConfigConst.kt | 27 +------ .../jing332/text_searcher/data/AppDatabase.kt | 28 ++----- .../jing332/text_searcher/help/AppConfig.kt | 73 ++----------------- .../jing332/text_searcher/ui/MainActivity.kt | 18 ++++- .../text_searcher/ui/search/SearchDialog.kt | 59 ++++++++------- .../ui/search/website/WebsiteSearchScreen.kt | 3 +- .../ui/widgets/ExpandableText.kt | 4 +- app/src/main/res/values/strings.xml | 11 +-- 8 files changed, 76 insertions(+), 147 deletions(-) diff --git a/app/src/main/java/com/github/jing332/text_searcher/const/ConfigConst.kt b/app/src/main/java/com/github/jing332/text_searcher/const/ConfigConst.kt index c4dd23c..708ab10 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/const/ConfigConst.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/const/ConfigConst.kt @@ -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 } diff --git a/app/src/main/java/com/github/jing332/text_searcher/data/AppDatabase.kt b/app/src/main/java/com/github/jing332/text_searcher/data/AppDatabase.kt index 3a728e6..6cba940 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/data/AppDatabase.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/data/AppDatabase.kt @@ -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, @@ -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() + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/jing332/text_searcher/help/AppConfig.kt b/app/src/main/java/com/github/jing332/text_searcher/help/AppConfig.kt index 4e7a40f..0893a04 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/help/AppConfig.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/help/AppConfig.kt @@ -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()) diff --git a/app/src/main/java/com/github/jing332/text_searcher/ui/MainActivity.kt b/app/src/main/java/com/github/jing332/text_searcher/ui/MainActivity.kt index 8619ca4..63ffc12 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/ui/MainActivity.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/ui/MainActivity.kt @@ -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 @@ -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( @@ -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) diff --git a/app/src/main/java/com/github/jing332/text_searcher/ui/search/SearchDialog.kt b/app/src/main/java/com/github/jing332/text_searcher/ui/search/SearchDialog.kt index ebfb665..3844659 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/ui/search/SearchDialog.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/ui/search/SearchDialog.kt @@ -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 @@ -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 @@ -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 @@ -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), @@ -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) @@ -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() { diff --git a/app/src/main/java/com/github/jing332/text_searcher/ui/search/website/WebsiteSearchScreen.kt b/app/src/main/java/com/github/jing332/text_searcher/ui/search/website/WebsiteSearchScreen.kt index b82669f..2484352 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/ui/search/website/WebsiteSearchScreen.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/ui/search/website/WebsiteSearchScreen.kt @@ -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, ) diff --git a/app/src/main/java/com/github/jing332/text_searcher/ui/widgets/ExpandableText.kt b/app/src/main/java/com/github/jing332/text_searcher/ui/widgets/ExpandableText.kt index 15b1f6c..edf89e2 100644 --- a/app/src/main/java/com/github/jing332/text_searcher/ui/widgets/ExpandableText.kt +++ b/app/src/main/java/com/github/jing332/text_searcher/ui/widgets/ExpandableText.kt @@ -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, ) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f28c657..d21fd49 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,8 +1,8 @@ - ChatGPT文本搜索器 + 文本搜索器 二维码 - ChatGPT·文本搜索 + 文本搜索 根据用户输入来输出其意义或字面意思 $text 更多选项 @@ -21,8 +21,8 @@ 外观设置 字体大小:%s 行间隔:%s - " 展开" - " 收起" + " 展开" + " 收起" 字体粗细:%s 粗体 正常 @@ -42,7 +42,7 @@ 字体 选择字体 系统默认字体 - ChatGPT搜索源 + ChatGPT搜索 错误: 请填写OpenAI API 密钥 上移 下移 @@ -69,4 +69,5 @@ 外部浏览器 选择浏览器 搜索引擎 + Bing搜索 \ No newline at end of file