Skip to content

Commit

Permalink
皮肤主题切换
Browse files Browse the repository at this point in the history
  • Loading branch information
yollpoll committed Nov 17, 2022
1 parent cd2ceb9 commit 153e43f
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 102 deletions.
26 changes: 17 additions & 9 deletions app/src/main/java/com/yollpoll/nmb/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import androidx.annotation.ChecksSdkIntAtLeast
import androidx.datastore.core.DataStore
import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import com.yollpoll.arch.message.MessageManager
import com.yollpoll.base.NMBApplication
import com.yollpoll.base.R
import com.yollpoll.floweventbus.FlowEventBus
import com.yollpoll.framework.extensions.getString
import com.yollpoll.framework.extensions.putString
import com.yollpoll.framework.extensions.saveBean
import com.yollpoll.nmb.db.MainDB
import com.yollpoll.nmb.model.bean.CookieBean
import com.yollpoll.skin.SkinTheme
Expand Down Expand Up @@ -59,17 +63,21 @@ class App : NMBApplication() {
return true
}

/**
* 获取attr中的颜色
*/
fun getAttrColor(id: Int): Int {
return this.getAttrColor(id)
}
var appSkinTheme: SkinTheme = SkinTheme.NULL
set(value) {
field = value
skinTheme=value
GlobalScope.launch {
putString("theme", value.name)
}
}

fun initTheme() {
private fun initTheme() {
GlobalScope.launch {
val theme = getString("theme", SkinTheme.MATERIAL.name)
skinTheme = SkinTheme.valueOf(theme)
appSkinTheme = SkinTheme.valueOf(getString("theme", SkinTheme.NULL.name))
skinTheme=appSkinTheme
}
}


}
18 changes: 0 additions & 18 deletions app/src/main/java/com/yollpoll/nmb/Constant.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.yollpoll.nmb.model.repository

import android.util.Log
import com.yollpoll.base.TAG
import com.yollpoll.framework.net.http.RetrofitFactory
import com.yollpoll.nmb.TAG
import com.yollpoll.nmb.di.LauncherRetrofitFactory
import com.yollpoll.nmb.net.DIRECT_BASE_URL
import com.yollpoll.nmb.net.HttpService
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/yollpoll/nmb/view/activity/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import com.yollpoll.annotation.annotation.Route
import com.yollpoll.arch.annotation.PermissionAuto
import com.yollpoll.arch.annotation.ViewModel
import com.yollpoll.arch.log.LogUtils
import com.yollpoll.arch.message.MessageManager
import com.yollpoll.arch.message.liveeventbus.LiveEventBus
import com.yollpoll.arch.message.liveeventbus.ObserverWrapper
import com.yollpoll.base.*
import com.yollpoll.framework.dispatch.DispatchRequest
import com.yollpoll.framework.dispatch.OnBackListener
Expand All @@ -55,6 +58,7 @@ import com.yollpoll.nmb.net.realCover
import com.yollpoll.nmb.router.DispatchClient
import com.yollpoll.nmb.router.ROUTE_HOME
import com.yollpoll.nmb.view.widgets.*
import com.yollpoll.skin.SkinTheme
import com.yollpoll.utils.copyStr
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -258,6 +262,14 @@ class HomeActivity : NMBActivity<ActivityHomeBinding, HomeVm>() {
}
}
}
//切换主题刷新界面
override fun onThemeChanged(theme: SkinTheme) {
super.onThemeChanged(theme)
mDataBinding.refresh.isRefreshing=true
mDataBinding.rvContent.adapter=adapterThread
refreshThread()
}


