Converted UserInfo activity to fragment

main
Prathamesh More 2021-09-12 19:49:37 +05:30
parent 15a86f0f5b
commit 9f17bf6378
6 changed files with 101 additions and 59 deletions

View File

@ -112,7 +112,6 @@
<activity android:name=".activities.tageditor.AlbumTagEditorActivity" /> <activity android:name=".activities.tageditor.AlbumTagEditorActivity" />
<activity android:name=".activities.tageditor.SongTagEditorActivity" /> <activity android:name=".activities.tageditor.SongTagEditorActivity" />
<activity android:name=".activities.LyricsActivity" /> <activity android:name=".activities.LyricsActivity" />
<activity android:name=".activities.UserInfoActivity" />
<activity android:name=".activities.SupportDevelopmentActivity" /> <activity android:name=".activities.SupportDevelopmentActivity" />
<activity android:name=".activities.LicenseActivity" /> <activity android:name=".activities.LicenseActivity" />
<activity android:name=".activities.PurchaseActivity" /> <activity android:name=".activities.PurchaseActivity" />

View File

@ -12,25 +12,33 @@
* See the GNU General Public License for more details. * 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.app.Activity
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import android.view.LayoutInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast 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.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialValueHelper import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.retromusic.Constants.USER_BANNER import code.name.monkey.retromusic.Constants.USER_BANNER
import code.name.monkey.retromusic.Constants.USER_PROFILE import code.name.monkey.retromusic.Constants.USER_PROFILE
import code.name.monkey.retromusic.activities.base.AbsBaseActivity import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.databinding.ActivityUserInfoBinding import code.name.monkey.retromusic.databinding.FragmentUserInfoBinding
import code.name.monkey.retromusic.extensions.accentColor import code.name.monkey.retromusic.extensions.accentColor
import code.name.monkey.retromusic.extensions.applyToolbar 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.GlideApp
import code.name.monkey.retromusic.glide.RetroGlideExtension import code.name.monkey.retromusic.glide.RetroGlideExtension
import code.name.monkey.retromusic.util.ImageUtil 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.bumptech.glide.request.target.Target
import com.github.dhaval2404.imagepicker.ImagePicker import com.github.dhaval2404.imagepicker.ImagePicker
import com.github.dhaval2404.imagepicker.constant.ImageProvider import com.github.dhaval2404.imagepicker.constant.ImageProvider
import com.google.android.material.transition.MaterialContainerTransform
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -52,16 +61,28 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException 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?) { override fun onCreateView(
super.onCreate(savedInstanceState) inflater: LayoutInflater,
binding = ActivityUserInfoBinding.inflate(layoutInflater) container: ViewGroup?,
setContentView(binding.root) savedInstanceState: Bundle?
setStatusbarColorAuto() ): View {
setTaskDescriptionColorAuto() 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) applyToolbar(binding.toolbar)
binding.nameContainer.accentColor() binding.nameContainer.accentColor()
@ -78,20 +99,30 @@ class UserInfoActivity : AbsBaseActivity() {
binding.next.setOnClickListener { binding.next.setOnClickListener {
val nameString = binding.name.text.toString().trim { it <= ' ' } val nameString = binding.name.text.toString().trim { it <= ' ' }
if (TextUtils.isEmpty(nameString)) { 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 return@setOnClickListener
} }
PreferenceUtil.userName = nameString PreferenceUtil.userName = nameString
setResult(Activity.RESULT_OK) findNavController().navigateUp()
finish()
} }
val textColor = val textColor =
MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(accentColor())) MaterialValueHelper.getPrimaryTextColor(
requireContext(),
ColorUtil.isColorLight(accentColor())
)
binding.next.backgroundTintList = ColorStateList.valueOf(accentColor()) binding.next.backgroundTintList = ColorStateList.valueOf(accentColor())
binding.next.iconTint = ColorStateList.valueOf(textColor) binding.next.iconTint = ColorStateList.valueOf(textColor)
binding.next.setTextColor(textColor) binding.next.setTextColor(textColor)
loadProfile() loadProfile()
postponeEnterTransition()
view.doOnPreDraw {
startPostponedEnterTransition()
}
} }
private fun loadProfile() { private fun loadProfile() {
@ -110,7 +141,7 @@ class UserInfoActivity : AbsBaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) { if (item.itemId == android.R.id.home) {
onBackPressed() findNavController().navigateUp()
} }
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
@ -131,7 +162,7 @@ class UserInfoActivity : AbsBaseActivity() {
.start(PICK_IMAGE_REQUEST) .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) super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) { if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
val fileUri = data?.data val fileUri = data?.data
@ -140,9 +171,9 @@ class UserInfoActivity : AbsBaseActivity() {
val fileUri = data?.data val fileUri = data?.data
fileUri?.let { setAndSaveBannerImage(it) } fileUri?.let { setAndSaveBannerImage(it) }
} else if (resultCode == ImagePicker.RESULT_ERROR) { } 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 { } 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) { private fun saveImage(bitmap: Bitmap, fileName: String) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val appDir = applicationContext.filesDir val appDir = requireContext().filesDir
val file = File(appDir, fileName) val file = File(appDir, fileName)
var successful = false var successful = false
try { try {
@ -190,7 +221,7 @@ class UserInfoActivity : AbsBaseActivity() {
} }
if (successful) { if (successful) {
withContext(Dispatchers.Main) { 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) .into(binding.userImage)
} }
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
companion object { companion object {
private const val PICK_IMAGE_REQUEST = 9002 private const val PICK_IMAGE_REQUEST = 9002
private const val PICK_BANNER_REQUEST = 9004 private const val PICK_BANNER_REQUEST = 9004

View File

@ -14,7 +14,6 @@
*/ */
package code.name.monkey.retromusic.fragments.home package code.name.monkey.retromusic.fragments.home
import android.app.ActivityOptions
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
@ -24,6 +23,7 @@ import android.view.View
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.core.view.doOnPreDraw import androidx.core.view.doOnPreDraw
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.ThemeStore 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.fragments.base.AbsMainActivityFragment
import code.name.monkey.retromusic.glide.GlideApp import code.name.monkey.retromusic.glide.GlideApp
import code.name.monkey.retromusic.glide.RetroGlideExtension import code.name.monkey.retromusic.glide.RetroGlideExtension
import code.name.monkey.retromusic.util.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import com.google.android.gms.cast.framework.CastButtonFactory 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 import com.google.android.material.transition.MaterialSharedAxis
class HomeFragment : class HomeFragment :
@ -55,14 +54,35 @@ class HomeFragment :
_binding = getBinding(PreferenceUtil.isHomeBanner, view) _binding = getBinding(PreferenceUtil.isHomeBanner, view)
mainActivity.setSupportActionBar(binding.toolbar) mainActivity.setSupportActionBar(binding.toolbar)
mainActivity.supportActionBar?.title = null 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 { binding.bannerImage?.setOnClickListener {
val options = ActivityOptions.makeSceneTransitionAnimation( binding.userImage.transitionName = "userImage"
mainActivity, findNavController().navigate(
binding.userImage, R.id.user_info_fragment, null, null, FragmentNavigatorExtras(
getString(R.string.transition_user_image) binding.userImage to "userImage"
)
) )
NavigationUtil.goToUserInfo(requireActivity(), options) reenterTransition = null
} }
binding.lastAdded.setOnClickListener { binding.lastAdded.setOnClickListener {
@ -94,28 +114,12 @@ class HomeFragment :
} }
binding.userImage.setOnClickListener { binding.userImage.setOnClickListener {
val options = ActivityOptions.makeSceneTransitionAnimation( findNavController().navigate(
mainActivity, R.id.user_info_fragment, null, null, FragmentNavigatorExtras(
binding.userImage, binding.userImage to "user_image"
getString(R.string.transition_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 { private fun getBinding(homeBanner: Boolean, view: View): HomeBindingAdapter {

View File

@ -34,7 +34,7 @@ import code.name.monkey.retromusic.activities.LyricsActivity;
import code.name.monkey.retromusic.activities.PlayingQueueActivity; import code.name.monkey.retromusic.activities.PlayingQueueActivity;
import code.name.monkey.retromusic.activities.PurchaseActivity; import code.name.monkey.retromusic.activities.PurchaseActivity;
import code.name.monkey.retromusic.activities.SupportDevelopmentActivity; 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.WhatsNewActivity;
import code.name.monkey.retromusic.activities.bugreport.BugReportActivity; import code.name.monkey.retromusic.activities.bugreport.BugReportActivity;
import code.name.monkey.retromusic.helper.MusicPlayerRemote; import code.name.monkey.retromusic.helper.MusicPlayerRemote;
@ -70,8 +70,8 @@ public class NavigationUtil {
public static void goToUserInfo( public static void goToUserInfo(
@NonNull Activity activity, @NonNull ActivityOptions activityOptions) { @NonNull Activity activity, @NonNull ActivityOptions activityOptions) {
ActivityCompat.startActivity( ActivityCompat.startActivity(
activity, new Intent(activity, UserInfoActivity.class), activityOptions.toBundle()); activity, new Intent(activity, UserInfoFragment.class), activityOptions.toBundle());
} }
public static void gotoDriveMode(@NotNull final Activity activity) { public static void gotoDriveMode(@NotNull final Activity activity) {

View File

@ -16,13 +16,15 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:transitionGroup="true"> android:transitionGroup="true">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout" android:id="@+id/appBarLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:liftOnScroll="true"> app:liftOnScroll="true"
android:fitsSystemWindows="true">
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -33,7 +35,6 @@
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/colorSurface"
app:navigationIcon="@drawable/ic_search" app:navigationIcon="@drawable/ic_search"
app:popupTheme="?attr/toolbarPopupTheme" app:popupTheme="?attr/toolbarPopupTheme"
app:title="@null" app:title="@null"
@ -80,7 +81,8 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:retroCornerSize="21dp" app:retroCornerSize="21dp"
tools:srcCompat="@tools:sample/avatars" /> tools:srcCompat="@tools:sample/avatars"
android:transitionName="user_image" />
<com.google.android.material.textview.MaterialTextView <com.google.android.material.textview.MaterialTextView
android:id="@+id/text" android:id="@+id/text"

View File

@ -52,7 +52,7 @@
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:scaleType="centerCrop" android:scaleType="centerCrop"
android:transitionName="@string/transition_user_image" android:transitionName="user_image"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/bannerImage" app:layout_constraintTop_toBottomOf="@id/bannerImage"
app:retroCornerSize="36dp" app:retroCornerSize="36dp"
@ -92,7 +92,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:layout_margin="16dp" android:layout_marginHorizontal="16dp"
android:layout_marginBottom="70dp"
android:gravity="center" android:gravity="center"
android:text="@string/save" android:text="@string/save"
app:icon="@drawable/ic_save" app:icon="@drawable/ic_save"