Selaa lähdekoodia

user account profile in progress

dola 1 vuosi sitten
vanhempi
commit
bd9f461db5

+ 6 - 0
app/src/main/java/com/sambath/kunkhmer/app/MainActivity.kt

@@ -7,12 +7,16 @@ import androidx.navigation.ui.AppBarConfiguration
 import androidx.navigation.ui.setupWithNavController
 import com.google.android.material.bottomnavigation.BottomNavigationView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.remote.User
 import com.sambath.kunkhmer.screen.fighter.FighterFragment
+import com.sambath.kunkhmer.util.Const
+import com.sambath.kunkhmer.util.ModelPreferencesManager
 import kotlinx.android.synthetic.main.app_bar_boxing.iv_account
 
 class MainActivity : AppCompatActivity(), FighterFragment.TabSelectionListener {
 
     lateinit var navView: BottomNavigationView
+    private lateinit var user: User
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -36,6 +40,8 @@ class MainActivity : AppCompatActivity(), FighterFragment.TabSelectionListener {
         iv_account.setOnClickListener{
             findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.navigation_account)
         }
+
+        user = ModelPreferencesManager.get<User>(Const.USER_KEY)!!
     }
 
     override fun onTabSelected(tabIndex: Int) {

+ 104 - 0
app/src/main/java/com/sambath/kunkhmer/extension/CurrentUserDataModel.kt

@@ -0,0 +1,104 @@
+package com.sambath.kunkhmer.extension
+
+import com.sambath.kunkhmer.remote.NewsDetail
+import com.sambath.kunkhmer.remote.NewsDetailData
+import com.sambath.kunkhmer.remote.ResponseMessage
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class CurrentUserResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: CurrentUserData?
+)
+
+@JsonClass(generateAdapter = true)
+data class CurrentUserData(
+    @Json(name = "object")
+    val obj: CurrentUser,
+)
+
+@JsonClass(generateAdapter = true)
+data class CurrentUser(
+    @Json(name = "_id")
+    val id: String,
+
+    @Json(name = "socket_id")
+    val socket_id: String?,
+
+    @Json(name = "user_name")
+    val user_name: String,
+
+    @Json(name = "fist_name")
+    val fist_name: String,
+
+    @Json(name = "last_name")
+    val last_name: String,
+
+    @Json(name = "gender")
+    val gender: String,
+
+    @Json(name = "password")
+    val password: String,
+
+    @Json(name = "register_by")
+    val register_by: String,
+
+    @Json(name = "email_or_phone")
+    val email_or_phone: String,
+
+    @Json(name = "role")
+    val role: String,
+
+    @Json(name = "firebase_token")
+    val firebase_token: String,
+
+    @Json(name = "device_id")
+    val device_id: String,
+
+    @Json(name = "createdAt")
+    val createdAt: String,
+
+    @Json(name = "updatedAt")
+    val updatedAt: String,
+
+    @Json(name = "__v")
+    val _v: Int,
+
+    @Json(name = "file_id")
+    val file_id: FileId,
+)
+
+@JsonClass(generateAdapter = true)
+data class FileId(
+    @Json(name = "fieldname")
+    val fieldname: String,
+    @Json(name = "originalname")
+    val originalname: String,
+    @Json(name = "encoding")
+    val encoding: String,
+    @Json(name = "mimetype")
+    val mimetype: String,
+    @Json(name = "id")
+    val id: String,
+    @Json(name = "filename")
+    val filename: String,
+    @Json(name = "metadata")
+    val metadata: String?,
+    @Json(name = "bucketName")
+    val bucketName: String,
+    @Json(name = "chunkSize")
+    val chunkSize: Int,
+    @Json(name = "size")
+    val size: Int,
+    @Json(name = "md5")
+    val md5: String,
+    @Json(name = "uploadDate")
+    val uploadDate: String,
+    @Json(name = "contentType")
+    val contentType: String,
+)

