|
@@ -6,10 +6,23 @@ import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.view.ViewGroup
|
|
import com.sambath.kunkhmer.R
|
|
import com.sambath.kunkhmer.R
|
|
|
|
+import com.sambath.kunkhmer.app.App
|
|
|
|
+import com.sambath.kunkhmer.remote.User
|
|
|
|
+import com.sambath.kunkhmer.util.Const
|
|
|
|
+import com.sambath.kunkhmer.util.ModelPreferencesManager
|
|
|
|
+import kotlinx.android.synthetic.main.fragment_account.view.country_tv
|
|
|
|
+import kotlinx.android.synthetic.main.fragment_account.view.email_tv
|
|
|
|
+import kotlinx.android.synthetic.main.fragment_account.view.member_tv
|
|
|
|
+import kotlinx.android.synthetic.main.fragment_account.view.username_tv
|
|
|
|
+import java.text.SimpleDateFormat
|
|
|
|
+import java.util.Locale
|
|
|
|
+import java.util.TimeZone
|
|
|
|
|
|
class AccountFragment : Fragment() {
|
|
class AccountFragment : Fragment() {
|
|
private var _root: View? = null
|
|
private var _root: View? = null
|
|
private val binding get() = _root!!
|
|
private val binding get() = _root!!
|
|
|
|
+ private lateinit var user: User
|
|
|
|
+ private lateinit var accountViewModel: AccountViewModel
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
super.onCreate(savedInstanceState)
|
|
@@ -18,6 +31,44 @@ class AccountFragment : Fragment() {
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
// Inflate the layout for this fragment
|
|
// Inflate the layout for this fragment
|
|
_root = inflater.inflate(R.layout.fragment_account, container, false)
|
|
_root = inflater.inflate(R.layout.fragment_account, container, false)
|
|
|
|
+ user = ModelPreferencesManager.get<User>(Const.USER_KEY)!!
|
|
|
|
+
|
|
|
|
+ binding.username_tv.text = user.userName
|
|
|
|
+ accountViewModel = AccountViewModel(App.injectApiService(), App.injectPrefHelper())
|
|
|
|
+ accountViewModel.getCurrentUser()
|
|
|
|
+ accountViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
|
|
|
|
+ render(it)
|
|
|
|
+ })
|
|
|
|
+
|
|
return _root
|
|
return _root
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private fun render(it: AccountViewModel.AccountViewState) {
|
|
|
|
+ if(it.isLoginSuccess){
|
|
|
|
+ if(it.currentUser != null) {
|
|
|
|
+ val data = it.currentUser
|
|
|
|
+
|
|
|
|
+ binding.member_tv.text = parseAndFormatDate(data.createdAt)
|
|
|
|
+ binding.email_tv.text = data.email_or_phone
|
|
|
|
+ binding.country_tv.text = "Cambodia"
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun parseAndFormatDate(dateString: String): String {
|
|
|
|
+ // Create a SimpleDateFormat for parsing the date string
|
|
|
|
+ val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
|
|
|
|
+ parser.timeZone = TimeZone.getTimeZone("UTC")
|
|
|
|
+
|
|
|
|
+ // Parse the date
|
|
|
|
+ val date = parser.parse(dateString)
|
|
|
|
+
|
|
|
|
+ // Create a SimpleDateFormat for formatting the date
|
|
|
|
+ val formatter = SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault())
|
|
|
|
+ formatter.timeZone = TimeZone.getDefault() // Use local timezone
|
|
|
|
+
|
|
|
|
+ // Format the date
|
|
|
|
+ return formatter.format(date)
|
|
|
|
+ }
|
|
}
|
|
}
|