Skip to content

Commit

Permalink
number selection
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Jan 18, 2024
1 parent e1ee909 commit ba95ce5
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 24 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

## 更新日志

### v1.4.5(高版本专用)

* 数字选台配置

### v1.4.4(通用版)

* 优化图标显示
* 增加换台反转

### v1.3.3

* 部分错误会提示用户
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyTV">
Expand Down
74 changes: 74 additions & 0 deletions app/src/main/java/com/lizongying/mytv/ChannelFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.lizongying.mytv

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.lizongying.mytv.databinding.ChannelBinding
import com.lizongying.mytv.models.TVViewModel

class ChannelFragment : Fragment() {
private var _binding: ChannelBinding? = null
private val binding get() = _binding!!

private val handler = Handler()
private val delay: Long = 3000

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = ChannelBinding.inflate(inflater, container, false)
(activity as MainActivity).fragmentReady()
return binding.root
}

fun show(tvViewModel: TVViewModel) {
binding.channelContent.text = tvViewModel.id.value.toString()
handler.removeCallbacks(removeRunnable)
view?.visibility = View.VISIBLE
handler.postDelayed(removeRunnable, delay)
}

fun show(channel: String) {
if (binding.channelContent.text == "") {
binding.channelContent.text = channel
handler.removeCallbacks(removeRunnable)
view?.visibility = View.VISIBLE
handler.postDelayed(removeRunnable, delay)
} else {
val ch = "${binding.channelContent.text}$channel".toInt()
Log.i(TAG, "channel $ch")
(activity as MainActivity).play(ch)
binding.channelContent.text = ""
view?.visibility = View.GONE
}
}

override fun onResume() {
super.onResume()
handler.postDelayed(removeRunnable, delay)
}

override fun onPause() {
super.onPause()
handler.removeCallbacks(removeRunnable)
}

private val removeRunnable = Runnable {
binding.channelContent.text = ""
view?.visibility = View.GONE
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
private const val TAG = "ChannelFragment"
}
}
116 changes: 110 additions & 6 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ class MainActivity : FragmentActivity() {
var playerFragment = PlayerFragment()
private val mainFragment = MainFragment()
private val infoFragment = InfoFragment()
private val channelFragment = ChannelFragment()

private var doubleBackToExitPressedOnce = false

private lateinit var gestureDetector: GestureDetector

private val handler = Handler()
private val delay: Long = 4000
private val delayHideHelp: Long = 10000

private lateinit var sharedPref: SharedPreferences
private var channelReversal = false
private var channelNum = true

private var versionName = ""
private lateinit var dialogFragment: MyDialogFragment

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -50,26 +56,39 @@ class MainActivity : FragmentActivity() {
supportFragmentManager.beginTransaction()
.add(R.id.main_browse_fragment, playerFragment)
.add(R.id.main_browse_fragment, infoFragment)
.add(R.id.main_browse_fragment, channelFragment)
.add(R.id.main_browse_fragment, mainFragment)
.hide(infoFragment)
.hide(channelFragment)
.commit()
mainFragment.view?.requestFocus()
}
gestureDetector = GestureDetector(this, GestureListener())

sharedPref = getPreferences(Context.MODE_PRIVATE)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, false)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)

versionName = getPackageInfo().versionName
dialogFragment = MyDialogFragment(versionName, channelReversal, channelNum)
}

fun showInfoFragment(tvViewModel: TVViewModel) {
infoFragment.show(tvViewModel)
if (channelNum) {
channelFragment.show(tvViewModel)
}
}

fun play(tvViewModel: TVViewModel) {
playerFragment.play(tvViewModel)
mainFragment.view?.requestFocus()
}

fun play(itemPosition: Int) {
mainFragment.play(itemPosition)
}

fun prev() {
mainFragment.prev()
}
Expand All @@ -79,11 +98,11 @@ class MainActivity : FragmentActivity() {
}

private fun prevSource() {
mainFragment.prevSource()
// mainFragment.prevSource()
}

private fun nextSource() {
mainFragment.nextSource()
// mainFragment.nextSource()
}