+ 4 - 0
app/src/main/java/com/sambath/kunkhmer/remote/service/ApiService.kt

@@ -1,5 +1,6 @@
 package com.sambath.kunkhmer.remote.service
 
+import com.sambath.kunkhmer.extension.CurrentUserResponse
 import com.sambath.kunkhmer.remote.*
 import io.reactivex.Single
 import okhttp3.ResponseBody
@@ -34,6 +35,9 @@ interface ApiService {
 
     @GET("/v1/new/get/{newsId}")
     fun getNewsDetail(@Path("newsId") newsId: String): Single<NewsDetailResponse>
+
+    @POST("/v1/users/get")
+    fun getCurrentUser(): Single<CurrentUserResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 51 - 0
app/src/main/java/com/sambath/kunkhmer/screen/account/AccountFragment.kt

@@ -6,10 +6,23 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 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() {
     private var _root: View? = null
     private val binding get() = _root!!
+    private lateinit var user: User
+    private lateinit var accountViewModel: AccountViewModel
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -18,6 +31,44 @@ class AccountFragment : Fragment() {
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         // Inflate the layout for this fragment
         _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
     }
+
+    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)
+    }
 }

+ 49 - 0
app/src/main/java/com/sambath/kunkhmer/screen/account/AccountViewModel.kt

@@ -0,0 +1,49 @@
+package com.sambath.kunkhmer.screen.account
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import com.sambath.kunkhmer.app.getErrorCode
+import com.sambath.kunkhmer.base.BaseViewModel
+import com.sambath.kunkhmer.extension.CurrentUser
+import com.sambath.kunkhmer.remote.service.ApiService
+import com.sambath.kunkhmer.util.PrefHelper
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+import java.util.concurrent.TimeUnit
+
+class AccountViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+    private val _state = MutableLiveData<AccountViewState>(AccountViewState())
+    val state: LiveData<AccountViewState> = _state
+    private fun prev() = _state.value!!
+
+    data class AccountViewState(
+        val initial: Boolean = false,
+        val isProgress: Boolean = false,
+        val isLoginSuccess: Boolean = false,
+        val error: String? = null,
+        val currentUser: CurrentUser? = null,
+    )
+
+    fun getCurrentUser() {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.getCurrentUser()
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, currentUser = it.data!!.obj)
+                    } else {
+                        _state.value =
+                            prev().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
+}

+ 1 - 1
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterViewModel.kt

@@ -15,7 +15,7 @@ import io.reactivex.schedulers.Schedulers
 import okhttp3.ResponseBody
 import java.util.concurrent.TimeUnit
 
-class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+    class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
     private val _state = MutableLiveData<FighterViewState>(FighterViewState())
     val state: LiveData<FighterViewState> = _state
     private fun prev() = _state.value!!

+ 3 - 2
app/src/main/res/layout/fragment_account.xml

@@ -93,7 +93,6 @@
                 android:layout_height="wrap_content"
                 android:layout_weight="0.5"
                 android:gravity="end"
-                android:text="@string/account"
                 android:textColor="@color/color_white" />
 
             <ImageView
@@ -120,6 +119,7 @@
                 android:textColor="@color/color_white" />
 
             <TextView
+                android:id="@+id/email_tv"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="0.5"
@@ -151,6 +151,7 @@
                 android:textColor="@color/color_white" />
 
             <TextView
+                android:id="@+id/country_tv"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="0.5"
@@ -186,7 +187,7 @@
                 android:layout_height="wrap_content"
                 android:layout_weight="0.5"
                 android:gravity="end"
-                android:text="@string/Language"
+                android:text="@string/English"
                 android:textColor="@color/color_white" />
 
             <ImageView

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -97,4 +97,5 @@
     <string name="privacy_policy">Privacy Policy</string>
     <string name="copyright_text"><![CDATA[Copyright 2023 HAHA SUPER FIGHT & GYM]]></string>
     <string name="member_since">Member since</string>
+    <string name="English">English</string>
 </resources>