From ac423623e6b93eaa67f2446520f52449eb1efc11 Mon Sep 17 00:00:00 2001 From: Shabinder Date: Mon, 9 Nov 2020 10:56:31 +0530 Subject: [PATCH] Clean Up --- app/build.gradle | 2 +- .../com/shabinder/spotiflyer/MainActivity.kt | 32 +++++++++++++------ .../ui/mainfragment/MainFragment.kt | 3 ++ .../spotiflyer/ui/spotify/SpotifyFragment.kt | 26 +++------------ .../spotiflyer/ui/youtube/YoutubeFragment.kt | 1 - .../shabinder/spotiflyer/utils/Provider.kt | 2 +- .../res/layout/download_record_fragment.xml | 2 ++ app/src/main/res/layout/main_activity.xml | 2 +- .../main/res/layout/track_list_fragment.xml | 1 - 9 files changed, 34 insertions(+), 37 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3382dc5f..96594a07 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -123,7 +123,7 @@ dependencies { implementation 'com.mpatric:mp3agic:0.9.1' implementation 'com.shreyaspatil:EasyUpiPayment:3.0.0' - implementation 'com.github.sealedtx:java-youtube-downloader:2.4.3' + implementation 'com.github.sealedtx:java-youtube-downloader:2.4.4' implementation "androidx.tonyodev.fetch2:xfetch2:3.1.5" implementation 'com.github.javiersantos:AppUpdater:2.7' diff --git a/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt b/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt index bf6e941f..2e96f354 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt @@ -21,7 +21,6 @@ import android.Manifest import android.annotation.SuppressLint import android.content.Context import android.content.Intent -import android.content.SharedPreferences import android.net.Uri import android.os.Build import android.os.Bundle @@ -33,13 +32,14 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProvider +import androidx.navigation.NavController +import androidx.navigation.findNavController import com.github.javiersantos.appupdater.AppUpdater import com.github.javiersantos.appupdater.enums.UpdateFrom import com.shabinder.spotiflyer.databinding.MainActivityBinding import com.shabinder.spotiflyer.networking.SpotifyService import com.shabinder.spotiflyer.networking.SpotifyServiceTokenRequest import com.shabinder.spotiflyer.utils.NetworkInterceptor -import com.shabinder.spotiflyer.utils.Provider.activity import com.shabinder.spotiflyer.utils.createDirectories import com.shabinder.spotiflyer.utils.isOnline import com.shabinder.spotiflyer.utils.startService @@ -53,25 +53,28 @@ import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import javax.inject.Inject +/* +* This is App's God Activity +* */ @Suppress("DEPRECATION") @AndroidEntryPoint class MainActivity : AppCompatActivity(){ private var spotifyService : SpotifyService? = null - private var sharedPref :SharedPreferences? = null private lateinit var binding: MainActivityBinding lateinit var snackBarAnchor: View private lateinit var sharedViewModel: SharedViewModel + private lateinit var navController: NavController @Inject lateinit var moshi: Moshi @Inject lateinit var spotifyServiceTokenRequest: SpotifyServiceTokenRequest override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.main_activity) - snackBarAnchor = binding.snackBarPosition sharedViewModel = ViewModelProvider(this).get(SharedViewModel::class.java) + navController = findNavController(R.id.navHostFragment) + snackBarAnchor = binding.snackBarPosition //Enabling Dark Mode AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) - sharedPref = this.getPreferences(Context.MODE_PRIVATE) authenticateSpotify() @@ -89,7 +92,8 @@ class MainActivity : AppCompatActivity(){ override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) - Log.i("NEW INTENT", "Received") + //Return to MainFragment For Further Processing of this Intent + navController.popBackStack(R.id.mainFragment,false) handleIntentFromExternalActivity(intent) } @@ -100,9 +104,10 @@ class MainActivity : AppCompatActivity(){ this.getSystemService(Context.POWER_SERVICE) as PowerManager val isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(packageName) if (!isIgnoringBatteryOptimizations) { - val intent = Intent() - intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS - intent.data = Uri.parse("package:$packageName") + val intent = Intent().apply{ + action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS + data = Uri.parse("package:$packageName") + } startActivityForResult(intent, 1233) } } @@ -188,6 +193,8 @@ class MainActivity : AppCompatActivity(){ .setUpdateFrom(UpdateFrom.XML) .setUpdateXML("https://raw.githubusercontent.com/Shabinder/SpotiFlyer/master/app/src/main/res/xml/app_update.xml") .setCancelable(false) + .setButtonDoNotShowAgain("Remind Later") + .setButtonDoNotShowAgainClickListener { dialog, _ -> dialog.dismiss() } .setButtonUpdateClickListener { _, _ -> val uri: Uri = Uri.parse("http://github.com/Shabinder/SpotiFlyer/releases") @@ -200,7 +207,12 @@ class MainActivity : AppCompatActivity(){ appUpdater.start() } + companion object{ + private lateinit var instance: MainActivity + fun getInstance():MainActivity = instance + } + init { - activity = this + instance = this } } \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt index 37779f6c..47e631c7 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt @@ -107,6 +107,9 @@ class MainFragment : Fragment() { sharedViewModel.intentString.observe(viewLifecycleOwner,{ if(it != ""){ sharedViewModel.uiScope.launch(Dispatchers.IO) { + //Wait for any Authentication to Finish , + // this Wait prevents from multiple Authentication Requests + Thread.sleep(1000) if(sharedViewModel.spotifyService.value == null){ //Not Authenticated Yet Provider.activity.authenticateSpotify() diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt index 3c3d9052..97876314 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt @@ -173,16 +173,6 @@ class SpotifyFragment : Fragment() { *Live Data Observers **/ private fun initializeLiveDataObservers() { - /** - * CoverUrl Binding Observer! - **/ - viewModel.coverUrl.observe(viewLifecycleOwner, { - if(it!="Loading") bindImage(binding.coverImage,it, Source.Spotify) - }) - - /** - * TrackList Binding Observer! - **/ viewModel.trackList.observe(viewLifecycleOwner, { if (it.isNotEmpty()){ Log.i("SpotifyFragment","TrackList Updated") @@ -191,19 +181,12 @@ class SpotifyFragment : Fragment() { } }) - /** - * Title Binding Observer! - **/ - viewModel.title.observe(viewLifecycleOwner, { - binding.titleView.text = it + viewModel.coverUrl.observe(viewLifecycleOwner, { + if(it!="Loading") bindImage(binding.coverImage,it, Source.Spotify) }) - sharedViewModel.intentString.observe(viewLifecycleOwner,{ - //Waiting for Authentication to Finish with Spotify()Access Token Observe - if(it != "" && it!=SpotifyFragmentArgs.fromBundle(requireArguments()).link){ - //New Intent Received , Time TO RELOAD - (activity as MainActivity).onBackPressed() - } + viewModel.title.observe(viewLifecycleOwner, { + binding.titleView.text = it }) } @@ -215,7 +198,6 @@ class SpotifyFragment : Fragment() { setImageResource(R.drawable.ic_tick) visibility = View.VISIBLE clearAnimation() - keepScreenOn = false } } } diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/youtube/YoutubeFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/youtube/YoutubeFragment.kt index dcf3737c..6d2a4432 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/youtube/YoutubeFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/youtube/YoutubeFragment.kt @@ -181,7 +181,6 @@ class YoutubeFragment : Fragment() { setImageResource(R.drawable.ic_tick) visibility = View.VISIBLE clearAnimation() - keepScreenOn = false } } } diff --git a/app/src/main/java/com/shabinder/spotiflyer/utils/Provider.kt b/app/src/main/java/com/shabinder/spotiflyer/utils/Provider.kt index aad87022..15323e62 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/utils/Provider.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/utils/Provider.kt @@ -49,7 +49,7 @@ import javax.inject.Singleton @Module object Provider { - lateinit var activity: MainActivity + val activity: MainActivity = MainActivity.getInstance() val defaultDir = Environment.DIRECTORY_MUSIC + File.separator + "SpotiFlyer" + File.separator diff --git a/app/src/main/res/layout/download_record_fragment.xml b/app/src/main/res/layout/download_record_fragment.xml index 3e921168..ab736686 100755 --- a/app/src/main/res/layout/download_record_fragment.xml +++ b/app/src/main/res/layout/download_record_fragment.xml @@ -20,6 +20,7 @@