Skip to content

Commit

Permalink
Upgraded features
Browse files Browse the repository at this point in the history
Emojis, render images, fixed format
  • Loading branch information
simplekjl committed May 28, 2019
1 parent 32904a4 commit 00f8975
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
Expand All @@ -33,6 +34,7 @@ dependencies {
def lifecycle_version = "1.1.1"
def room_version = "1.1.1"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:multidex:1.0.3'
/** Design **/
implementation 'com.android.support:design:28.0.0'
/** Network **/
Expand All @@ -51,6 +53,8 @@ dependencies {
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-storage:17.0.0'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'com.google.firebase:firebase-config:17.0.0'
// ...
implementation 'com.firebaseui:firebase-ui-auth:5.0.0'
/** GLIDE **/
Expand Down
80 changes: 63 additions & 17 deletions app/src/main/java/com/simplekjl/letschat/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
Expand All @@ -20,6 +21,8 @@ import com.google.android.gms.tasks.Task
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.*
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.UploadTask
Expand All @@ -30,18 +33,23 @@ import java.util.*

class MainActivity : AppCompatActivity() {

private val TAG = "MainActivity"
companion object{
private val TAG = "MainActivity"
const val ANONYMOUS = "anonymous"
const val DEFAULT_MSG_LENGTH_LIMIT = 1000
const val MSG_LENGTH_KEY = "msg_length"
const val RC_SIGN_IN: Int = 12
const val RC_PHOTO_PICKER: Int = 2
}

val ANONYMOUS = "anonymous"
val DEFAULT_MSG_LENGTH_LIMIT = 1000
val RC_SIGN_IN: Int = 12
val RC_PHOTO_PICKER: Int = 2
//firebase
private lateinit var mFirebaseDatabase: FirebaseDatabase
private lateinit var mMessagesDatabaseReference: DatabaseReference
private var mChildEventListener: ChildEventListener? = null
private lateinit var mFirebaseStorage: FirebaseStorage
private lateinit var mChatStorageReference: StorageReference

private lateinit var mFirebaseRemoteConfig: FirebaseRemoteConfig
//activity
private lateinit var viewBinding: ActivityMainBinding
private lateinit var mMessagesAdapter: MessageAdapter
Expand All @@ -61,7 +69,7 @@ class MainActivity : AppCompatActivity() {
mFirebaseDatabase = FirebaseDatabase.getInstance()
mFirebaseAuth = FirebaseAuth.getInstance()
mFirebaseStorage = FirebaseStorage.getInstance()

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance()

mMessagesDatabaseReference = mFirebaseDatabase.getReference("messages")
mChatStorageReference = mFirebaseStorage.reference.child("chat_photos")
Expand All @@ -78,7 +86,7 @@ class MainActivity : AppCompatActivity() {
//Image Picker button

viewBinding.photoPickerButton.setOnClickListener {
var intent: Intent = Intent(Intent.ACTION_GET_CONTENT)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/jpeg"
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER)
Expand Down Expand Up @@ -106,14 +114,14 @@ class MainActivity : AppCompatActivity() {
//Send messages action
viewBinding.sendButton.setOnClickListener { _ ->

var message = CustomMessage(viewBinding.messageEditText.text.toString(), userName, null)
val message = CustomMessage(viewBinding.messageEditText.text.toString(), userName, null)
mMessagesDatabaseReference.push().setValue(message)
viewBinding.messageEditText.setText("")
}


mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
var user: FirebaseUser? = firebaseAuth.currentUser
val user: FirebaseUser? = firebaseAuth.currentUser
if (user != null) {
// user signed in
//Toast.makeText(applicationContext, "Welcome, you've signed in.", Toast.LENGTH_LONG).show()
Expand All @@ -130,6 +138,44 @@ class MainActivity : AppCompatActivity() {
)
}
}

val config :FirebaseRemoteConfigSettings = FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG)
.build()
mFirebaseRemoteConfig.setConfigSettings(config)

val defaultConfiguration = mutableMapOf<String,Any>()
defaultConfiguration[MSG_LENGTH_KEY] = DEFAULT_MSG_LENGTH_LIMIT
//setting up new configuration
mFirebaseRemoteConfig.setDefaults(defaultConfiguration)

fetchConfig()


}

private fun fetchConfig(){
var cacheExpiration : Long = 3600
if (mFirebaseRemoteConfig.info.configSettings.isDeveloperModeEnabled){
cacheExpiration = 0
}
mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnSuccessListener {
mFirebaseRemoteConfig.fetchAndActivate()
applyRetrievedLengthLimit()

}.addOnFailureListener{
// this case will happen when offline
Log.w(TAG,"Error fetching config",it)
applyRetrievedLengthLimit()
}

}
private fun applyRetrievedLengthLimit(){
var msgLength = mFirebaseRemoteConfig.getLong(MSG_LENGTH_KEY)
//adapt the edit text
viewBinding.messageEditText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(msgLength.toInt()))
Log.d(TAG, "$MSG_LENGTH_KEY=$msgLength")
}

private fun onSignedInInitilized(name: String) {
Expand Down Expand Up @@ -160,7 +206,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onChildAdded(p0: DataSnapshot, p1: String?) {
var customMessage = p0.getValue(CustomMessage::class.java)
val customMessage = p0.getValue(CustomMessage::class.java)
// var customMessage = p0.value as? CustomMessage
mMessagesAdapter.add(customMessage)
}
Expand Down Expand Up @@ -207,14 +253,14 @@ class MainActivity : AppCompatActivity() {
finish()
}
} else if (requestCode == RC_PHOTO_PICKER && resultCode == Activity.RESULT_OK) {
var selectedImageUri: Uri = data?.data ?: Uri.EMPTY
var photoRef: StorageReference =
mChatStorageReference.child(selectedImageUri.lastPathSegment)
val selectedImageUri: Uri = data?.data ?: Uri.EMPTY
val photoRef: StorageReference =
mChatStorageReference.child(selectedImageUri.lastPathSegment!!)
//upload the file to Firebase
var uploadTask = photoRef.putFile(selectedImageUri)
val uploadTask = photoRef.putFile(selectedImageUri)

//register observers to listen for when the download is done or if it falls
val urlTask = uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
Expand All @@ -224,7 +270,7 @@ class MainActivity : AppCompatActivity() {
}).addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
var customMessage = CustomMessage(null, userName, downloadUri.toString())
val customMessage = CustomMessage(null, userName, downloadUri.toString())
mMessagesDatabaseReference.push().setValue(customMessage)
} else {
// Handle failures
Expand All @@ -240,7 +286,7 @@ class MainActivity : AppCompatActivity() {


override fun onCreateOptionsMenu(menu: Menu?): Boolean {
var inflater: MenuInflater = menuInflater
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.main_menu, menu)
return true
}
Expand Down

0 comments on commit 00f8975

Please sign in to comment.