style: Maintain architectural boundaries

This commit is contained in:
AnranYus
2023-10-21 15:31:12 +08:00
parent b9de8535c1
commit 091a891394
3 changed files with 36 additions and 15 deletions
@@ -30,9 +30,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var search: EditText
private var shortAnnotationDuration: Int = 0
private var username: String? = ""
private var nowSourceName: String? = null
val TAG = javaClass.simpleName
private var safeMode: Boolean = true //安全模式
private lateinit var viewModel: MainViewModel
@@ -60,8 +58,7 @@ class MainActivity : AppCompatActivity() {
search.setOnEditorActionListener { content, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
viewModel.uiState.value.nowSearchText.value = content.text.toString()
viewModel.fetchPostByRefresh()
viewModel.fetchPostBySearch(content.text.toString())
}
return@setOnEditorActionListener false
}
@@ -150,18 +147,18 @@ class MainActivity : AppCompatActivity() {
//加载源改变
val configSp = getSharedPreferences("com.lsp.view_preferences", 0)
if (nowSourceName != configSp.getString("source_name",null)){
viewModel.fetchPostByRefresh()
nowSourceName = configSp.getString("source_name",null)
val nowSourceName = configSp.getString("source_name", null)
if (nowSourceName != null && viewModel.uiState.value.nowSourceName.value != nowSourceName){
viewModel.updateNowSource(nowSourceName)
}
if (safeMode != configSp.getBoolean("safe_mode",true)){
viewModel.fetchPostByRefresh()
safeMode = configSp.getBoolean("safe_mode",true)
val nowMode = configSp.getBoolean("safe_mode", true)
if (viewModel.uiState.value.isSafe != nowMode){
viewModel.updateSafeMode(nowMode)
}
// search.setText("")
}
//隐藏搜索栏
@@ -22,7 +22,8 @@ import kotlinx.coroutines.launch
class MainViewModel(private val repository: PostRepository,context: Context):ViewModel() {
private val _uiState = MutableStateFlow(UiState())
val uiState:StateFlow<UiState> = _uiState.asStateFlow()
val uiState:StateFlow<UiState>
get() = _uiState.asStateFlow()
private var fetchJob: Job? = null
val adapter = PostAdapter(context)
private val TAG = this::class.java.simpleName
@@ -62,7 +63,9 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
}
_uiState.update {
it.copy(nowSourceName = configSp.getString("source_name",null))
it.apply {
it.nowSourceName.postValue(configSp.getString("source_name",null))
}
}
}
@@ -109,6 +112,16 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
fetchNewPost()
}
fun fetchPostBySearch(searchTarget:String){
_uiState.update {
it.apply {
it.nowSearchText.postValue(searchTarget)
}
}
fetchNewPost()
}
fun appendPost(){
_uiState.update {
@@ -123,4 +136,15 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
}
fun updateSafeMode(mode:Boolean){
_uiState.update {
it.copy(isSafe = mode)
}
fetchNewPost()
}
fun updateNowSource(source:String){
_uiState.value.nowSourceName.postValue(source)
fetchPostByRefresh()
}
}
@@ -6,7 +6,7 @@ data class UiState(
var showSearchBar:Boolean = false,
val isRefreshing:MutableLiveData<Boolean> = MutableLiveData(),
var nowSearchText:MutableLiveData<String> = MutableLiveData(""),
val nowSourceName: String? = null,
val nowSourceName: MutableLiveData<String> = MutableLiveData(),
val isSafe: Boolean = true, //安全模式
val nowPage:Int = 1,
val isAppend:Boolean = false