diff --git a/app/src/main/java/net/harimurti/tv/adapter/SourcesAdapter.kt b/app/src/main/java/net/harimurti/tv/adapter/SourcesAdapter.kt index cff440f..3a1e13b 100644 --- a/app/src/main/java/net/harimurti/tv/adapter/SourcesAdapter.kt +++ b/app/src/main/java/net/harimurti/tv/adapter/SourcesAdapter.kt @@ -4,27 +4,28 @@ import android.content.ClipData import android.content.ClipboardManager import android.content.Context import android.content.Context.CLIPBOARD_SERVICE -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.Toast +import androidx.appcompat.widget.PopupMenu import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.RecyclerView import net.harimurti.tv.BR import net.harimurti.tv.R import net.harimurti.tv.databinding.ItemSourceBinding import net.harimurti.tv.dialog.SettingDialog +import net.harimurti.tv.extra.Preferences import net.harimurti.tv.model.Source interface SourceClickListener { fun onClicked(position: Int) fun onCheckChanged(view: View, checked: Boolean, position: Int) - fun onlongClicked(view: View, source: Source?): Boolean + fun onlongClicked(view: View, source: Source?, position: Int): Boolean } class SourcesAdapter(private val sources: ArrayList?): RecyclerView.Adapter(), SourceClickListener { lateinit var context: Context + val preferences = Preferences() class ViewHolder(var itemBinding: ItemSourceBinding) : RecyclerView.ViewHolder(itemBinding.root) { @@ -46,8 +47,9 @@ class SourcesAdapter(private val sources: ArrayList?): viewHolder.bind(source) viewHolder.itemBinding.position = position viewHolder.itemBinding.clickListener = this - if (source?.path?.equals(context.getString(R.string.json_playlist)) == true) { - viewHolder.itemBinding.swSource.setText(R.string.default_playlist) + if (position == 0) { + val name = String.format(context.getString(R.string.default_playlist), preferences.countryId.uppercase()) + viewHolder.itemBinding.swSource.text = name viewHolder.itemBinding.btnRemove.visibility = View.GONE } } @@ -65,22 +67,42 @@ class SourcesAdapter(private val sources: ArrayList?): sources?.get(0)?.active = true notifyItemChanged(0) } - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } - override fun onlongClicked(view: View, source: Source?): Boolean { - val clipboard = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager - val clipData = ClipData.newPlainText("url", source?.path) - - clipboard.setPrimaryClip(clipData) - Toast.makeText(context, R.string.link_copied_to_clipboard, Toast.LENGTH_SHORT).show() + override fun onlongClicked(view: View, source: Source?, position: Int): Boolean { + if (position == 0) { + PopupMenu(context, view, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0).apply { + inflate(R.menu.country_list) + setOnMenuItemClickListener { m: MenuItem -> + val mode = when(m.itemId) { + R.id.japan -> "jp" + R.id.malaysia -> "my" + R.id.singapore -> "sg" + R.id.southkorea -> "kr" + else -> "id" + } + preferences.countryId = mode + SettingDialog.isSourcesChanged = true + notifyItemChanged(0) + true + } + show() + } + } + else { + val clipData = ClipData.newPlainText("url", source?.path) + val clipboard = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager + clipboard.setPrimaryClip(clipData) + Toast.makeText(context, R.string.link_copied_to_clipboard, Toast.LENGTH_SHORT).show() + } return true } override fun onCheckChanged(view: View, checked: Boolean, position: Int) { sources?.get(position)?.active = checked - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } override fun getItemCount(): Int { @@ -95,6 +117,6 @@ class SourcesAdapter(private val sources: ArrayList?): val position = itemCount sources?.add(source) notifyItemInserted(position) - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } } \ No newline at end of file diff --git a/app/src/main/java/net/harimurti/tv/dialog/SettingAppFragment.kt b/app/src/main/java/net/harimurti/tv/dialog/SettingAppFragment.kt index 1c56fdd..6221aa5 100644 --- a/app/src/main/java/net/harimurti/tv/dialog/SettingAppFragment.kt +++ b/app/src/main/java/net/harimurti/tv/dialog/SettingAppFragment.kt @@ -39,7 +39,7 @@ class SettingAppFragment : Fragment() { isChecked = sortFavorite setOnClickListener { sortFavorite = isChecked - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } } @@ -47,7 +47,7 @@ class SettingAppFragment : Fragment() { isChecked = sortCategory setOnClickListener { sortCategory = isChecked - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } } @@ -55,7 +55,7 @@ class SettingAppFragment : Fragment() { isChecked = sortChannel setOnClickListener { sortChannel = isChecked - SettingDialog.isChanged = true + SettingDialog.isSourcesChanged = true } } diff --git a/app/src/main/java/net/harimurti/tv/dialog/SettingDialog.kt b/app/src/main/java/net/harimurti/tv/dialog/SettingDialog.kt index 5213c7b..c1e0db3 100644 --- a/app/src/main/java/net/harimurti/tv/dialog/SettingDialog.kt +++ b/app/src/main/java/net/harimurti/tv/dialog/SettingDialog.kt @@ -17,11 +17,14 @@ import net.harimurti.tv.databinding.SettingDialogBinding import net.harimurti.tv.extra.Preferences class SettingDialog : DialogFragment() { + val preferences = Preferences() private val tabFragment = arrayOf(SettingSourcesFragment(), SettingAppFragment(), SettingAboutFragment()) private val tabTitle = arrayOf(R.string.tab_sources, R.string.tab_app, R.string.tab_about) + private var revertCountryId = "" + private var isCancelled = true companion object { - var isChanged = false + var isSourcesChanged = false } @Suppress("DEPRECATION") @@ -50,10 +53,9 @@ class SettingDialog : DialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val binding = SettingDialogBinding.inflate(inflater, container, false) - val preferences = Preferences() // init - isChanged = false + isSourcesChanged = false SettingAppFragment.launchAtBoot = preferences.launchAtBoot SettingAppFragment.playLastWatched = preferences.playLastWatched SettingAppFragment.sortFavorite = preferences.sortFavorite @@ -62,6 +64,7 @@ class SettingDialog : DialogFragment() { SettingAppFragment.optimizePrebuffer = preferences.optimizePrebuffer SettingAppFragment.reverseNavigation = preferences.reverseNavigation SettingSourcesFragment.sources = preferences.sources + revertCountryId = preferences.countryId // view pager binding.settingViewPager.adapter = FragmentAdapter(childFragmentManager) @@ -74,14 +77,7 @@ class SettingDialog : DialogFragment() { // button ok binding.settingOkButton.apply { setOnClickListener { - // playlist sources - val sources = SettingSourcesFragment.sources - if (sources?.filter { s -> s.active }?.size == 0) { - sources[0].active = true - Toast.makeText(context, R.string.warning_none_source_active, Toast.LENGTH_SHORT).show() - } - preferences.sources = sources - if (isChanged) sendUpdatePlaylist(rootView.context) + isCancelled = false // setting app preferences.launchAtBoot = SettingAppFragment.launchAtBoot preferences.playLastWatched = SettingAppFragment.playLastWatched @@ -90,6 +86,13 @@ class SettingDialog : DialogFragment() { preferences.sortChannel = SettingAppFragment.sortChannel preferences.optimizePrebuffer = SettingAppFragment.optimizePrebuffer preferences.reverseNavigation = SettingAppFragment.reverseNavigation + // playlist sources + val sources = SettingSourcesFragment.sources + if (sources?.filter { s -> s.active }?.size == 0) { + sources[0].active = true + Toast.makeText(context, R.string.warning_none_source_active, Toast.LENGTH_SHORT).show() + } + preferences.sources = sources dismiss() } } @@ -97,6 +100,12 @@ class SettingDialog : DialogFragment() { return binding.root } + override fun onDestroyView() { + super.onDestroyView() + if (isCancelled) preferences.countryId = revertCountryId + else if (isSourcesChanged) sendUpdatePlaylist(requireContext()) + } + private fun sendUpdatePlaylist(context: Context) { LocalBroadcastManager.getInstance(context).sendBroadcast( Intent(MainActivity.MAIN_CALLBACK) diff --git a/app/src/main/java/net/harimurti/tv/extra/Preferences.kt b/app/src/main/java/net/harimurti/tv/extra/Preferences.kt index 4002630..fec5fa8 100644 --- a/app/src/main/java/net/harimurti/tv/extra/Preferences.kt +++ b/app/src/main/java/net/harimurti/tv/extra/Preferences.kt @@ -32,6 +32,7 @@ class Preferences { private const val CONTRIBUTORS = "CONTRIBUTORS" private const val RESIZE_MODE = "RESIZE_MODE" private const val SOURCES_PLAYLIST = "SOURCES_PLAYLIST" + private const val COUNTRY_ID = "COUNTRY_ID" } var isFirstTime: Boolean @@ -115,23 +116,31 @@ class Preferences { editor.apply() } + var countryId: String + get() = preferences.getString(COUNTRY_ID, "id").toString() + set(value) { + editor = preferences.edit() + editor.putString(COUNTRY_ID, value) + editor.apply() + } + var sources: ArrayList? get() { val result = ArrayList() val default = Source().apply { - path = context.getString(R.string.json_playlist) + path = String.format(context.getString(R.string.iptv_playlist), countryId) active = true } try { val json = preferences.getString(SOURCES_PLAYLIST, "").toString() - if (json.isNotBlank()) { - val list = Gson().fromJson(json, Array::class.java) - if (list.firstOrNull()?.path != default.path) result.add(default) - list.forEach { - if (it.path.isLinkUrl()) result.add(it) - else if (it.path.isPathExist()) result.add(it) - } - } else throw Exception("no playlist sources in preference") + if (json.isBlank()) throw Exception("no playlist sources in preference") + val list = Gson().fromJson(json, Array::class.java) + if (list == null || list.isEmpty()) throw Exception("cannot parse sources?") + list.first().path = default.path + list.forEach { + if (it.path.isLinkUrl()) result.add(it) + else if (it.path.isPathExist()) result.add(it) + } } catch (e: Exception) { e.printStackTrace() } diff --git a/app/src/main/res/layout/item_source.xml b/app/src/main/res/layout/item_source.xml index 556b781..41c650b 100644 --- a/app/src/main/res/layout/item_source.xml +++ b/app/src/main/res/layout/item_source.xml @@ -27,7 +27,7 @@ android:text="@{modelSource.path}" android:checked="@{modelSource.active}" android:onCheckedChanged="@{(view, checked) -> clickListener.onCheckChanged(view, checked, position)}" - android:onLongClick="@{(view) -> clickListener.onlongClicked(view, modelSource)}"/> + android:onLongClick="@{(view) -> clickListener.onlongClicked(view, modelSource, position)}"/> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 51f66ff..17d04f1 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -4,7 +4,7 @@ Gratis - OpenSource - Tanpa Iklan Ringan - Simpel - Minimalis Terimakasih Kepada : - dan orang yg mendukung proyek ini ๐Ÿ˜˜ + iptv-org utk playlist dan orangยฒ yg mendukung proyek ini ๐Ÿ˜˜ Coba Ulang Coba Ulang (%d) Keluar diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 15c0635..6685f41 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,7 +4,7 @@ https://github.com/hariimurti/NontonTV https://t.me/paijemdev - https://github.com/hariimurti/NontonTV/raw/playlist/NontonTV.json + https://iptv-org.github.io/iptv/countries/%s.m3u https://github.com/hariimurti/NontonTV/raw/master/json/release.json https://github.com/hariimurti/NontonTV/releases/download/%s/net.harimurti.tv_%s_b%d.apk https://api.github.com/repos/hariimurti/NontonTV/contributors @@ -15,7 +15,7 @@ Free - OpenSource - No Ads, Light - Simple - Minimalist Thanks To : - and people who support this project ๐Ÿ˜˜ + iptv-org for playlist and people who support this project ๐Ÿ˜˜ Retry Retry (%d) Close @@ -84,7 +84,7 @@ Link URL Hidden Select File Json - NontonTV (Default) + IPTV-ORG ( %s ) No playlists that can be displayed. Check your settings! checking urlโ€ฆ Link Error! @@ -93,4 +93,9 @@ More Information : Website Telegram + Indonesia + Japan + Malaysia + Singapore + South Korea