스팀 앱 - 개발 완료: 프로필 화면 담당 프래그먼트

개발 완료: 프로필 화면 담당 프래그먼트

2024. 01. 17 (수) | Written by @dorian-mobileapp

시작하며...

프로필 화면 담당 프래그먼트도 1차 완료되어 코드를 공유하고자 합니다.


ProfileFragment 클래스

package lee.dorian.steem_ui.ui.profile

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import lee.dorian.steem_domain.model.SteemitProfile
import lee.dorian.steem_ui.MainViewModel
import lee.dorian.steem_ui.R
import lee.dorian.steem_ui.databinding.FragmentProfileBinding
import lee.dorian.steem_ui.ext.load
import lee.dorian.steem_ui.model.State
import lee.dorian.steem_ui.ui.base.BaseFragment

class ProfileFragment : BaseFragment<FragmentProfileBinding, ProfileViewModel>(R.layout.fragment_profile) {

    override val viewModel by lazy {
        ViewModelProvider(this).get(ProfileViewModel::class.java)
    }

    val activityViewModel by lazy {
        ViewModelProvider(requireActivity()).get(MainViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState).apply {
            binding.viewModel = viewModel
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        activityViewModel.currentAccount.removeObservers(viewLifecycleOwner)
        activityViewModel.currentAccount.observe(viewLifecycleOwner, currentAccountObserver)

        viewLifecycleOwner.lifecycleScope.launch {
            viewModel.profileState.collect(profileStateCollector)
        }
    }

    private val currentAccountObserver = Observer<String> {
        when {
            it.isEmpty() -> showEmpty()
            else -> viewLifecycleOwner.lifecycleScope.launch {
                viewModel.readSteemitProfile(it)
            }
        }
    }

    private val profileStateCollector = FlowCollector<State<SteemitProfile>> { newState ->
        when (newState) {
            is State.Empty -> showEmpty()
            is State.Loading -> showLoading()
            is State.Success -> showProfile(newState.data)
            is State.Error, is State.Failure -> {}
        }
    }

    private fun showEmpty() {
        binding.includeInputAccount.layoutInputAccount.visibility = View.VISIBLE
        binding.includeLoading.layoutLoading.visibility = View.GONE
        binding.scrollProfile.visibility = View.GONE
    }

    private fun showLoading() {
        binding.includeInputAccount.layoutInputAccount.visibility = View.GONE
        binding.includeLoading.layoutLoading.visibility = View.VISIBLE
        binding.scrollProfile.visibility = View.GONE
    }

    private fun showProfile(profile: SteemitProfile) {
        binding.includeInputAccount.layoutInputAccount.visibility = View.GONE
        binding.includeLoading.layoutLoading.visibility = View.GONE
        binding.scrollProfile.visibility = View.VISIBLE

        binding.includeProfile.apply {
            imageProfileBackground.load(profile.coverImageURL, null)
            imageProfile.load("https://steemitimages.com/u/${profile.account}/avatar/small")
            textAccount.text = "@${profile.account}"
            textName.text = profile.name
            textFollowers.text = "${profile.followerCount} Followers"
            textFollowing.text = "${profile.followingCount} Following"
            textAbout.text = profile.about
            textLocation.text = "📍 ${profile.location}"
            textWebLink.text = "🔗 ${profile.website}"
        }
    }

}

Layout provided by Steemit Enhancer hommage by ayogom


Posted through the ECblog app (https://blog.etain.club)


Posted through the ECblog app (https://blog.etain.club)
Sort:  
 6 months ago 

[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.

안녕하세요.
SteemitKorea팀에서 제공하는 'steemit-enhancer'를 사용해 주셔서 감사합니다. 개선 사항이 있으면 언제나 저에게 연락을 주시면 되고, 관심이 있으신 분들은 https://cafe.naver.com/steemitkorea/425 에서 받아보실 수 있습니다. 사용시 @응원해 가 포함이 되며, 악용시에는 모든 서비스에서 제외될 수 있음을 알려드립니다.


안녕하세요.
이 글은 SteemitKorea팀(@ayogom)님께서 저자이신 @dorian-mobileapp님을 응원하는 글입니다.
소정의 보팅을 해드렸습니다 ^^ 항상 좋은글 부탁드립니다
SteemitKorea팀에서는 보다 즐거운 steemit 생활을 위해 노력하고 있습니다.
이 글은 다음날 다시 한번 포스팅을 통해 소개 될 예정입니다. 감사합니다!

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.030
BTC 56966.72
ETH 3008.71
USDT 1.00
SBD 2.19