From 7bb0d196ee60e94ac49b1c8ee9cf8bef49df74b0 Mon Sep 17 00:00:00 2001 From: slim-shaddy-29 <116194018+slim-shaddy-29@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:04:33 +0530 Subject: [PATCH] updating profile section with additional of interests On the profile page, I found a bug, so fixed it and I have add one new thing to add interests(extra curricular) of the person. Thank You --- .../PROFILE/UpdateProfileActivity.kt | 275 ++++++++++++++++++ .../free time chat/PROFILE/UserRepository.kt | 31 ++ .../PROFILE/update_profile_activity.xml | 195 +++++++++++++ 3 files changed, 501 insertions(+) create mode 100644 android/app/src/main/java/com/example/free time chat/PROFILE/UpdateProfileActivity.kt create mode 100644 android/app/src/main/java/com/example/free time chat/PROFILE/UserRepository.kt create mode 100644 android/app/src/main/java/com/example/free time chat/PROFILE/update_profile_activity.xml diff --git a/android/app/src/main/java/com/example/free time chat/PROFILE/UpdateProfileActivity.kt b/android/app/src/main/java/com/example/free time chat/PROFILE/UpdateProfileActivity.kt new file mode 100644 index 0000000..9523c34 --- /dev/null +++ b/android/app/src/main/java/com/example/free time chat/PROFILE/UpdateProfileActivity.kt @@ -0,0 +1,275 @@ +package com.codechef.ffds + +import android.annotation.SuppressLint +import android.app.Activity +import android.app.Dialog +import android.content.Intent +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Color +import android.graphics.ImageDecoder +import android.graphics.drawable.ColorDrawable +import android.net.Uri +import android.os.Bundle +import android.provider.MediaStore +import android.widget.TextView +import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.lifecycle.ViewModelProvider +import com.codechef.ffds.databinding.UpdateProfileActivityBinding +import com.cunoraz.tagview.Tag +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.gson.Gson +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.MultipartBody +import okhttp3.RequestBody.Companion.asRequestBody +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.ResponseBody +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import java.io.* +import java.util.* +import kotlin.collections.ArrayList + + +class UpdateProfileActivity : AppCompatActivity() { + + private lateinit var binding: UpdateProfileActivityBinding + lateinit var viewModel: UserViewModel + var user = Profile() + private val tags = ArrayList() + private var imageArray = byteArrayOf() + private var filePart: MultipartBody.Part? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = UpdateProfileActivityBinding.inflate(layoutInflater) + setContentView(binding.root) + + viewModel = ViewModelProvider( + this, + UserViewModelFactory(application) + ).get(UserViewModel::class.java) + setDefaultData() + + val resultLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + + if (result.resultCode == Activity.RESULT_OK) { + val data = result.data + val imageURI: Uri = data?.data!! + val bitmap = if (android.os.Build.VERSION.SDK_INT >= 29) + ImageDecoder.decodeBitmap( + ImageDecoder.createSource( + contentResolver, + imageURI + ) + ) + else + MediaStore.Images.Media.getBitmap(contentResolver, imageURI) + binding.dp.setImageBitmap(bitmap) + val stream = ByteArrayOutputStream() + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + imageArray = stream.toByteArray() + + val file = + File(getExternalFilesDir(null)?.absolutePath + File.separator + "profile.png") + file.createNewFile() + + val fos = FileOutputStream(file) + fos.write(imageArray) + fos.flush() + fos.close() + + filePart = MultipartBody.Part.createFormData( + "image", file.name, file.asRequestBody("image/png".toMediaTypeOrNull()) + ) + } + + } + + val resultLauncher2 = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + val data = result.data + val args = data?.getBundleExtra("bundle") + user = + user.copy(slot = args?.getSerializable("tableMap") as java.util.ArrayList>>) + } + + } + + binding.apply { + uploadDp.setOnClickListener { + val gallery = Intent() + gallery.type = "image/*" + gallery.action = Intent.ACTION_GET_CONTENT + + resultLauncher.launch(Intent.createChooser(gallery, "Select profile photo")) + } + + add.setOnClickListener { + handleTags(tags) + } + + tagView2.setOnTagDeleteListener { _, tag, position -> + tags.remove(tag.text) + tagView2.remove(position) + } + + uploadTimeTable.setOnClickListener { + val intent = Intent(this@UpdateProfileActivity, TimeTableActivity::class.java) + resultLauncher2.launch(intent) + } + + delete.setOnClickListener { + imageArray = byteArrayOf() + val bitmap = BitmapFactory.decodeResource(resources, R.drawable.profile_image) + dp.setImageBitmap(bitmap) + } + + saveProfile.setOnClickListener { + updateUser( + user.copy( + bio = bio.text.toString().trim(), + name = yourName.text.toString().trim(), + phone = phoneNoEdit.text.toString(), + expectations = tags, + ) + ) + } + } + } + + @SuppressLint("InflateParams") + private fun updateUser(user: Profile) { + val dialog = Dialog(this) + val view = layoutInflater.inflate(R.layout.loading_dialog, null) + view.findViewById(R.id.text).text = getString(R.string.saving) + dialog.setContentView(view) + dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + dialog.show() + val om = ObjectMapper() + val fields = om.writeValueAsString(user) + val body = fields.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull()) + Api.retrofitService.update(user.token, body) + ?.enqueue(object : Callback { + override fun onFailure(call: Call, t: Throwable) { + dialog.dismiss() + Toast.makeText(applicationContext, t.message, Toast.LENGTH_SHORT).show() + } + + override fun onResponse( + call: Call, + response: Response + ) { + if (response.message() == "OK") { + if (filePart != null) + uploadImage(user, dialog) + else { + dialog.dismiss() + viewModel.updateUser(user) + startActivity(Intent(baseContext, MainActivity::class.java)) + } + } else { + dialog.dismiss() + val gson = Gson() + val (_,message) = gson.fromJson(response.errorBody()?.charStream(), Error::class.java) + Toast.makeText( + applicationContext, + "Error ${response.code()}: $message", + Toast.LENGTH_SHORT + ).show() + } + } + }) + } + + private fun uploadImage(user: Profile, dialog: Dialog) { + Api.retrofitService.uploadImage(user.token, filePart!!)?.enqueue(object : Callback { + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText(applicationContext, "Failed: " + t.message, Toast.LENGTH_SHORT) + .show() + } + + override fun onResponse(call: Call, response: Response) { + dialog.dismiss() + if (response.message() == "OK") { + val image = response.body() + if (image != null) { + val profile = user.copy(userArray = imageArray.toList(), userImage = image) + viewModel.updateUser(profile) + } + startActivity(Intent(baseContext, MainActivity::class.java)) + } else { + val gson = Gson() + val (_,message) = gson.fromJson(response.errorBody()?.charStream(), Error::class.java) + Toast.makeText( + applicationContext, + "Error ${response.code()}: $message", + Toast.LENGTH_SHORT + ).show() + } + } + + + }) + } + + private fun setDefaultData() { + + binding.apply { + val prefs = getSharedPreferences("MY PREFS", MODE_PRIVATE) + viewModel.getUserData(prefs.getString("id", "")!!) + .observe(this@UpdateProfileActivity) { user -> + this@UpdateProfileActivity.user = user + for (tag in user.expectations) + tags.add(tag) + bio.setText(user.bio) + yourName.setText(user.name) + phoneNoEdit.text = user.phone + tagView2.setTagMargin(10f) + tagView2.setTextPaddingTop(2f) + tagView2.settextPaddingBottom(2f) + for (tag in tags) { + tagView2.addTag(getNewTag(tag)) + } + val bitmap = if (user.userArray.isNotEmpty()) + BitmapFactory.decodeByteArray(user.userArray.toByteArray(), 0, user.userArray.size) + else + BitmapFactory.decodeResource(resources, R.drawable.profile_image) + dp.setImageBitmap(bitmap) + } + } + } + + private fun handleTags(tags: ArrayList) { + binding.apply { + val tag = addTags.text.toString().trim() + if (tag.isNotEmpty()) { + if (!tags.contains(tag)) { + tagView2.addTag(getNewTag(tag)) + tags.add(tag) + } else + Toast.makeText( + this@UpdateProfileActivity, + "Tag already present", + Toast.LENGTH_SHORT + ).show() + } + addTags.text = null + } + } + + private fun getNewTag(text: String): Tag { + val tag = Tag(text) + tag.isDeletable = true + tag.layoutColor = + ContextCompat.getColor(this, R.color.colorPrimary) + + return tag + } +} diff --git a/android/app/src/main/java/com/example/free time chat/PROFILE/UserRepository.kt b/android/app/src/main/java/com/example/free time chat/PROFILE/UserRepository.kt new file mode 100644 index 0000000..3899eb9 --- /dev/null +++ b/android/app/src/main/java/com/example/free time chat/PROFILE/UserRepository.kt @@ -0,0 +1,31 @@ +package com.codechef.ffds + +import android.app.Application +import androidx.lifecycle.LiveData +import java.util.concurrent.Executors + +class UserRepository(application: Application) { + + private var userDatabase: UserDatabase = UserDatabase.getInstance(application) + private var userDao: UserDao = userDatabase.userDao() + + private val executorService = Executors.newSingleThreadExecutor() + + fun insertUser(vararg user: Profile) = executorService.execute { userDao.insertUser(*user) } + + fun updateUser(user: Profile) = executorService.execute { userDao.updateUser(user) } + + fun getUserData(_id: String) : LiveData = userDao.getUserData(_id) + + fun insertAllConversations(vararg conversation: Conversation) = executorService.execute { userDao.insertAllConversations(*conversation) } + + fun getAllConversations(): LiveData> = userDao.getAllConversations() + + fun insertAllMessages(vararg chat: Chat) = executorService.execute { userDao.insertAllMessages(*chat) } + + fun getAllMessages(conversationId: String): LiveData> = userDao.getAllMessages(conversationId) + + fun getLastMessage(conversationId: String): LiveData = userDao.getLastMessage(conversationId) + + fun clear() = executorService.execute { userDao.clear() } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/example/free time chat/PROFILE/update_profile_activity.xml b/android/app/src/main/java/com/example/free time chat/PROFILE/update_profile_activity.xml new file mode 100644 index 0000000..ca6d993 --- /dev/null +++ b/android/app/src/main/java/com/example/free time chat/PROFILE/update_profile_activity.xml @@ -0,0 +1,195 @@ + + + + + + + + + + + + + + + + + + + + + + + +