diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json index e288a7878..d8cfe2cf4 100644 --- a/app/release/output-metadata.json +++ b/app/release/output-metadata.json @@ -15,19 +15,6 @@ "versionName": "0.5.1", "outputFile": "app-universal-release.apk" }, - { - "type": "ONE_OF_MANY", - "filters": [ - { - "filterType": "ABI", - "value": "x86_64" - } - ], - "attributes": [], - "versionCode": 18, - "versionName": "0.5.1", - "outputFile": "app-x86_64-release.apk" - }, { "type": "ONE_OF_MANY", "filters": [ @@ -41,19 +28,6 @@ "versionName": "0.5.1", "outputFile": "app-armeabi-v7a-release.apk" }, - { - "type": "ONE_OF_MANY", - "filters": [ - { - "filterType": "ABI", - "value": "x86" - } - ], - "attributes": [], - "versionCode": 18, - "versionName": "0.5.1", - "outputFile": "app-x86-release.apk" - }, { "type": "ONE_OF_MANY", "filters": [ @@ -66,6 +40,32 @@ "versionCode": 18, "versionName": "0.5.1", "outputFile": "app-arm64-v8a-release.apk" + }, + { + "type": "ONE_OF_MANY", + "filters": [ + { + "filterType": "ABI", + "value": "x86_64" + } + ], + "attributes": [], + "versionCode": 18, + "versionName": "0.5.1", + "outputFile": "app-x86_64-release.apk" + }, + { + "type": "ONE_OF_MANY", + "filters": [ + { + "filterType": "ABI", + "value": "x86" + } + ], + "attributes": [], + "versionCode": 18, + "versionName": "0.5.1", + "outputFile": "app-x86-release.apk" } ], "elementType": "File" diff --git a/app/src/main/java/com/github/libretube/MyApp.kt b/app/src/main/java/com/github/libretube/MyApp.kt index 6a56aa38e..c3d24e891 100644 --- a/app/src/main/java/com/github/libretube/MyApp.kt +++ b/app/src/main/java/com/github/libretube/MyApp.kt @@ -16,9 +16,7 @@ import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID -import com.github.libretube.constants.PIPED_API_URL import com.github.libretube.constants.PUSH_CHANNEL_ID -import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchPosition @@ -55,7 +53,9 @@ class MyApp : Application() { /** * Set the api and the auth api url */ - initializeRetrofit() + RetrofitInstance.initialize() + CronetHelper.initCronet(this) + ImageHelper.initializeImageLoader(this) /** * Initialize the notification listener in the background @@ -82,26 +82,6 @@ class MyApp : Application() { databaseMigration() } - /** - * Set the api urls needed for the [RetrofitInstance] - */ - private fun initializeRetrofit() { - RetrofitInstance.url = - PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL) - // set auth instance - RetrofitInstance.authUrl = - if (PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) { - PreferenceHelper.getString( - PreferenceKeys.AUTH_INSTANCE, - PIPED_API_URL - ) - } else { - RetrofitInstance.url - } - CronetHelper.initCronet(this) - ImageHelper.initializeImageLoader(this) - } - /** * Initializes the required [NotificationChannel]s for the app. */ diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index a0cbaed35..920c93bb2 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -3,12 +3,10 @@ package com.github.libretube.activities import android.content.Intent import android.content.pm.ActivityInfo import android.content.res.Configuration -import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -27,11 +25,11 @@ import androidx.navigation.NavController import androidx.navigation.findNavController import androidx.navigation.ui.setupWithNavController import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.ActivityMainBinding import com.github.libretube.dialogs.ErrorDialog import com.github.libretube.extensions.BaseActivity -import com.github.libretube.extensions.TAG import com.github.libretube.fragments.PlayerFragment import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.SearchViewModel @@ -316,95 +314,39 @@ class MainActivity : BaseActivity() { override fun onStart() { super.onStart() - val intentData: Uri? = intent?.data - // check whether an URI got submitted over the intent data - if (intentData != null && intentData.host != null && intentData.path != null) { - Log.d(TAG(), "intentData: ${intentData.host} ${intentData.path} ") - // load the URI of the submitted link (e.g. video) - loadIntentData(intentData) + // check whether an URI got submitted over the intent data and load it + when { + intent?.getStringExtra(IntentData.channelId) != null -> navController.navigate( + R.id.channelFragment, + bundleOf( + IntentData.channelName to intent?.getStringExtra(IntentData.channelId)!! + ) + ) + intent?.getStringExtra(IntentData.channelName) != null -> navController.navigate( + R.id.channelFragment, + bundleOf( + IntentData.channelName to intent?.getStringExtra(IntentData.channelName) + ) + ) + intent?.getStringExtra(IntentData.playlistId) != null -> navController.navigate( + R.id.playlistFragment, + bundleOf( + IntentData.playlistId to intent?.getStringExtra(IntentData.playlistId)!! + ) + ) + intent?.getStringExtra(IntentData.videoId) != null -> loadVideo( + videoId = intent?.getStringExtra(IntentData.videoId)!!, + timeStamp = intent?.getLongExtra(IntentData.timeStamp, 0L) + ) } } - private fun loadIntentData(data: Uri) { - if (data.path!!.contains("/channel/") - ) { - val channelId = data.path!! - .replace("/channel/", "") - - loadChannel(channelId = channelId) - } else if ( - data.path!!.contains("/c/") || - data.path!!.contains("/user/") - ) { - val channelName = data.path!! - .replace("/c/", "") - .replace("/user/", "") - - loadChannel(channelName = channelName) - } else if ( - data.path!!.contains("/playlist") - ) { - var playlistId = data.query!! - if (playlistId.contains("&")) { - for (v in playlistId.split("&")) { - if (v.contains("list=")) { - playlistId = v.replace("list=", "") - break - } - } - } else { - playlistId = playlistId.replace("list=", "") - } - - loadPlaylist(playlistId) - } else if ( - data.path!!.contains("/shorts/") || - data.path!!.contains("/embed/") || - data.path!!.contains("/v/") - ) { - val videoId = data.path!! - .replace("/shorts/", "") - .replace("/v/", "") - .replace("/embed/", "") - - loadVideo(videoId, data.query) - } else if (data.path!!.contains("/watch") && data.query != null) { - var videoId = data.query!! - - if (videoId.contains("&")) { - val watches = videoId.split("&") - for (v in watches) { - if (v.contains("v=")) { - videoId = v.replace("v=", "") - break - } - } - } else { - videoId = videoId - .replace("v=", "") - } - - loadVideo(videoId, data.query) - } else { - val videoId = data.path!!.replace("/", "") - - loadVideo(videoId, data.query) - } - } - - private fun loadVideo(videoId: String, query: String?) { - Log.i(TAG(), "URI type: Video") - + private fun loadVideo(videoId: String, timeStamp: Long?) { val bundle = Bundle() - Log.e(TAG(), videoId) - // for time stamped links - if (query != null && query.contains("t=")) { - val timeStamp = query.toString().split("t=")[1] - bundle.putLong("timeStamp", timeStamp.toLong()) - } + bundle.putString(IntentData.videoId, videoId) + if (timeStamp != null) bundle.putLong(IntentData.timeStamp, timeStamp) - bundle.putString("videoId", videoId) val frag = PlayerFragment() frag.arguments = bundle @@ -421,27 +363,6 @@ class MainActivity : BaseActivity() { }, 100) } - private fun loadChannel( - channelId: String? = null, - channelName: String? = null - ) { - Log.i(TAG(), "Uri Type: Channel") - - val bundle = if (channelId != null) { - bundleOf("channel_id" to channelId) - } else { - bundleOf("channel_name" to channelName) - } - navController.navigate(R.id.channelFragment, bundle) - } - - private fun loadPlaylist(playlistId: String) { - Log.i(TAG(), "Uri Type: Playlist") - - val bundle = bundleOf("playlist_id" to playlistId) - navController.navigate(R.id.playlistFragment, bundle) - } - private fun minimizePlayer() { binding.mainMotionLayout.transitionToEnd() findViewById(R.id.main_container).isClickable = false diff --git a/app/src/main/java/com/github/libretube/activities/RouterActivity.kt b/app/src/main/java/com/github/libretube/activities/RouterActivity.kt index ea3abb77c..5cc20ffe2 100644 --- a/app/src/main/java/com/github/libretube/activities/RouterActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/RouterActivity.kt @@ -6,6 +6,7 @@ import android.net.Uri import android.os.Bundle import android.util.Log import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.TAG import com.github.libretube.util.ThemeHelper @@ -35,13 +36,87 @@ class RouterActivity : BaseActivity() { return hostsList.contains(intentDataHost) } + /** + * Resolve the uri and return a bundle with the arguments + */ + private fun resolveType(intent: Intent, uri: Uri): Intent { + when { + uri.path!!.contains("/channel/") -> { + val channelId = uri.path!! + .replace("/channel/", "") + + intent.putExtra(IntentData.channelId, channelId) + } + uri.path!!.contains("/c/") || uri.path!!.contains("/user/") -> { + val channelName = uri.path!! + .replace("/c/", "") + .replace("/user/", "") + + intent.putExtra(IntentData.channelName, channelName) + } + uri.path!!.contains("/playlist") -> { + var playlistId = uri.query!! + if (playlistId.contains("&")) { + for (v in playlistId.split("&")) { + if (v.contains("list=")) { + playlistId = v.replace("list=", "") + break + } + } + } else { + playlistId = playlistId.replace("list=", "") + } + + intent.putExtra(IntentData.playlistId, playlistId) + } + uri.path!!.contains("/shorts/") || + uri.path!!.contains("/embed/") || + uri.path!!.contains("/v/") + -> { + val videoId = uri.path!! + .replace("/shorts/", "") + .replace("/v/", "") + .replace("/embed/", "") + + intent.putExtra(IntentData.videoId, videoId) + } + uri.path!!.contains("/watch") && uri.query != null -> { + var videoId = uri.query!! + + if (videoId.contains("&")) { + val watches = videoId.split("&") + for (v in watches) { + if (v.contains("v=")) { + videoId = v.replace("v=", "") + break + } + } + } else { + videoId = videoId + .replace("v=", "") + } + + intent.putExtra(IntentData.videoId, videoId) + } else -> { + val timeStamp = uri.getQueryParameter("t") + val videoId = uri.path!!.replace("/", "") + + intent.putExtra(IntentData.videoId, videoId) + if (timeStamp != null) intent.putExtra(IntentData.timeStamp, timeStamp.toLong()) + } + } + return intent + } + private fun handleSendText(uri: Uri) { Log.i(TAG(), uri.toString()) + val pm: PackageManager = this.packageManager val intent = pm.getLaunchIntentForPackage(this.packageName) intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - intent?.data = uri - this.startActivity(intent) + this.startActivity( + resolveType(intent!!, uri) + ) this.finishAndRemoveTask() } } diff --git a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt index ac781fbb3..dcefa6541 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -49,7 +49,7 @@ class ChannelAdapter( root.setOnClickListener { NavigationHelper.navigateVideo(root.context, trending.url) } - val videoId = trending.url.toID() + val videoId = trending.url!!.toID() root.setOnLongClickListener { VideoOptionsDialog(videoId) .show(childFragmentManager, VideoOptionsDialog::class.java.name) diff --git a/app/src/main/java/com/github/libretube/adapters/LegacySubscriptionAdapter.kt b/app/src/main/java/com/github/libretube/adapters/LegacySubscriptionAdapter.kt index 677c8f742..253e6fb75 100644 --- a/app/src/main/java/com/github/libretube/adapters/LegacySubscriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/LegacySubscriptionAdapter.kt @@ -33,7 +33,7 @@ class LegacySubscriptionAdapter( root.setOnClickListener { NavigationHelper.navigateChannel( root.context, - subscription.url.toID() + subscription.url!!.toID() ) } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index 5abacf141..b36d3fd39 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -97,7 +97,7 @@ class SearchAdapter( root.setOnClickListener { NavigationHelper.navigateVideo(root.context, item.url) } - val videoId = item.url.toID() + val videoId = item.url!!.toID() root.setOnLongClickListener { VideoOptionsDialog(videoId) .show(childFragmentManager, VideoOptionsDialog::class.java.name) @@ -121,7 +121,7 @@ class SearchAdapter( root.setOnClickListener { NavigationHelper.navigateChannel(root.context, item.url) } - val channelId = item.url.toID() + val channelId = item.url!!.toID() isSubscribed(channelId, binding) } diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index ad1145412..23d5489a9 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -36,7 +36,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList + + // fetch latest version info + @GET(GITHUB_API_URL) + suspend fun getUpdateInfo(): UpdateInfo +} diff --git a/app/src/main/java/com/github/libretube/api/PipedApi.kt b/app/src/main/java/com/github/libretube/api/PipedApi.kt index ce6d406ba..6533b3ae6 100644 --- a/app/src/main/java/com/github/libretube/api/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/api/PipedApi.kt @@ -3,7 +3,6 @@ package com.github.libretube.api import com.github.libretube.obj.Channel import com.github.libretube.obj.CommentsPage import com.github.libretube.obj.DeleteUserRequest -import com.github.libretube.obj.Instances import com.github.libretube.obj.Login import com.github.libretube.obj.Message import com.github.libretube.obj.Playlist @@ -23,7 +22,6 @@ import retrofit2.http.Header import retrofit2.http.POST import retrofit2.http.Path import retrofit2.http.Query -import retrofit2.http.Url interface PipedApi { @GET("trending") @@ -165,8 +163,4 @@ interface PipedApi { @Header("Authorization") token: String, @Body playlistId: PlaylistId ): Message - - // only for fetching servers list - @GET - suspend fun getInstances(@Url url: String): List } diff --git a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt index 350accbab..03f69cee7 100644 --- a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt +++ b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt @@ -1,5 +1,8 @@ package com.github.libretube.api +import com.github.libretube.constants.PIPED_API_URL +import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.util.PreferenceHelper import retrofit2.Retrofit import retrofit2.converter.jackson.JacksonConverterFactory @@ -7,20 +10,55 @@ object RetrofitInstance { lateinit var url: String lateinit var authUrl: String val lazyMgr = resettableManager() + val jacksonConverterFactory = JacksonConverterFactory.create() + val api: PipedApi by resettableLazy(lazyMgr) { Retrofit.Builder() .baseUrl(url) .callFactory(CronetHelper.callFactory) - .addConverterFactory(JacksonConverterFactory.create()) + .addConverterFactory(jacksonConverterFactory) .build() .create(PipedApi::class.java) } + val authApi: PipedApi by resettableLazy(lazyMgr) { Retrofit.Builder() .baseUrl(authUrl) .callFactory(CronetHelper.callFactory) - .addConverterFactory(JacksonConverterFactory.create()) + .addConverterFactory(jacksonConverterFactory) .build() .create(PipedApi::class.java) } + + val externalApi: ExternalApi by resettableLazy(lazyMgr) { + Retrofit.Builder() + .baseUrl(url) + .callFactory(CronetHelper.callFactory) + .addConverterFactory(jacksonConverterFactory) + .build() + .create(ExternalApi::class.java) + } + + /** + * Set the api urls needed for the [RetrofitInstance] + */ + fun initialize() { + url = + PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL) + // set auth instance + authUrl = + if ( + PreferenceHelper.getBoolean( + PreferenceKeys.AUTH_INSTANCE_TOGGLE, + false + ) + ) { + PreferenceHelper.getString( + PreferenceKeys.AUTH_INSTANCE, + PIPED_API_URL + ) + } else { + url + } + } } diff --git a/app/src/main/java/com/github/libretube/constants/Constants.kt b/app/src/main/java/com/github/libretube/constants/Constants.kt index 51dde19c2..b3a959e13 100644 --- a/app/src/main/java/com/github/libretube/constants/Constants.kt +++ b/app/src/main/java/com/github/libretube/constants/Constants.kt @@ -34,6 +34,7 @@ const val YOUTUBE_FRONTEND_URL = "https://www.youtube.com" * Retrofit Instance */ const val PIPED_API_URL = "https://pipedapi.kavin.rocks/" +const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks/" /** * Notification IDs diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt new file mode 100644 index 000000000..b1e4b7ce7 --- /dev/null +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -0,0 +1,10 @@ +package com.github.libretube.constants + +object IntentData { + const val videoId = "videoId" + const val channelId = "channelId" + const val channelName = "channelName" + const val playlistId = "playlistId" + const val timeStamp = "timeStamp" + const val position = "position" +} diff --git a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt index 9855ed794..65c04f5b8 100644 --- a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt @@ -11,6 +11,7 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import com.github.libretube.R import com.github.libretube.api.RetrofitInstance +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.DialogAddtoplaylistBinding import com.github.libretube.extensions.TAG import com.github.libretube.models.PlaylistViewModel @@ -29,7 +30,7 @@ class AddToPlaylistDialog : DialogFragment() { private lateinit var token: String override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - videoId = arguments?.getString("videoId")!! + videoId = arguments?.getString(IntentData.videoId)!! binding = DialogAddtoplaylistBinding.inflate(layoutInflater) binding.title.text = ThemeHelper.getStyledAppName(requireContext()) diff --git a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt index 57402c18b..ce5898a08 100644 --- a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt @@ -63,7 +63,7 @@ class PlaylistOptionsDialog( } BackgroundHelper.playOnBackground( context = requireContext(), - videoId = playlist.relatedStreams!![0].url.toID(), + videoId = playlist.relatedStreams!![0].url!!.toID(), playlistId = playlistId ) } diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index 8793146ec..8caedc902 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -9,6 +9,7 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.Globals import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.PreferenceHelper @@ -79,7 +80,7 @@ class VideoOptionsDialog( if (token != "") { val newFragment = AddToPlaylistDialog() val bundle = Bundle() - bundle.putString("videoId", videoId) + bundle.putString(IntentData.videoId, videoId) newFragment.arguments = bundle newFragment.show( parentFragmentManager, diff --git a/app/src/main/java/com/github/libretube/extensions/ToID.kt b/app/src/main/java/com/github/libretube/extensions/ToID.kt index 9c6606c57..df7b284e0 100644 --- a/app/src/main/java/com/github/libretube/extensions/ToID.kt +++ b/app/src/main/java/com/github/libretube/extensions/ToID.kt @@ -3,8 +3,8 @@ package com.github.libretube.extensions /** * format a Piped route to an ID */ -fun Any?.toID(): String { - return this!! +fun Any.toID(): String { + return this .toString() .replace("/watch?v=", "") // videos .replace("/channel/", "") // channels diff --git a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt index 0c9595e9f..a2a92473e 100644 --- a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt @@ -11,6 +11,7 @@ import com.github.libretube.R import com.github.libretube.adapters.ChannelAdapter import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.FragmentChannelBinding import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.TAG @@ -34,10 +35,11 @@ class ChannelFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { - channelId = it.getString("channel_id").toID() - channelName = it.getString("channel_name") + channelId = it.getString(IntentData.channelId)?.toID() + channelName = it.getString(IntentData.channelName) ?.replace("/c/", "") ?.replace("/user/", "") + Log.e(TAG(), channelName.toString()) } } @@ -59,7 +61,6 @@ class ChannelFragment : BaseFragment() { val refreshChannel = { binding.channelRefresh.isRefreshing = true fetchChannel() - isSubscribed() } refreshChannel() binding.channelRefresh.setOnRefreshListener { @@ -81,31 +82,6 @@ class ChannelFragment : BaseFragment() { } } - private fun isSubscribed() { - lifecycleScope.launchWhenCreated { - isSubscribed = SubscriptionHelper.isSubscribed(channelId!!) - if (isSubscribed == null) return@launchWhenCreated - - runOnUiThread { - if (isSubscribed == true) { - binding.channelSubscribe.text = getString(R.string.unsubscribe) - } - - binding.channelSubscribe.setOnClickListener { - binding.channelSubscribe.text = if (isSubscribed == true) { - SubscriptionHelper.unsubscribe(channelId!!) - isSubscribed = false - getString(R.string.subscribe) - } else { - SubscriptionHelper.subscribe(channelId!!) - isSubscribed = true - getString(R.string.unsubscribe) - } - } - } - } - } - private fun fetchChannel() { fun run() { lifecycleScope.launchWhenCreated { @@ -125,9 +101,35 @@ class ChannelFragment : BaseFragment() { Log.e(TAG(), "HttpException, unexpected response") return@launchWhenCreated } + // needed if the channel gets loaded by the ID + channelId = response.id + + // fetch and update the subscription status + isSubscribed = SubscriptionHelper.isSubscribed(channelId!!) + if (isSubscribed == null) return@launchWhenCreated + + runOnUiThread { + if (isSubscribed == true) { + binding.channelSubscribe.text = getString(R.string.unsubscribe) + } + + binding.channelSubscribe.setOnClickListener { + binding.channelSubscribe.text = if (isSubscribed == true) { + SubscriptionHelper.unsubscribe(channelId!!) + isSubscribed = false + getString(R.string.subscribe) + } else { + SubscriptionHelper.subscribe(channelId!!) + isSubscribed = true + getString(R.string.unsubscribe) + } + } + } + nextPage = response.nextpage isLoading = false binding.channelRefresh.isRefreshing = false + runOnUiThread { binding.channelScrollView.visibility = View.VISIBLE binding.channelName.text = response.name diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 8de655787..8f35ddb08 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -44,6 +44,7 @@ import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceRanges import com.github.libretube.databinding.DialogSliderBinding @@ -193,8 +194,8 @@ class PlayerFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { - videoId = it.getString("videoId").toID() - playlistId = it.getString("playlistId") + videoId = it.getString(IntentData.videoId)!!.toID() + playlistId = it.getString(IntentData.playlistId) } } @@ -979,7 +980,7 @@ class PlayerFragment : BaseFragment() { // seek to saved watch position if available private fun seekToWatchPosition() { // support for time stamped links - val timeStamp: Long? = arguments?.getLong("timeStamp") + val timeStamp: Long? = arguments?.getLong(IntentData.timeStamp) if (timeStamp != null && timeStamp != 0L) { exoPlayer.seekTo(timeStamp * 1000) return @@ -1205,7 +1206,7 @@ class PlayerFragment : BaseFragment() { binding.playerChannel.setOnClickListener { val activity = view?.context as MainActivity - val bundle = bundleOf("channel_id" to response.uploaderUrl) + val bundle = bundleOf(IntentData.channelId to response.uploaderUrl) activity.navController.navigate(R.id.channelFragment, bundle) activity.binding.mainMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd() @@ -1218,7 +1219,7 @@ class PlayerFragment : BaseFragment() { binding.relPlayerSave.setOnClickListener { val newFragment = AddToPlaylistDialog() val bundle = Bundle() - bundle.putString("videoId", videoId) + bundle.putString(IntentData.videoId, videoId) newFragment.arguments = bundle newFragment.show(childFragmentManager, AddToPlaylistDialog::class.java.name) } @@ -1599,7 +1600,7 @@ class PlayerFragment : BaseFragment() { private fun isSubscribed() { fun run() { - val channelId = streams.uploaderUrl.toID() + val channelId = streams.uploaderUrl!!.toID() lifecycleScope.launchWhenCreated { isSubscribed = SubscriptionHelper.isSubscribed(channelId) diff --git a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt index fc7fcc63c..bc4b1c849 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt @@ -12,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.adapters.PlaylistAdapter import com.github.libretube.api.RetrofitInstance +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.FragmentPlaylistBinding import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.extensions.BaseFragment @@ -32,7 +33,7 @@ class PlaylistFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { - playlistId = it.getString("playlist_id") + playlistId = it.getString(IntentData.playlistId) isOwner = it.getBoolean("isOwner") } } diff --git a/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt b/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt index 4dd633a87..4640b2a03 100644 --- a/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt +++ b/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt @@ -48,7 +48,7 @@ class SubscriptionsViewModel : ViewModel() { this@SubscriptionsViewModel.videoFeed.postValue(videoFeed) if (videoFeed.isNotEmpty()) { // save the last recent video to the prefs for the notification worker - PreferenceHelper.setLatestVideoId(videoFeed[0].url.toID()) + PreferenceHelper.setLatestVideoId(videoFeed[0].url!!.toID()) } } } diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index 72d27726a..8b92b8f97 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -179,7 +179,7 @@ class InstanceSettings : MaterialPreferenceFragment() { // fetch official public instances val response = try { - RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/") + RetrofitInstance.externalApi.getInstances() } catch (e: Exception) { e.printStackTrace() emptyList() diff --git a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt index b56f13eb9..5a1dcd500 100644 --- a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt @@ -6,9 +6,9 @@ import androidx.preference.Preference import com.github.libretube.BuildConfig import com.github.libretube.R import com.github.libretube.activities.SettingsActivity +import com.github.libretube.api.RetrofitInstance import com.github.libretube.dialogs.UpdateDialog import com.github.libretube.extensions.getStyledSnackBar -import com.github.libretube.update.UpdateChecker import com.github.libretube.util.NetworkHelper import com.github.libretube.views.MaterialPreferenceFragment import kotlinx.coroutines.CoroutineScope @@ -102,8 +102,12 @@ class MainSettings : MaterialPreferenceFragment() { return@launch } // check for update - val updateInfo = UpdateChecker.getLatestReleaseInfo() - if (updateInfo?.name == null) { + val updateInfo = try { + RetrofitInstance.externalApi.getUpdateInfo() + } catch (e: Exception) { + return@launch + } + if (updateInfo.name == null) { // request failed (activity as? SettingsActivity)?.binding?.root?.getStyledSnackBar(R.string.unknown_error) ?.show() diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index e914b0bab..14b4ea40a 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -15,6 +15,7 @@ import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.BACKGROUND_CHANNEL_ID +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PreferenceKeys import com.github.libretube.extensions.toID @@ -120,9 +121,9 @@ class BackgroundMode : Service() { Globals.playingQueue.clear() // get the intent arguments - videoId = intent?.getStringExtra("videoId")!! - playlistId = intent.getStringExtra("playlistId") - val position = intent.getLongExtra("position", 0L) + videoId = intent?.getStringExtra(IntentData.videoId)!! + playlistId = intent.getStringExtra(IntentData.playlistId) + val position = intent.getLongExtra(IntentData.position, 0L) // initialize the playlist autoPlay Helper autoPlayHelper = AutoPlayHelper(playlistId) @@ -220,7 +221,7 @@ class BackgroundMode : Service() { */ private fun setNextStream() { if (streams!!.relatedStreams!!.isNotEmpty()) { - nextStreamId = streams?.relatedStreams!![0].url.toID() + nextStreamId = streams?.relatedStreams!![0].url!!.toID() } if (playlistId == null) return diff --git a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt b/app/src/main/java/com/github/libretube/update/UpdateChecker.kt deleted file mode 100644 index 693a50d15..000000000 --- a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.libretube.update - -import com.fasterxml.jackson.databind.ObjectMapper -import com.github.libretube.constants.GITHUB_API_URL -import com.github.libretube.extensions.await -import java.net.URL - -object UpdateChecker { - fun getLatestReleaseInfo(): UpdateInfo? { - var versionInfo: UpdateInfo? = null - // run http request as thread to make it async - Thread { - // otherwise crashes without internet - versionInfo = getUpdateInfo() - try { - versionInfo = getUpdateInfo() - } catch (e: Exception) { - } - }.await() - - // return the information about the latest version - return versionInfo - } - - private fun getUpdateInfo(): UpdateInfo? { - // get the github API response - val latestVersionApiUrl = URL(GITHUB_API_URL) - val json = latestVersionApiUrl.readText() - - // Parse and return the json data - val mapper = ObjectMapper() - return mapper.readValue(json, UpdateInfo::class.java) - } -} diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index cfd804cb6..3217312e4 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -55,7 +55,7 @@ class AutoPlayHelper( ) ) ) { - nextStreamId = relatedStreams[index].url.toID() + nextStreamId = relatedStreams[index].url!!.toID() if (index + 1 < relatedStreams.size) { index += 1 } else { @@ -94,7 +94,7 @@ class AutoPlayHelper( ) } // save the playlist urls to the list - playlistStreamIds += playlist.relatedStreams!!.map { it.url.toID() } + playlistStreamIds += playlist.relatedStreams!!.map { it.url!!.toID() } // save playlistNextPage for usage if video is not contained playlistNextPage = playlist.nextpage return@withContext getNextPlaylistVideoId(currentVideoId) diff --git a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt index 87d0f3ab0..475a21abf 100644 --- a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt @@ -3,6 +3,7 @@ package com.github.libretube.util import android.content.Context import android.content.Intent import android.os.Build +import com.github.libretube.constants.IntentData import com.github.libretube.services.BackgroundMode /** @@ -17,8 +18,8 @@ object BackgroundHelper { ) { // create an intent for the background mode service val intent = Intent(context, BackgroundMode::class.java) - intent.putExtra("videoId", videoId) - if (playlistId != null) intent.putExtra("playlistId", playlistId) + intent.putExtra(IntentData.videoId, videoId) + if (playlistId != null) intent.putExtra(IntentData.playlistId, playlistId) if (position != null) intent.putExtra("position", position) // start the background mode as foreground service diff --git a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt index 54c7cb72d..2f473dd26 100644 --- a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt @@ -7,6 +7,7 @@ import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.os.bundleOf import com.github.libretube.R import com.github.libretube.activities.MainActivity +import com.github.libretube.constants.IntentData import com.github.libretube.extensions.toID import com.github.libretube.fragments.PlayerFragment @@ -17,7 +18,7 @@ object NavigationHelper { ) { if (channelId != null) { val activity = context as MainActivity - val bundle = bundleOf("channel_id" to channelId) + val bundle = bundleOf(IntentData.channelId to channelId) activity.navController.navigate(R.id.channelFragment, bundle) try { val mainMotionLayout = @@ -39,8 +40,8 @@ object NavigationHelper { ) { if (videoId != null) { val bundle = Bundle() - bundle.putString("videoId", videoId.toID()) - if (playlistId != null) bundle.putString("playlistId", playlistId) + bundle.putString(IntentData.videoId, videoId.toID()) + if (playlistId != null) bundle.putString(IntentData.playlistId, playlistId) val frag = PlayerFragment() frag.arguments = bundle val activity = context as AppCompatActivity @@ -61,7 +62,7 @@ object NavigationHelper { if (playlistId != null) { val activity = context as MainActivity val bundle = Bundle() - bundle.putString("playlist_id", playlistId) + bundle.putString(IntentData.playlistId, playlistId) bundle.putBoolean("isOwner", isOwner) activity.navController.navigate(R.id.playlistFragment, bundle) } diff --git a/app/src/main/res/navigation/nav.xml b/app/src/main/res/navigation/nav.xml index 90a00d2be..19197ea5c 100644 --- a/app/src/main/res/navigation/nav.xml +++ b/app/src/main/res/navigation/nav.xml @@ -35,18 +35,12 @@ android:name="com.github.libretube.fragments.ChannelFragment" android:label="channel" tools:layout="@layout/fragment_channel"> - -