fun switchMainFragment() {
Expand Down Expand Up @@ -182,14 +201,34 @@ class MainActivity : FragmentActivity() {
this.channelReversal = channelReversal
}

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

private fun showHelp() {
if (!mainFragment.isHidden) {
return
}

val versionName = getPackageInfo().versionName
val dialogFragment = MyDialogFragment(versionName, channelReversal)
dialogFragment.show(supportFragmentManager, "settings_dialog")
Log.i(TAG, "dialogFragment ${dialogFragment.isVisible}")
if (!dialogFragment.isVisible) {
dialogFragment.show(supportFragmentManager, "settings_dialog")
handler.removeCallbacks(hideHelp)
handler.postDelayed(hideHelp, delayHideHelp)
} else {
handler.removeCallbacks(hideHelp)
dialogFragment.dismiss()
}
}

private val hideHelp = Runnable {
if (dialogFragment.isVisible) {
dialogFragment.dismiss()
}
}

private fun channelUp() {
Expand Down Expand Up @@ -244,8 +283,72 @@ class MainActivity : FragmentActivity() {
}, 2000)
}

private fun showChannel(channel: String) {
if (!mainFragment.isHidden) {
return
}

if (dialogFragment.isVisible) {
return
}

if (channelNum) {
channelFragment.show(channel)
}
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_0 -> {
showChannel("0")
return true
}

KeyEvent.KEYCODE_1 -> {
showChannel("1")
return true
}

KeyEvent.KEYCODE_2 -> {
showChannel("2")
return true
}

KeyEvent.KEYCODE_3 -> {
showChannel("3")
return true
}

KeyEvent.KEYCODE_4 -> {
showChannel("4")
return true
}

KeyEvent.KEYCODE_5 -> {
showChannel("5")
return true
}

KeyEvent.KEYCODE_6 -> {
showChannel("6")
return true
}

KeyEvent.KEYCODE_7 -> {
showChannel("7")
return true
}

KeyEvent.KEYCODE_8 -> {
showChannel("8")
return true
}

KeyEvent.KEYCODE_9 -> {
showChannel("9")
return true
}

KeyEvent.KEYCODE_ESCAPE -> {
back()
return true
Expand Down Expand Up @@ -388,5 +491,6 @@ class MainActivity : FragmentActivity() {
companion object {
private const val TAG = "MainActivity"
private const val CHANNEL_REVERSAL = "channel_reversal"
private const val CHANNEL_NUM = "channel_num"
}
}
24 changes: 14 additions & 10 deletions app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class MainFragment : BrowseSupportFragment() {
fun fragmentReady() {
ready++
Log.i(TAG, "ready $ready")
if (ready == 3) {
if (ready == 4) {
// request.fetchPage()
val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
tvViewModel?.changed()
Expand All @@ -225,16 +225,24 @@ class MainFragment : BrowseSupportFragment() {
}
}

fun play(itemPosition: Int) {
view?.post {
if (itemPosition < tvListViewModel.size()) {
this.itemPosition = itemPosition
tvListViewModel.setItemPosition(itemPosition)
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}
}
}

fun prev() {
view?.post {
itemPosition--
if (itemPosition == -1) {
itemPosition = tvListViewModel.size() - 1
}
tvListViewModel.setItemPosition(itemPosition)

val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
tvViewModel?.changed()
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}
}

Expand All @@ -245,9 +253,7 @@ class MainFragment : BrowseSupportFragment() {
itemPosition = 0
}
tvListViewModel.setItemPosition(itemPosition)

val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
tvViewModel?.changed()
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}
}

Expand Down Expand Up @@ -297,9 +303,7 @@ class MainFragment : BrowseSupportFragment() {
if (itemPosition != item.id.value!!) {
itemPosition = item.id.value!!
tvListViewModel.setItemPosition(itemPosition)

val tvViewModel = tvListViewModel.getTVViewModel(itemPosition)
tvViewModel?.changed()
tvListViewModel.getTVViewModel(itemPosition)?.changed()
}
(activity as? MainActivity)?.switchMainFragment()
}
Expand Down
Loading

0 comments on commit ba95ce5

Please sign in to comment.