From 613c896af781f18982fb7e891841821606c41f6a Mon Sep 17 00:00:00 2001 From: ssiori Date: Thu, 18 Jan 2024 10:36:10 +0800 Subject: [PATCH] user fragment --- .../github/yueeng/moebooru/UserActivity.kt | 68 ++++++++----------- app/src/main/res/drawable/ic_avatar.xml | 10 +++ app/src/main/res/layout/fragment_user.xml | 1 + app/src/main/res/menu/user.xml | 5 ++ app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 app/src/main/res/drawable/ic_avatar.xml diff --git a/app/src/main/java/com/github/yueeng/moebooru/UserActivity.kt b/app/src/main/java/com/github/yueeng/moebooru/UserActivity.kt index 92995940..6d3edff8 100644 --- a/app/src/main/java/com/github/yueeng/moebooru/UserActivity.kt +++ b/app/src/main/java/com/github/yueeng/moebooru/UserActivity.kt @@ -31,8 +31,7 @@ class UserActivity : MoeActivity(R.layout.activity_container) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportFragmentManager.run { - val fragment = supportFragmentManager.findFragmentById(R.id.container) as? UserOtherFragment - ?: UserOtherFragment().apply { arguments = intent.extras } + val fragment = supportFragmentManager.findFragmentById(R.id.container) as? UserFragment ?: UserFragment().apply { arguments = intent.extras } beginTransaction().replace(R.id.container, fragment).commit() } } @@ -51,20 +50,6 @@ class UserViewModelFactory(owner: SavedStateRegistryOwner, private val args: Bun override fun create(key: String, modelClass: Class, handle: SavedStateHandle): T = UserViewModel(handle, args) as T } -class UserOtherFragment : UserFragment() { - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = super.onCreateView(inflater, container, savedInstanceState).also { view -> - val binding = FragmentUserBinding.bind(view) - (requireActivity() as AppCompatActivity).setSupportActionBar(binding.toolbar) - lifecycleScope.launch { - viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.CREATED) { - model.name.asFlow().mapNotNull { it }.distinctUntilChanged().collectLatest { name -> - requireActivity().title = name.toTitleCase() - } - } - } - } -} - class UserMineFragment : UserFragment() { private fun prepareOptionsMenu(menu: Menu) { val auth = OAuth.user.value != null && OAuth.user.value != 0 @@ -72,20 +57,10 @@ class UserMineFragment : UserFragment() { menu.findItem(R.id.userLogout).isVisible = auth } - private fun optionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { - R.id.login -> true.also { OAuth.login(this) } - R.id.register -> true.also { OAuth.register(this) } - R.id.reset -> true.also { OAuth.reset(this) } - R.id.logout -> true.also { OAuth.logout(this) } - R.id.changeEmail -> true.also { OAuth.changeEmail(this) } - R.id.changePwd -> true.also { OAuth.changePwd(this) } - else -> false - } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = super.onCreateView(inflater, container, savedInstanceState).also { view -> val binding = FragmentUserBinding.bind(view) prepareOptionsMenu(binding.toolbar.menu) - binding.toolbar.setOnMenuItemClickListener { optionsItemSelected(it) } lifecycleScope.launch { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.CREATED) { @@ -104,14 +79,6 @@ class UserMineFragment : UserFragment() { } } - lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.CREATED) { - model.name.asFlow().mapNotNull { it }.collectLatest { name -> - binding.toolbar.title = name.toTitleCase() - } - } - } - lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.CREATED) { OAuth.avatar.asFlow().filter { model.avatar.value != it }.collectLatest { @@ -143,14 +110,29 @@ open class UserFragment : Fragment() { private val adapter by lazy { ImageAdapter() } private val busy = MutableLiveData(false) + private fun optionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { + R.id.login -> true.also { OAuth.login(this) } + R.id.register -> true.also { OAuth.register(this) } + R.id.reset -> true.also { OAuth.reset(this) } + R.id.logout -> true.also { OAuth.logout(this) } + R.id.changeEmail -> true.also { OAuth.changeEmail(this) } + R.id.changePwd -> true.also { OAuth.changePwd(this) } + R.id.userAvatar -> true.also { + if ((model.avatar.value ?: 0) == 0) return@also + val binding = FragmentUserBinding.bind(requireView()) + val options = binding.toolbar.findViewById(item.itemId)?.let { ActivityOptions.makeSceneTransitionAnimation(requireActivity(), it, "shared_element_container") } + startActivity(Intent(requireContext(), PreviewActivity::class.java).putExtra("query", Q().id(model.avatar.value!!)), options?.toBundle()) + } + + else -> false + } + @OptIn(ExperimentalCoroutinesApi::class) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = FragmentUserBinding.inflate(inflater, container, false).also { binding -> - binding.toolbar.setNavigationOnClickListener { - if ((model.avatar.value ?: 0) == 0) return@setNavigationOnClickListener - val options = ActivityOptions.makeSceneTransitionAnimation(requireActivity(), it, "shared_element_container") - startActivity(Intent(requireContext(), PreviewActivity::class.java).putExtra("query", Q().id(model.avatar.value!!)), options.toBundle()) - } + binding.toolbar.menu.findItem(R.id.userLogin).isVisible = false + binding.toolbar.menu.findItem(R.id.userLogout).isVisible = false + binding.toolbar.setOnMenuItemClickListener { optionsItemSelected(it) } (binding.recycler.layoutManager as? FlexboxLayoutManager)?.flexDirection = FlexDirection.ROW adapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY binding.recycler.adapter = adapter @@ -175,6 +157,14 @@ open class UserFragment : Fragment() { } } } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.CREATED) { + model.name.asFlow().mapNotNull { it }.collectLatest { name -> + binding.collapsing.title = name.toTitleCase() + binding.toolbar.title = name.toTitleCase() + } + } + } lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.CREATED) { val user = model.user.asFlow().distinctUntilChanged() diff --git a/app/src/main/res/drawable/ic_avatar.xml b/app/src/main/res/drawable/ic_avatar.xml new file mode 100644 index 00000000..ca18629c --- /dev/null +++ b/app/src/main/res/drawable/ic_avatar.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_user.xml b/app/src/main/res/layout/fragment_user.xml index b852d940..ac5c3df7 100644 --- a/app/src/main/res/layout/fragment_user.xml +++ b/app/src/main/res/layout/fragment_user.xml @@ -12,6 +12,7 @@ android:layout_height="wrap_content"> diff --git a/app/src/main/res/menu/user.xml b/app/src/main/res/menu/user.xml index 01912d20..32ea427f 100644 --- a/app/src/main/res/menu/user.xml +++ b/app/src/main/res/menu/user.xml @@ -1,6 +1,11 @@ + 安全 全部 + 头像 变更邮箱 变更密码 邮箱 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 704f8598..cece8578 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -154,6 +154,7 @@ Safe All + Avatar Change Email Change Password Email