Skip to content

Commit

Permalink
user fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
yueeng committed Jan 18, 2024
1 parent 19d9158 commit 613c896
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
68 changes: 29 additions & 39 deletions app/src/main/java/com/github/yueeng/moebooru/UserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -51,41 +50,17 @@ class UserViewModelFactory(owner: SavedStateRegistryOwner, private val args: Bun
override fun <T : ViewModel> create(key: String, modelClass: Class<T>, 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
menu.findItem(R.id.userLogin).isVisible = !auth
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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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<View>(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
Expand All @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_avatar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,5v14L5,19L5,5h14m0,-2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM14.14,11.86l-3,3.87L9,13.14 6,17h12l-3.86,-5.14z" />
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:layout_height="wrap_content">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/user.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/userAvatar"
android:icon="@drawable/ic_avatar"
android:title="@string/user_avatar"
app:showAsAction="ifRoom" />
<item
android:id="@+id/userLogin"
android:icon="@drawable/ic_reg"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<string name="settings_safe_turn_off">安全</string>
<string name="settings_safe_turn_on">全部</string>

<string name="user_avatar">头像</string>
<string name="user_change_email">变更邮箱</string>
<string name="user_change_pwd">变更密码</string>
<string name="user_email">邮箱</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<string name="settings_safe_turn_off">Safe</string>
<string name="settings_safe_turn_on">All</string>

<string name="user_avatar">Avatar</string>
<string name="user_change_email">Change Email</string>
<string name="user_change_pwd">Change Password</string>
<string name="user_email">Email</string>
Expand Down

0 comments on commit 613c896

Please sign in to comment.