1
1
package com.example.studentportal.profile.ui.fragment
2
2
3
3
import android.content.SharedPreferences
4
+ import android.os.Bundle
4
5
import android.view.LayoutInflater
5
6
import android.view.ViewGroup
7
+ import android.widget.Toast
6
8
import androidx.compose.foundation.layout.fillMaxSize
7
- import androidx.compose.runtime.getValue
8
9
import androidx.compose.ui.Modifier
9
10
import androidx.core.os.bundleOf
10
11
import androidx.fragment.app.viewModels
12
+ import androidx.lifecycle.ViewModelProvider
11
13
import com.example.studentportal.R
12
14
import com.example.studentportal.auth.ui.showLogoutDialog
13
15
import com.example.studentportal.common.di.clearAuthenticatedUserData
14
16
import com.example.studentportal.common.di.getUserId
15
17
import com.example.studentportal.common.di.koin
16
18
import com.example.studentportal.common.ui.fragment.BaseFragment
19
+ import com.example.studentportal.common.ui.model.data
20
+ import com.example.studentportal.common.ui.showBaseDialogFragment
17
21
import com.example.studentportal.course.ui.model.UserType
18
22
import com.example.studentportal.databinding.FragmentProfileBinding
19
23
import com.example.studentportal.home.ui.activity.HomeActivity
20
24
import com.example.studentportal.profile.ui.layout.ProfileLayout
25
+ import com.example.studentportal.profile.ui.model.UserUiModel
21
26
import com.example.studentportal.profile.ui.viewModel.UserProfileViewModel
22
27
23
- class ProfileFragment : BaseFragment <FragmentProfileBinding >(TAG ) {
28
+ class ProfileFragment (
29
+ viewModelFactory : ViewModelProvider .Factory = UserProfileViewModel .UserProfileViewModelFactory
30
+ ) : BaseFragment<FragmentProfileBinding>(TAG ) {
24
31
internal val viewModel by viewModels<UserProfileViewModel > {
25
- UserProfileViewModel . UserProfileViewModelFactory
32
+ viewModelFactory
26
33
}
27
34
28
35
val userId: String
@@ -48,12 +55,37 @@ class ProfileFragment : BaseFragment<FragmentProfileBinding>(TAG) {
48
55
koin.get<SharedPreferences >().clearAuthenticatedUserData() // Clear JwtToken
49
56
requireActivity().finish()
50
57
}
58
+ },
59
+ onEditClicked = {
60
+ viewModel.uiResultLiveData.value.data()?.let {
61
+ childFragmentManager.showBaseDialogFragment(
62
+ EditProfileFragment .newInstance(it)
63
+ )
64
+ }
51
65
}
52
66
)
53
67
}
54
68
return binding
55
69
}
56
70
71
+ override fun onCreate (savedInstanceState : Bundle ? ) {
72
+ super .onCreate(savedInstanceState)
73
+ childFragmentManager.setFragmentResultListener(
74
+ EditProfileFragment .KEY_FRAGMENT_RESULT ,
75
+ this
76
+ ) { key, bundle ->
77
+ bundle.getParcelable<UserUiModel >(EditProfileFragment .KEY_USER )?.let { userUiModel ->
78
+ viewModel.updateProfile(userUiModel, onError = {
79
+ Toast .makeText(
80
+ requireContext(),
81
+ it.error?.message.orEmpty(),
82
+ Toast .LENGTH_LONG
83
+ ).show()
84
+ })
85
+ }
86
+ }
87
+ }
88
+
57
89
override fun menuItem () = R .id.nav_profile
58
90
59
91
companion object {
0 commit comments