From 9f17bf6378041eecf204e29983171a76183dbde2 Mon Sep 17 00:00:00 2001 From: Prathamesh More Date: Sun, 12 Sep 2021 19:49:37 +0530 Subject: [PATCH] Converted UserInfo activity to fragment --- app/src/main/AndroidManifest.xml | 1 - .../UserInfoFragment.kt} | 78 ++++++++++++++----- .../retromusic/fragments/home/HomeFragment.kt | 62 ++++++++------- .../retromusic/util/NavigationUtil.java | 6 +- app/src/main/res/layout/fragment_home.xml | 8 +- ...y_user_info.xml => fragment_user_info.xml} | 5 +- 6 files changed, 101 insertions(+), 59 deletions(-) rename app/src/main/java/code/name/monkey/retromusic/{activities/UserInfoActivity.kt => fragments/UserInfoFragment.kt} (75%) rename app/src/main/res/layout/{activity_user_info.xml => fragment_user_info.xml} (95%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 16fa048d..0ef67ef1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -112,7 +112,6 @@ - diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/UserInfoActivity.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/UserInfoFragment.kt similarity index 75% rename from app/src/main/java/code/name/monkey/retromusic/activities/UserInfoActivity.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/UserInfoFragment.kt index 6288b14b..063f04ef 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/UserInfoActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/UserInfoFragment.kt @@ -12,25 +12,33 @@ * See the GNU General Public License for more details. * */ -package code.name.monkey.retromusic.activities +package code.name.monkey.retromusic.fragments import android.app.Activity import android.content.Intent import android.content.res.ColorStateList import android.graphics.Bitmap +import android.graphics.Color import android.net.Uri import android.os.Bundle import android.text.TextUtils +import android.view.LayoutInflater import android.view.MenuItem +import android.view.View +import android.view.ViewGroup import android.widget.Toast +import androidx.core.view.doOnPreDraw +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController import code.name.monkey.appthemehelper.util.ColorUtil import code.name.monkey.appthemehelper.util.MaterialValueHelper import code.name.monkey.retromusic.Constants.USER_BANNER import code.name.monkey.retromusic.Constants.USER_PROFILE -import code.name.monkey.retromusic.activities.base.AbsBaseActivity -import code.name.monkey.retromusic.databinding.ActivityUserInfoBinding +import code.name.monkey.retromusic.R +import code.name.monkey.retromusic.databinding.FragmentUserInfoBinding import code.name.monkey.retromusic.extensions.accentColor import code.name.monkey.retromusic.extensions.applyToolbar +import code.name.monkey.retromusic.extensions.resolveColor import code.name.monkey.retromusic.glide.GlideApp import code.name.monkey.retromusic.glide.RetroGlideExtension import code.name.monkey.retromusic.util.ImageUtil @@ -43,6 +51,7 @@ import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.github.dhaval2404.imagepicker.ImagePicker import com.github.dhaval2404.imagepicker.constant.ImageProvider +import com.google.android.material.transition.MaterialContainerTransform import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -52,16 +61,28 @@ import java.io.File import java.io.FileOutputStream import java.io.IOException -class UserInfoActivity : AbsBaseActivity() { +class UserInfoFragment : Fragment() { - private lateinit var binding: ActivityUserInfoBinding + private var _binding: FragmentUserInfoBinding? = null + private val binding get() = _binding!! - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - binding = ActivityUserInfoBinding.inflate(layoutInflater) - setContentView(binding.root) - setStatusbarColorAuto() - setTaskDescriptionColorAuto() + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + sharedElementEnterTransition = MaterialContainerTransform().apply { + drawingViewId = R.id.fragment_container + duration = 300L + scrimColor = Color.TRANSPARENT + setAllContainerColors(requireContext().resolveColor(R.attr.colorSurface)) + } + _binding = FragmentUserInfoBinding.inflate(layoutInflater) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) applyToolbar(binding.toolbar) binding.nameContainer.accentColor() @@ -78,20 +99,30 @@ class UserInfoActivity : AbsBaseActivity() { binding.next.setOnClickListener { val nameString = binding.name.text.toString().trim { it <= ' ' } if (TextUtils.isEmpty(nameString)) { - Toast.makeText(this, "Umm you're name can't be empty!", Toast.LENGTH_SHORT).show() + Toast.makeText( + requireContext(), + "Umm you're name can't be empty!", + Toast.LENGTH_SHORT + ).show() return@setOnClickListener } PreferenceUtil.userName = nameString - setResult(Activity.RESULT_OK) - finish() + findNavController().navigateUp() } val textColor = - MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(accentColor())) + MaterialValueHelper.getPrimaryTextColor( + requireContext(), + ColorUtil.isColorLight(accentColor()) + ) binding.next.backgroundTintList = ColorStateList.valueOf(accentColor()) binding.next.iconTint = ColorStateList.valueOf(textColor) binding.next.setTextColor(textColor) loadProfile() + postponeEnterTransition() + view.doOnPreDraw { + startPostponedEnterTransition() + } } private fun loadProfile() { @@ -110,7 +141,7 @@ class UserInfoActivity : AbsBaseActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == android.R.id.home) { - onBackPressed() + findNavController().navigateUp() } return super.onOptionsItemSelected(item) } @@ -131,7 +162,7 @@ class UserInfoActivity : AbsBaseActivity() { .start(PICK_IMAGE_REQUEST) } - public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) { val fileUri = data?.data @@ -140,9 +171,9 @@ class UserInfoActivity : AbsBaseActivity() { val fileUri = data?.data fileUri?.let { setAndSaveBannerImage(it) } } else if (resultCode == ImagePicker.RESULT_ERROR) { - Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), ImagePicker.getError(data), Toast.LENGTH_SHORT).show() } else { - Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), "Task Cancelled", Toast.LENGTH_SHORT).show() } } @@ -177,7 +208,7 @@ class UserInfoActivity : AbsBaseActivity() { private fun saveImage(bitmap: Bitmap, fileName: String) { CoroutineScope(Dispatchers.IO).launch { - val appDir = applicationContext.filesDir + val appDir = requireContext().filesDir val file = File(appDir, fileName) var successful = false try { @@ -190,7 +221,7 @@ class UserInfoActivity : AbsBaseActivity() { } if (successful) { withContext(Dispatchers.Main) { - Toast.makeText(this@UserInfoActivity, "Updated", Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), "Updated", Toast.LENGTH_SHORT).show() } } } @@ -225,6 +256,11 @@ class UserInfoActivity : AbsBaseActivity() { .into(binding.userImage) } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + companion object { private const val PICK_IMAGE_REQUEST = 9002 private const val PICK_BANNER_REQUEST = 9004 diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt index d55f196b..7812e679 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt @@ -14,7 +14,6 @@ */ package code.name.monkey.retromusic.fragments.home -import android.app.ActivityOptions import android.os.Bundle import android.view.Menu import android.view.MenuInflater @@ -24,6 +23,7 @@ import android.view.View import androidx.core.os.bundleOf import androidx.core.text.HtmlCompat import androidx.core.view.doOnPreDraw +import androidx.navigation.fragment.FragmentNavigatorExtras import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager import code.name.monkey.appthemehelper.ThemeStore @@ -38,10 +38,9 @@ import code.name.monkey.retromusic.dialogs.ImportPlaylistDialog import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment import code.name.monkey.retromusic.glide.GlideApp import code.name.monkey.retromusic.glide.RetroGlideExtension -import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.PreferenceUtil import com.google.android.gms.cast.framework.CastButtonFactory -import com.google.android.material.transition.MaterialFadeThrough +import com.google.android.material.shape.MaterialShapeDrawable import com.google.android.material.transition.MaterialSharedAxis class HomeFragment : @@ -55,14 +54,35 @@ class HomeFragment : _binding = getBinding(PreferenceUtil.isHomeBanner, view) mainActivity.setSupportActionBar(binding.toolbar) mainActivity.supportActionBar?.title = null - setStatusBarColorAuto(view) + setupListeners() + binding.titleWelcome.text = String.format("%s", PreferenceUtil.userName) + + val homeAdapter = HomeAdapter(mainActivity) + binding.recyclerView.apply { + layoutManager = LinearLayoutManager(mainActivity) + adapter = homeAdapter + } + libraryViewModel.getHome().observe(viewLifecycleOwner, { + homeAdapter.swapData(it) + }) + + loadProfile() + setupTitle() + postponeEnterTransition() + view.doOnPreDraw { startPostponedEnterTransition() } + binding.appBarLayout.statusBarForeground = + MaterialShapeDrawable.createWithElevationOverlay(requireContext()) + } + + private fun setupListeners() { binding.bannerImage?.setOnClickListener { - val options = ActivityOptions.makeSceneTransitionAnimation( - mainActivity, - binding.userImage, - getString(R.string.transition_user_image) + binding.userImage.transitionName = "userImage" + findNavController().navigate( + R.id.user_info_fragment, null, null, FragmentNavigatorExtras( + binding.userImage to "userImage" + ) ) - NavigationUtil.goToUserInfo(requireActivity(), options) + reenterTransition = null } binding.lastAdded.setOnClickListener { @@ -94,28 +114,12 @@ class HomeFragment : } binding.userImage.setOnClickListener { - val options = ActivityOptions.makeSceneTransitionAnimation( - mainActivity, - binding.userImage, - getString(R.string.transition_user_image) + findNavController().navigate( + R.id.user_info_fragment, null, null, FragmentNavigatorExtras( + binding.userImage to "user_image" + ) ) - NavigationUtil.goToUserInfo(requireActivity(), options) } - binding.titleWelcome.text = String.format("%s", PreferenceUtil.userName) - - val homeAdapter = HomeAdapter(mainActivity) - binding.recyclerView.apply { - layoutManager = LinearLayoutManager(mainActivity) - adapter = homeAdapter - } - libraryViewModel.getHome().observe(viewLifecycleOwner, { - homeAdapter.swapData(it) - }) - - loadProfile() - setupTitle() - postponeEnterTransition() - view.doOnPreDraw { startPostponedEnterTransition() } } private fun getBinding(homeBanner: Boolean, view: View): HomeBindingAdapter { diff --git a/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java b/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java index f0d324c7..03fe930d 100755 --- a/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java @@ -34,7 +34,7 @@ import code.name.monkey.retromusic.activities.LyricsActivity; import code.name.monkey.retromusic.activities.PlayingQueueActivity; import code.name.monkey.retromusic.activities.PurchaseActivity; import code.name.monkey.retromusic.activities.SupportDevelopmentActivity; -import code.name.monkey.retromusic.activities.UserInfoActivity; +import code.name.monkey.retromusic.fragments.UserInfoFragment; import code.name.monkey.retromusic.activities.WhatsNewActivity; import code.name.monkey.retromusic.activities.bugreport.BugReportActivity; import code.name.monkey.retromusic.helper.MusicPlayerRemote; @@ -70,8 +70,8 @@ public class NavigationUtil { public static void goToUserInfo( @NonNull Activity activity, @NonNull ActivityOptions activityOptions) { - ActivityCompat.startActivity( - activity, new Intent(activity, UserInfoActivity.class), activityOptions.toBundle()); + ActivityCompat.startActivity( + activity, new Intent(activity, UserInfoFragment.class), activityOptions.toBundle()); } public static void gotoDriveMode(@NotNull final Activity activity) { diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index dc261dac..e4100c67 100755 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -16,13 +16,15 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:fitsSystemWindows="true" android:transitionGroup="true"> + app:liftOnScroll="true" + android:fitsSystemWindows="true"> + tools:srcCompat="@tools:sample/avatars" + android:transitionName="user_image" />