Skip to content

Commit

Permalink
Implemented profile picture changing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
fin1te committed Jul 25, 2023
1 parent 1777897 commit cec9055
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 23 deletions.
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!-- Optional: only required for FILE payloads -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<application
android:allowBackup="true"
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/finite/nearbuddy/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ class MainActivity : AppCompatActivity() {
val profilePic = sharedPreferences.getString("profileImage", "")

val profilePicByteArray = Base64.decode(profilePic, Base64.DEFAULT)
val profilePicBitmap =
BitmapFactory.decodeByteArray(profilePicByteArray, 0, profilePicByteArray.size)

Log.d("CheckBitmap", "${profilePicBitmap == null}")
Log.d("CheckByteArray", "${profilePicByteArray == null}")
// val profilePicBitmap =
// BitmapFactory.decodeByteArray(profilePicByteArray, 0, profilePicByteArray.size)
//
// Log.d("CheckBitmap", "${profilePicBitmap == null}")
// Log.d("CheckByteArray", "${profilePicByteArray == null}")

viewModel.user1 = UserProfile(name, dob, gender, interests, profilePicByteArray)

Log.d("profileUpdated to vm from main", viewModel.user1.toString())
Log.d("profileUpdated to vm from main", viewModel.user1.profilePic.size.toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ChatAdapter(

fun bind(chatMessage: ChatMessage) {
val u2Bitmap = BitmapFactory.decodeByteArray(
ncvm.user1.profilePic,
ncvm.user2.value!!.profilePic,
0,
ncvm.user2.value!!.profilePic.size
)
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/finite/nearbuddy/ui/ConnectionViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class ConnectionViewModel(application: Application) : AndroidViewModel(applicati
private val _chatMessages = MutableLiveData<MutableList<ChatMessage>>()
val chatMessages: LiveData<MutableList<ChatMessage>> = _chatMessages

private val _isConnected = MutableLiveData(false)
val _isConnected = MutableLiveData(false)
val isConnected: LiveData<Boolean> = _isConnected


// Function to initialize the chatMessages list with an empty list
init {
_chatMessages.value = mutableListOf()
Expand Down Expand Up @@ -185,10 +186,14 @@ class ConnectionViewModel(application: Application) : AndroidViewModel(applicati
Log.d("DebugData", "Reaching OK")
// endpoint id of the connected device
currentEndPoint = endpointId
_isConnected.value = true
//_isConnected.value = true

val data = Payload.fromBytes(getUserProfileByteArray())
Nearby.getConnectionsClient(context).sendPayload(endpointId, data)
Nearby.getConnectionsClient(context).sendPayload(endpointId, data).addOnSuccessListener {
Log.d("DebugData", "onConnectionResult: success")
}.addOnFailureListener { e ->
Log.d("DebugData", "onConnectionResult: fail ${e.toString()}")
}
Nearby.getConnectionsClient(context).stopDiscovery()
isDiscovering = false
}
Expand Down
89 changes: 78 additions & 11 deletions app/src/main/java/com/finite/nearbuddy/ui/EditProfileFragment.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.finite.nearbuddy.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.provider.MediaStore
import android.util.Base64
import android.util.Log
import android.view.LayoutInflater
Expand Down Expand Up @@ -49,14 +53,10 @@ class EditProfileFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val sharedPreferences =
requireActivity().getSharedPreferences("profileDataPreference", Context.MODE_PRIVATE)
val sharedPreferences = requireActivity().getSharedPreferences("profileDataPreference", Context.MODE_PRIVATE)

// Updating the profile data with the data stored in shared preferences
if (sharedPreferences.contains("name") && sharedPreferences.contains("gender") && sharedPreferences.contains(
"dob"
)
) {
if (sharedPreferences.contains("name") && sharedPreferences.contains("gender") && sharedPreferences.contains("dob")) {
binding?.editTextName?.setText(sharedPreferences.getString("name", ""))
binding?.textViewDOB?.text = sharedPreferences.getString("dob", "")
binding?.aboutEditText?.editText?.setText(sharedPreferences.getString("about", ""))
Expand All @@ -71,6 +71,14 @@ class EditProfileFragment : Fragment() {
)
}

// set the profile image
val profileImage = sharedPreferences.getString("profileImage", "")
if (profileImage != "") {
val decodedString: ByteArray = Base64.decode(profileImage, Base64.DEFAULT)
val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
binding?.profileImage?.setImageBitmap(decodedByte)
}

// when food is selected, select the food from drop down
when (sharedPreferences.getString("interestFood", "")) {
"Food | 1" -> (binding?.foodDropDown?.editText as? MaterialAutoCompleteTextView)?.setText(
Expand Down Expand Up @@ -193,6 +201,7 @@ class EditProfileFragment : Fragment() {
binding!!.editProfileTitle.text = "Complete Profile"
}

// set the drop down items
val genderList = arrayOf("Male", "Female")
(binding!!.genderMenu.editText as? MaterialAutoCompleteTextView)?.setSimpleItems(genderList)

Expand Down Expand Up @@ -224,12 +233,20 @@ class EditProfileFragment : Fragment() {
programmingList
)

val moviesList =
arrayOf("", "Movies | 1", "Movies | 2", "Movies | 3", "Movies | 4", "Movies | 5")
val moviesList = arrayOf("", "Movies | 1", "Movies | 2", "Movies | 3", "Movies | 4", "Movies | 5")
(binding!!.moviesDropDown.editText as? MaterialAutoCompleteTextView)?.setSimpleItems(
moviesList
)

binding?.profileImage?.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
// start the activity with the gallery intent and set the selected image to the profile image
startActivityForResult(intent, 0)
}




binding?.saveChanges?.setOnClickListener {

Expand Down Expand Up @@ -269,9 +286,10 @@ class EditProfileFragment : Fragment() {
}

// Step 3: Compress the Bitmap to a byte array (Choose the format as needed)
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val byteArray: ByteArray = outputStream.toByteArray()
//val outputStream = ByteArrayOutputStream()
//bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
//val byteArray: ByteArray = outputStream.toByteArray()
val byteArray = compressImageTo300KB(bitmap)


// Save the ByteArray to SharedPreferences
Expand Down Expand Up @@ -327,4 +345,53 @@ class EditProfileFragment : Fragment() {
}

}

private fun compressImageTo300KB(bitmap: Bitmap, quality: Int = 100, attempt: Int = 1): ByteArray {
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
val byteArray = outputStream.toByteArray()

if (byteArray.size <= 300 * 1024) {
// Image is under 300KB, return the compressed byteArray
return byteArray
} else {
// Image size is still larger than 300KB
// Reduce the quality by 10% and try again
val newQuality = (quality - 10)
if (newQuality > 0 && attempt < 10) {
return compressImageTo300KB(bitmap, newQuality, attempt + 1)
} else {
// Image cannot be compressed further or max attempts reached
// Return null or take other actions based on your requirements
return byteArray
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
val selectedImage = data.data
val bitmap = MediaStore.Images.Media.getBitmap(requireActivity().contentResolver, selectedImage)
binding?.profileImage?.setImageBitmap(bitmap)

// val outputStream = ByteArrayOutputStream()
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
// val byteArray: ByteArray = outputStream.toByteArray()
//
// val sharedPreferences = requireActivity().getSharedPreferences("profileDataPreference", Context.MODE_PRIVATE)
// val editor = sharedPreferences.edit()
//
// editor.putString("profileImage", Base64.encodeToString(byteArray, Base64.DEFAULT))
//
// editor.apply()

// save the image to the user 1 view model
// val tempUser1 = UserProfile(ncvm.user1.name,ncvm.user1.dateOfBirth,ncvm.user1.gender,ncvm.user1.interests,byteArray)
// ncvm.user1 = tempUser1
}
}


}
10 changes: 10 additions & 0 deletions app/src/main/java/com/finite/nearbuddy/ui/ProfileFragment.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.finite.nearbuddy.ui

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.util.Base64
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -49,6 +51,14 @@ class ProfileFragment : Fragment() {
val interestProgramming = sharedPreferences.getString("interestProgramming", "")
val interestMovies = sharedPreferences.getString("interestMovies", "")

// set the profile image
val profileImage = sharedPreferences.getString("profileImage", "")
if (profileImage != "") {
val decodedString: ByteArray = Base64.decode(profileImage, Base64.DEFAULT)
val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
binding?.profileImage?.setImageBitmap(decodedByte)
}

binding?.textViewName?.text = name
binding?.textViewGender?.text = gender
binding?.textViewDOB?.text = dob
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/finite/nearbuddy/ui/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ class SearchFragment : Fragment() {

ncvm.user2.observe(viewLifecycleOwner) { user2 ->
if (user2.name.isNotBlank()) {
Toast.makeText(context, "Connected with ${user2.name}", Toast.LENGTH_SHORT).show()
// if(!ncvm.isConnectionActive) {
// Toast.makeText(context, "Connected with ${user2.name}", Toast.LENGTH_SHORT).show()
// ncvm.isConnectionActive = true
// }
if (!ncvm._isConnected.value!!) {
Toast.makeText(context, "Connected with ${user2.name}", Toast.LENGTH_SHORT).show()
ncvm._isConnected.value = true
}
binding?.searchLottie?.pauseAnimation()
binding?.discoverBtn?.background?.setTint(resources.getColor(R.color.discoverBtn))
binding?.discoverBtn?.radius = 45.0F
Expand Down

0 comments on commit cec9055

Please sign in to comment.