/**
* 新建串
Expand Down Expand Up @@ -298,8 +310,9 @@ class HomeActivity : NMBActivity<ActivityHomeBinding, HomeVm>() {
ImageActivity.gotoImageActivity(context, 0, arrayListOf("封面"), arrayListOf(realCover))
}
}
fun gotoVisitHistory(){
lifecycleScope.launch{

fun gotoVisitHistory() {
lifecycleScope.launch {
gotoHistory(context)
}
}
Expand Down
60 changes: 48 additions & 12 deletions app/src/main/java/com/yollpoll/nmb/view/activity/SettingActivity.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package com.yollpoll.nmb.view.activity

import android.Manifest
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.databinding.Bindable
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.permissionx.guolindev.PermissionX
import com.yollpoll.annotation.annotation.Route
import com.yollpoll.arch.message.MessageManager
import com.yollpoll.base.NMBActivity
import com.yollpoll.framework.dispatch.DispatchRequest
import com.yollpoll.framework.extensions.*
import com.yollpoll.framework.fast.FastActivity
import com.yollpoll.framework.fast.FastViewModel
import com.yollpoll.framework.utils.getBoolean
import com.yollpoll.nmb.BR
import com.yollpoll.nmb.KEY_OPEN_CRASH_HANDLER
import com.yollpoll.nmb.MyCrashHandler
import com.yollpoll.nmb.R
import com.yollpoll.nmb.*
import com.yollpoll.nmb.databinding.ActivitySettingBinding
import com.yollpoll.nmb.router.DispatchClient
import com.yollpoll.nmb.router.ROUTE_SETTING
import com.yollpoll.utils.saveBitmapTpMediaStore
import com.yollpoll.skin.SkinTheme
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
Expand All @@ -43,18 +38,59 @@ class SettingActivity : NMBActivity<ActivitySettingBinding, SettingVm>() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initView()
}

private fun initView() {
initTitle(mDataBinding.headerTitle.toolbar, true)
mDataBinding.switchLog.setOnCheckedChangeListener { view, checked ->
vm.openCrashHandler(checked)
}
//uiTheme
var mThemeAdapter = ArrayAdapter<String>(
this,
R.layout.item_for_custom_spinner,
SkinTheme.values().map {
return@map when (it) {
SkinTheme.NULL -> ""
SkinTheme.MATERIAL -> "Material Design"
SkinTheme.OLD_SCHOOL -> "复古怀旧"
SkinTheme.OTHER -> "其他"
}
}
)
mDataBinding.spinner.adapter = mThemeAdapter
mDataBinding.spinner.setSelection(SkinTheme.values().indexOf(App.INSTANCE.appSkinTheme))
mDataBinding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
MessageManager.getInstance().sendMessage(ACTION_SELECT_THEME,SkinTheme.values()[position])
App.INSTANCE.appSkinTheme = SkinTheme.values()[position]
}

override fun onNothingSelected(parent: AdapterView<*>?) {

}

}
}

fun selectTheme(view: View) {

}
}

@HiltViewModel
class SettingVm @Inject constructor(val app: Application, val crashHandler: MyCrashHandler) :
FastViewModel(app) {

@Bindable
var openLog: Boolean = false

init {
initLog()
}
Expand All @@ -68,7 +104,7 @@ class SettingVm @Inject constructor(val app: Application, val crashHandler: MyCr

fun openCrashHandler(open: Boolean) {
viewModelScope.launch {
openLog=open
openLog = open
app.putBoolean(KEY_OPEN_CRASH_HANDLER, open)
}
}
Expand Down
35 changes: 31 additions & 4 deletions app/src/main/res/layout/activity_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorSurface"
android:background="?attr/colorSecondaryContainer"
android:orientation="vertical">

<include
Expand All @@ -26,9 +26,9 @@


<RelativeLayout
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/item_height">
android:layout_height="@dimen/item_height"
android:visibility="gone">

<TextView
android:layout_width="wrap_content"
Expand All @@ -41,8 +41,35 @@
android:id="@+id/switch_log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{viewModel.openLog}"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:checked="@{viewModel.openLog}" />
</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="@dimen/line"
android:background="?attr/colorOutline"
android:visibility="gone" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/item_height"
android:background="?attr/selectableItemBackground">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="@string/ui_theme" />

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</RelativeLayout>

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/item_for_custom_spinner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="right|center_vertical"
android:text="下拉列表项样式"
android:textSize="18dp"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="@dimen/item_height"
android:paddingRight="10dp"
android:singleLine="true"
android:ellipsize="marquee"/>
34 changes: 24 additions & 10 deletions app/src/main/res/layout/item_thread.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@
</data>


<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/ll_root"
android:layout_height="wrap_content">


<LinearLayout
android:id="@+id/ll_root"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
app:skin_enable="true"
app:skin_tag="item"

android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
Expand Down Expand Up @@ -101,14 +112,17 @@
android:textSize="@dimen/sec_content_sp" />
</RelativeLayout>

<View
android:id="@+id/line_bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/line"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:skin_enable="true"
app:skin_tag="item_line"
android:background="?attr/colorOutline" />

</LinearLayout>

<View
android:id="@+id/line_bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/line"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:skin_enable="true"
app:skin_tag="item_line"
android:background="?attr/colorOutline" />
</LinearLayout>
</layout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@
<string name="upgrade">检测新版本</string>
<string name="_import">导入</string>
<string name="No.">>>No.</string>
<string name="ui_theme">UI风格</string>
</resources>
21 changes: 19 additions & 2 deletions base/src/main/java/com/yollpoll/base/Constant.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package com.yollpoll.base
package com.yollpoll.nmb

const val TAG = "nmb"
const val KEY_FORUM_LIST = "forum_list"
const val USER_AGENT = "NMBClient-yollpoll-android"
const val APP_ID = "yollpoll"
const val KEY_CUR_COOKIE = "current_cookie"
const val KEY_SHOW_ANNOUNCEMENT = "show_announcement"
const val KEY_OPEN_CRASH_HANDLER = "open_crash_handler"

//eventbus msg
const val ACTION_TAG_NAME = "action_tag_name"
const val ACTION_TAG_ID = "action_tag_id"
const val ACTION_SELECT_THEME = "action_select_theme"

const val AUTHOR = "yollpoll"
const val GIT = "https://github.com/yollpoll/nmb"
const val EMAIL = "[email protected]"
const val FIR = "http://d.firim.pro/newnmb"//fir仓库

const val TAG = "nmb"
Loading

0 comments on commit 153e43f

Please sign in to comment.