Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加本地频道模块 #426

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MainActivity : FragmentActivity() {
private var channelReversal = false
private var channelNum = true
private var bootStartup = true
private var selectedProvince = "湖南"

private var versionName = ""

Expand Down Expand Up @@ -84,9 +85,10 @@ class MainActivity : FragmentActivity() {
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)
bootStartup = sharedPref.getBoolean(BOOT_STARTUP, bootStartup)
selectedProvince = sharedPref.getString(SELECTED_PROVINCE, selectedProvince)

versionName = getPackageInfo().versionName
settingFragment = SettingFragment(versionName, channelReversal, channelNum, bootStartup)
settingFragment = SettingFragment(versionName, channelReversal, channelNum, bootStartup, selectedProvince)
}

fun showInfoFragment(tvViewModel: TVViewModel) {
Expand Down Expand Up @@ -241,12 +243,20 @@ class MainActivity : FragmentActivity() {

fun saveBootStartup(bootStartup: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_NUM, channelNum)
putBoolean(BOOT_STARTUP, bootStartup)
apply()
}
this.bootStartup = bootStartup
}

fun saveSelectedProvince(selectedProvince: Sring) {
with(sharedPref.edit()) {
putString(SELECTED_PROVINCE, selectedProvince)
apply()
}
this.selectedProvince = selectedProvince
}

private fun showSetting() {
if (!mainFragment.isHidden) {
return
Expand Down Expand Up @@ -536,5 +546,6 @@ class MainActivity : FragmentActivity() {
private const val CHANNEL_REVERSAL = "channel_reversal"
private const val CHANNEL_NUM = "channel_num"
const val BOOT_STARTUP = "boot_startup"
const val SELECTED_PROVINCE = "selected_province"
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/lizongying/mytv/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.fragment.app.DialogFragment
import com.lizongying.mytv.databinding.DialogBinding

Expand All @@ -12,6 +14,7 @@ class SettingFragment(private val versionName: String,
private val channelReversal: Boolean,
private val channelNum: Boolean,
private val bootStartup: Boolean,
private val selectedProvince: String,
) :
DialogFragment() {

Expand Down Expand Up @@ -50,12 +53,37 @@ class SettingFragment(private val versionName: String,
(activity as MainActivity).saveBootStartup(isChecked)
}

// 设置省份Spinner
val provinceSpinner = binding.provinceSpinner
val provinces = arrayOf("北京", "上海", "天津", "重庆", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆", "台湾", "香港", "澳门")
val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_dropdown_item, provinces)
provinceSpinner.adapter = adapter

// 设置默认选择为“湖南”
val defaultIndex = provinces.indexOf("湖南")
if (defaultIndex != -1) {
provinceSpinner.setSelection(defaultIndex)
}

// 设置Spinner的监听器
provinceSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
selectedProvince = provinces[position]
}

override fun onNothingSelected(parent: AdapterView<*>?) {
selectedProvince = null
}
}

return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
// 取消Spinner的监听器(看是否需要吧)
binding.provinceSpinner.onItemSelectedListener = null
}

companion object {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/lizongying/mytv/TVList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,19 @@ object TVList {
//获取本地频道
ChannelUtils.getLocalChannel(context)
}
//获取定义的省份数据
val sp = context.getSharedPreferences("MainActivity", Context.MODE_PRIVATE)
//按频道分类
for (tv in tvList) {

// 如果是本地频道,则判断频道名称是否包含设置的省份,默认设置为湖南省,可遥控呼出菜单修改
if ( tv.channel == "本地频道" ) {
if ( !tv.title.contains(sp.getString(MainActivity.SELECTED_PROVINCE, "湖南")) ) {
Log.i("TVList", "跳过频道:${tv.title}")
continue;
}
}

val key = tv.channel
if (channelTVMap.containsKey(key)) {
val list = channelTVMap[key]!!
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/provinceSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/please_select_province"/>
</LinearLayout>
<ImageView
android:layout_width="300dp"
Expand Down
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 @@ -3,4 +3,5 @@
<string name="title_channel_reversal">换台反转</string>
<string name="title_channel_num">数字选台</string>
<string name="title_boot_startup">开机自启</string>
<string name="please_select_province">请选择您的省份</string>
</resources>