Last active
April 29, 2020 10:19
-
-
Save binrebin/4d2e45c5b0aed659ddca04ff203b4efb to your computer and use it in GitHub Desktop.
basic profile fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imports ..... | |
class BasicProfileFragment : DaggerFragment() { | |
@Inject | |
lateinit var viewModelFactory: ViewModelProvider.Factory | |
private lateinit var basicProfileViewModel: BasicProfileViewModel | |
private var filterEventHasBeenHandled = false | |
companion object { | |
const val FILTER_EVENT_HANDLED = "filterEventHasBeenHandled" | |
} | |
override fun onSaveInstanceState(outState: Bundle) { | |
outState.run { putBoolean(FILTER_EVENT_HANDLED, filterEventHasBeenHandled) } | |
super.onSaveInstanceState(outState) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
filterEventHasBeenHandled = savedInstanceState?.getBoolean(FILTER_EVENT_HANDLED) ?: false | |
basicProfileViewModel = activityViewModel(viewModelFactory){ | |
observe(userDataState) { handleUserState(it) } | |
observe(submitEvent, ::handleProfileEvent) | |
observe(profileDataState) { handleDataState(it) } | |
} | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
return inflater.inflate(R.layout.fragment_basic_profile, container, false) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
if (!filterEventHasBeenHandled) { | |
val basicProfileModel = | |
arguments?.getParcelable("basicProfileModel") ?: BasicProfileModel.empty() | |
basicProfileViewModel.setProfile(basicProfileModel) | |
} | |
initializeView() | |
if (basicProfileViewModel.obtainCurrentUser() == null) { | |
val uid = Prefs.getString("USER_OID", "0") | |
loadUser(uid) | |
filterEventHasBeenHandled = true | |
} | |
} | |
private fun initializeView() { | |
(activity as AppCompatActivity).setSupportActionBar(toolbar) | |
(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true) | |
toolbar.setPadding(0, (activity as MainActivity).getStatusBarHeight(), 0, 0) | |
genderStrip.setOnClickListener { | |
it.hideKeyboard() | |
navigate(R.id.action_basicProfileFragment_to_gendersFragment) | |
} | |
private fun handleProfileEvent(submitEvent: Event<BasicProfileModel>?) { | |
if (filterEventHasBeenHandled && submitEvent?.hasBeenHandled == true) { | |
submitEvent.peekContent { renderProfileFields(it) } | |
} else { | |
submitEvent?.getContentIfNotHandled { | |
renderProfileFields(it) | |
filterEventHasBeenHandled = true | |
} | |
} | |
} | |
private fun handleUserState(userDataState: DataState<BasicProfileModel>?) { .... } | |
private fun handleDataState(profileDataState: DataState<BasicProfileModel>?) {....} | |
private fun handleError(exception: Exception) {.... } | |
private fun applySubmit(basicProfileModel: BasicProfileModel){ | |
basicProfileViewModel.submitProfile(basicProfileModel) | |
} | |
private fun handleUserError(exception: Exception) {....} | |
private fun updatedFields(basicProfileModel: BasicProfileModel){ | |
context?.onSuccess(R.string.updation_success) | |
renderProfileFields(basicProfileModel) | |
} | |
private fun renderProfileFields(basicProfileModel: BasicProfileModel) { | |
renderGender(basicProfileModel.genderModel) | |
} | |
private fun renderGender(genderModel: GenderModel?) { | |
genderModel?.let { | |
tvGender.visible() | |
tvGender.text = it.gender | |
} ?: tvGender.gone() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment