diff --git a/README.md b/README.md index cec0d55..0e13dbb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ ## 更新日志 +### v1.0.2 + +* 改变部分样式 + ### v1.0.1 * 支持返回键退出 diff --git a/app/src/main/java/com/lizongying/mytv0/CategoryAdapter.kt b/app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt similarity index 85% rename from app/src/main/java/com/lizongying/mytv0/CategoryAdapter.kt rename to app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt index 253e550..738db6c 100644 --- a/app/src/main/java/com/lizongying/mytv0/CategoryAdapter.kt +++ b/app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt @@ -8,17 +8,17 @@ import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.recyclerview.widget.RecyclerView -import com.lizongying.mytv0.databinding.CategoryItemBinding -import com.lizongying.mytv0.models.TVCategoryModel +import com.lizongying.mytv0.databinding.GroupItemBinding +import com.lizongying.mytv0.models.TVGroupModel import com.lizongying.mytv0.models.TVListModel -class CategoryAdapter( +class GroupAdapter( private val context: Context, private val recyclerView: RecyclerView, - private var tvCategoryModel: TVCategoryModel, + private var tvgroupModel: TVGroupModel, ) : - RecyclerView.Adapter() { + RecyclerView.Adapter() { private var listener: ItemListener? = null private var focused: View? = null @@ -29,7 +29,7 @@ class CategoryAdapter( override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val inflater = LayoutInflater.from(context) - val binding = CategoryItemBinding.inflate(inflater, parent, false) + val binding = GroupItemBinding.inflate(inflater, parent, false) binding.root.isFocusable = true binding.root.isFocusableInTouchMode = true return ViewHolder(context, binding) @@ -51,7 +51,7 @@ class CategoryAdapter( } override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { - val tvListModel = tvCategoryModel.getTVListModel(position)!! + val tvListModel = tvgroupModel.getTVListModel(position)!! val view = viewHolder.itemView view.tag = position @@ -67,8 +67,8 @@ class CategoryAdapter( viewHolder.focus(true) focused = view if (visiable) { - if (position != tvCategoryModel.position.value) { - tvCategoryModel.setPosition(position) + if (position != tvgroupModel.position.value) { + tvgroupModel.setPosition(position) } } else { visiable = true @@ -102,9 +102,9 @@ class CategoryAdapter( viewHolder.bind(tvListModel.getName()) } - override fun getItemCount() = tvCategoryModel.size() + override fun getItemCount() = tvgroupModel.size() - class ViewHolder(private val context: Context, private val binding: CategoryItemBinding) : + class ViewHolder(private val context: Context, private val binding: GroupItemBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(text: String) { binding.textView.text = text @@ -126,7 +126,7 @@ class CategoryAdapter( fun toPosition(position: Int) { recyclerView.post { - Log.i(TAG, "category smoothScrollToPosition $position") + Log.i(TAG, "group smoothScrollToPosition $position") recyclerView.scrollToPosition(position) recyclerView.getChildAt(position)?.isSelected recyclerView.getChildAt(position)?.requestFocus() diff --git a/app/src/main/java/com/lizongying/mytv0/MainActivity.kt b/app/src/main/java/com/lizongying/mytv0/MainActivity.kt index 34fefa0..175eecc 100644 --- a/app/src/main/java/com/lizongying/mytv0/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv0/MainActivity.kt @@ -7,7 +7,9 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log +import android.view.GestureDetector import android.view.KeyEvent +import android.view.MotionEvent import android.view.View import android.view.WindowManager import android.widget.Toast @@ -31,6 +33,8 @@ class MainActivity : FragmentActivity() { private var position = 0 + private lateinit var gestureDetector: GestureDetector + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -60,6 +64,8 @@ class MainActivity : FragmentActivity() { .commitNow() } + gestureDetector = GestureDetector(this, GestureListener()) + TVList.listModel.forEach { tvModel -> tvModel.errInfo.observe(this) { _ -> if (tvModel.errInfo.value != null @@ -87,6 +93,40 @@ class MainActivity : FragmentActivity() { TVList.setPosition(SP.position) } + override fun onTouchEvent(event: MotionEvent?): Boolean { + if (event != null) { + gestureDetector.onTouchEvent(event) + } + return super.onTouchEvent(event) + } + + private inner class GestureListener : GestureDetector.SimpleOnGestureListener() { + + override fun onSingleTapConfirmed(e: MotionEvent): Boolean { + showMenu() + return true + } + + override fun onFling( + e1: MotionEvent?, + e2: MotionEvent, + velocityX: Float, + velocityY: Float + ): Boolean { + if (velocityY > 0) { + if (menuFragment.isHidden && settingFragment.isHidden) { + prev() + } + } + if (velocityY < 0) { + if (menuFragment.isHidden && settingFragment.isHidden) { + next() + } + } + return super.onFling(e1, e2, velocityX, velocityY) + } + } + fun play(position: Int) { if (position > -1 && position < TVList.size()) { TVList.setPosition(position) @@ -129,7 +169,7 @@ class MainActivity : FragmentActivity() { private val hideSetting = Runnable { if (!settingFragment.isHidden) { - supportFragmentManager.beginTransaction().hide(settingFragment).commit() + supportFragmentManager.beginTransaction().hide(settingFragment).commitNow() } } diff --git a/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt b/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt index 37b85aa..ba1124b 100644 --- a/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt @@ -17,11 +17,11 @@ import com.lizongying.mytv0.models.TVList import com.lizongying.mytv0.models.TVListModel import com.lizongying.mytv0.models.TVModel -class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemListener { +class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemListener { private var _binding: MenuBinding? = null private val binding get() = _binding!! - private lateinit var categoryAdapter: CategoryAdapter + private lateinit var groupAdapter: GroupAdapter private lateinit var listAdapter: ListAdapter override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -35,20 +35,20 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL ): View { _binding = MenuBinding.inflate(inflater, container, false) - categoryAdapter = CategoryAdapter( + groupAdapter = GroupAdapter( context!!, - binding.category, - TVList.categoryModel, + binding.group, + TVList.groupModel, ) - binding.category.adapter = categoryAdapter - binding.category.layoutManager = + binding.group.adapter = groupAdapter + binding.group.layoutManager = LinearLayoutManager(context) - categoryAdapter.setItemListener(this) + groupAdapter.setItemListener(this) listAdapter = ListAdapter( context!!, binding.list, - TVList.categoryModel.getTVListModel(TVList.categoryModel.position.value!!)!!, + TVList.groupModel.getTVListModel(TVList.groupModel.position.value!!)!!, ) binding.list.adapter = listAdapter binding.list.layoutManager = @@ -86,8 +86,8 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL Toast.makeText(context, "暂无频道", Toast.LENGTH_LONG).show() return true } - binding.category.visibility = GONE - categoryAdapter.focusable(false) + binding.group.visibility = GONE + groupAdapter.focusable(false) listAdapter.focusable(true) listAdapter.toPosition(listAdapter.tvListModel.position.value!!) return true @@ -104,12 +104,12 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL override fun onKey(listAdapter: ListAdapter, keyCode: Int): Boolean { when (keyCode) { KeyEvent.KEYCODE_DPAD_LEFT -> { - binding.category.visibility = VISIBLE - categoryAdapter.focusable(true) + binding.group.visibility = VISIBLE + groupAdapter.focusable(true) listAdapter.focusable(false) listAdapter.clear() - Log.i(TAG, "category toPosition on left") - categoryAdapter.toPosition(TVList.categoryModel.position.value!!) + Log.i(TAG, "group toPosition on left") + groupAdapter.toPosition(TVList.groupModel.position.value!!) return true } } @@ -120,25 +120,25 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL super.onHiddenChanged(hidden) if (!hidden) { if (binding.list.isVisible) { -// if (binding.category.isVisible) { -// categoryAdapter.focusable(true) +// if (binding.group.isVisible) { +// groupAdapter.focusable(true) // listAdapter.focusable(false) // } else { -// categoryAdapter.focusable(false) +// groupAdapter.focusable(false) // listAdapter.focusable(true) // } Log.i(TAG, "list on show toPosition ${listAdapter.tvListModel.position.value!!}") listAdapter.toPosition(listAdapter.tvListModel.position.value!!) } - if (binding.category.isVisible) { -// categoryAdapter.focusable(true) + if (binding.group.isVisible) { +// groupAdapter.focusable(true) // listAdapter.focusable(false) - Log.i(TAG, "category on show toPosition ${TVList.categoryModel.position.value!!}") - categoryAdapter.toPosition(TVList.categoryModel.position.value!!) + Log.i(TAG, "group on show toPosition ${TVList.groupModel.position.value!!}") + groupAdapter.toPosition(TVList.groupModel.position.value!!) } } else { view?.post { - categoryAdapter.visiable = false + groupAdapter.visiable = false listAdapter.visiable = false } } @@ -146,7 +146,7 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL override fun onResume() { super.onResume() -// categoryAdapter.toPosition(TVList.categoryModel.position.value!!) +// groupAdapter.toPosition(TVList.groupModel.position.value!!) } override fun onDestroyView() { diff --git a/app/src/main/java/com/lizongying/mytv0/SP.kt b/app/src/main/java/com/lizongying/mytv0/SP.kt index 856e2c5..372b80a 100644 --- a/app/src/main/java/com/lizongying/mytv0/SP.kt +++ b/app/src/main/java/com/lizongying/mytv0/SP.kt @@ -17,7 +17,7 @@ object SP { // Position in list of the selected channel item private const val KEY_POSITION = "position" - private const val KEY_POSITION_CATEGORY = "position_category" + private const val KEY_POSITION_CATEGORY = "position_group" private const val KEY_POSITION_SUB = "position_sub" diff --git a/app/src/main/java/com/lizongying/mytv0/models/TV.kt b/app/src/main/java/com/lizongying/mytv0/models/TV.kt index ed831c9..0777767 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TV.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TV.kt @@ -4,27 +4,28 @@ import java.io.Serializable data class TV( var id: Int = 0, - var programId: String = "", + var name: String = "", var title: String = "", var description: String? = null, var logo: String = "", var image: String? = null, - var videoUrl: List, + var uris: List, var headers: Map? = null, - var category: String = "", + var group: String = "", var child: List, ) : Serializable { override fun toString(): String { return "TV{" + "id=" + id + - ", programId='" + programId + '\'' + + ", name='" + name + '\'' + ", title='" + title + '\'' + ", description='" + description + '\'' + ", logo='" + logo + '\'' + ", image='" + image + '\'' + - ", videoUrl='" + videoUrl + '\'' + - ", category='" + category + '\'' + + ", uris='" + uris + '\'' + + ", headers='" + headers + '\'' + + ", group='" + group + '\'' + '}' } } \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVCategoryModel.kt b/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt similarity index 55% rename from app/src/main/java/com/lizongying/mytv0/models/TVCategoryModel.kt rename to app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt index 9796405..1010c16 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVCategoryModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt @@ -5,10 +5,10 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.lizongying.mytv0.SP -class TVCategoryModel : ViewModel() { - private val _tvCategoryModel = MutableLiveData>() - val tvCategoryModel: LiveData> - get() = _tvCategoryModel +class TVGroupModel : ViewModel() { + private val _tvGroupModel = MutableLiveData>() + val tvGroupModel: LiveData> + get() = _tvGroupModel private val _position = MutableLiveData() val position: LiveData @@ -19,31 +19,30 @@ class TVCategoryModel : ViewModel() { } fun setTVListModelList(tvTVListModelList: MutableList) { - _tvCategoryModel.value = tvTVListModelList + _tvGroupModel.value = tvTVListModelList } fun addTVListModel(tvListModel: TVListModel) { - if (_tvCategoryModel.value == null) { - _tvCategoryModel.value = mutableListOf(tvListModel) + if (_tvGroupModel.value == null) { + _tvGroupModel.value = mutableListOf(tvListModel) return } - _tvCategoryModel.value?.add(tvListModel) + _tvGroupModel.value?.add(tvListModel) } fun getTVListModel(idx: Int): TVListModel? { - return _tvCategoryModel.value?.get(idx) + return _tvGroupModel.value?.get(idx) } init { _position.value = SP.positionCategory - _position.value = 2 } fun size(): Int { - if (_tvCategoryModel.value == null) { + if (_tvGroupModel.value == null) { return 0 } - return _tvCategoryModel.value!!.size + return _tvGroupModel.value!!.size } } \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVList.kt b/app/src/main/java/com/lizongying/mytv0/models/TVList.kt index dad8b5f..986017c 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVList.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVList.kt @@ -12,12 +12,13 @@ import kotlinx.coroutines.withContext import java.io.File object TVList { + private const val TAG = "TVList" private const val FILE_NAME = "channels.json" private lateinit var appDirectory: File private lateinit var serverUrl: String private lateinit var list: List lateinit var listModel: List - val categoryModel = TVCategoryModel() + val groupModel = TVGroupModel() private val _position = MutableLiveData() val position: LiveData @@ -29,8 +30,10 @@ object TVList { serverUrl = context.resources.getString(R.string.server_url) val file = File(appDirectory, FILE_NAME) val str = if (file.exists()) { + Log.i(TAG, "local file") file.readText() } else { + Log.i(TAG, "read resource") context.resources.openRawResource(R.raw.channels).bufferedReader() .use { it.readText() } } @@ -82,22 +85,22 @@ object TVList { TVModel(tv) } - val category: MutableList = mutableListOf() + val group: MutableList = mutableListOf() var tvListModel = TVListModel("我的收藏") - category.add(tvListModel) + group.add(tvListModel) tvListModel = TVListModel("全部频道") tvListModel.setTVListModel(listModel) - category.add(tvListModel) + group.add(tvListModel) val map: MutableMap> = mutableMapOf() for ((id, v) in list.withIndex()) { - if (v.category !in map) { - map[v.category] = mutableListOf() + if (v.group !in map) { + map[v.group] = mutableListOf() } v.id = id - map[v.category]?.add(TVModel(v)) + map[v.group]?.add(TVModel(v)) } for ((k, v) in map) { @@ -105,10 +108,10 @@ object TVList { for (v1 in v) { tvListModel.addTVModel(v1) } - category.add(tvListModel) + group.add(tvListModel) } - categoryModel.setTVListModelList(category) + groupModel.setTVListModelList(group) } fun getTVModel(idx: Int): TVModel { diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt b/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt index d777d86..5df3f18 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt @@ -1,6 +1,5 @@ package com.lizongying.mytv0.models -import android.net.Uri import androidx.annotation.OptIn import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData @@ -40,7 +39,15 @@ class TVModel(var tv: TV) : ViewModel() { } fun getVideoUrl(): String? { - return _videoIndex.value?.let { tv.videoUrl[it] } + if (_videoIndex.value == null || tv.uris.isEmpty()) { + return null + } + + if (_videoIndex.value!! >= tv.uris.size) { + return null + } + + return tv.uris[_videoIndex.value!!] } private val _ready = MutableLiveData() @@ -72,7 +79,7 @@ class TVModel(var tv: TV) : ViewModel() { tv.headers?.let { httpDataSource.setDefaultRequestProperties(it) } return HlsMediaSource.Factory(httpDataSource).createMediaSource( - MediaItem.fromUri(_videoUrl.value!!) + MediaItem.fromUri(_videoUrl.value!!) ) } diff --git a/app/src/main/res/layout/category_item.xml b/app/src/main/res/layout/group_item.xml similarity index 95% rename from app/src/main/res/layout/category_item.xml rename to app/src/main/res/layout/group_item.xml index b42540d..d7a31f2 100644 --- a/app/src/main/res/layout/category_item.xml +++ b/app/src/main/res/layout/group_item.xml @@ -1,6 +1,6 @@ diff --git a/app/src/main/res/layout/menu.xml b/app/src/main/res/layout/menu.xml index 10dd405..77203a8 100644 --- a/app/src/main/res/layout/menu.xml +++ b/app/src/main/res/layout/menu.xml @@ -7,7 +7,7 @@ android:orientation="horizontal" >