Created
September 7, 2021 08:08
-
-
Save kyodgorbek/2a369fcbb2138cb9e25c0855514dc162 to your computer and use it in GitHub Desktop.
MainActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
val mainView: View by lazy { binding.root } | |
private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } | |
private val navController: NavController by lazy { findNavController(R.id.main_nav_host_fragment) } | |
private val isNewUser: Boolean by lazy { | |
intent.getBooleanExtra( | |
LibAppKeys.KEY_ARGS_IS_NEW_USER, | |
false | |
) | |
} | |
private var shareCounterGoal = 0 | |
private val shareCounterCurrent = MutableLiveData(0) | |
private val bluetoothBroadcastReceiver | |
get() = BluetoothBroadcastReceiver(bluetoothViewModel) | |
private val bleAdvertiser by lazy { BleAdvertiser(bluetoothViewModel) } | |
private val bluetoothViewModel: BluetoothViewModel by viewModels { | |
BluetoothViewModelFactory( | |
getSystemService(BLUETOOTH_SERVICE) as BluetoothManager, | |
this | |
) | |
} | |
private val updateStringToAdvertiseCallback = | |
object : SettingsViewModel.AdvertiseStringChangedCallback { | |
override fun onCallback() { | |
updateBleAdvertisedString() | |
} | |
} | |
private val swapBackCallback = object : SwapManager.SwapBackCallback { | |
override fun onCallback(contacts: ArrayList<UserContact>) { | |
bluetoothViewModel.listOfSelectedUser.clear() | |
bluetoothViewModel.listToSwapBackOfSelectedUser.clear() | |
for (contact in contacts) { | |
bluetoothViewModel.listToSwapBackOfSelectedUser.add( | |
contact.data.uid.take( | |
LibFirebaseValues.Uid.SHORT_UID_LENGHT | |
) | |
) | |
} | |
navController.navigate(R.id.action_bnv_home_to_swapSelectDataFragment) | |
} | |
} | |
private val swapReceivedCallback: SwapManager.SwapReceivedCallback = | |
object : SwapManager.SwapReceivedCallback { | |
override fun onCallback(snapshot: DataSnapshot, imageUrl: String, uid: String) { | |
if (snapshot.hasChildren()) { | |
d("Swap received! $snapshot") | |
var contact: UserContact? = null | |
for (child in snapshot.children) { | |
contact = | |
SwapManager.createContact( | |
snapshot, | |
imageUrl, | |
uid, | |
child.key.toString(), | |
sharedViewModel.appUser.value!!.contacts | |
) | |
if (sharedViewModel.receivedSwaps.value!!.find { receivedContact -> receivedContact.data.uid == contact.data.uid } == null) { | |
sharedViewModel.receivedSwaps.value!!.add(contact) | |
if (SwapDialog.displayed) { | |
SwapDialog.adapter.submitList(sharedViewModel.receivedSwaps.value!!) | |
SwapDialog.adapter.notifyDataSetChanged() | |
d("SWAPDEBUG ${sharedViewModel.receivedSwaps.value!!}") | |
} | |
if (navController.currentDestination?.id == R.id.bnv_home && !SwapDialog.displayed) | |
popSwapDialog() | |
} | |
// else { | |
// if (SwapDialog.contactList.value.isNullOrEmpty()) { | |
// SwapDialog.displaySwapDialog( | |
// this@MainActivity, | |
// this@MainActivity, | |
// contact, | |
// swapBackCallback | |
// ) | |
// } else SwapDialog.addSwap(contact) | |
// } | |
} | |
if (contact != null) notifyAboutSwap(contact.isDoc) | |
} | |
} | |
} | |
private fun setSwapDialog() { | |
SwapDialog.ownerNameFirst = sharedViewModel.appUser.value!!.getFirstName() | |
sharedViewModel.receivedSwaps.observe(this, { | |
if (it != null && it.isNotEmpty()) { | |
if (navController.currentDestination?.id == R.id.bnv_home) | |
popSwapDialog() | |
} | |
}) | |
} | |
private fun popSwapDialog() { | |
if (SwapDialog.displayed) return | |
SwapDialog.displaySwapDialog( | |
this@MainActivity, | |
this@MainActivity, | |
swapBackCallback, | |
sharedViewModel | |
) | |
} | |
private val settingsViewModel: SettingsViewModel by viewModels { | |
SettingsViewModelFactory(this, updateStringToAdvertiseCallback) | |
} | |
val sharedViewModel: SharedViewModel by viewModels { | |
SharedViewModelFactory( | |
this, | |
isNewUser | |
) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
checkForUser() | |
initiateActivity() | |
prepareViews() | |
setNavListener() | |
LoadingFullscreenDialog.removeLoadingDialog() | |
registerBluetoothBroadcastReceiver() | |
enableAdvertisement() | |
listenForDynamicLinks() | |
receiveShareIntent() | |
setSwapDialog() | |
if (!sharedViewModel.appUser.value?.contacts.isNullOrEmpty()) { | |
getContactImages() | |
listenForContactUpdates() | |
} | |
getCurrentToken() | |
} | |
private fun checkForUser() { | |
if (!AuthManager.isUserLoggedIn()) { | |
val authIntent = Intent(this, AuthActivity::class.java) | |
authIntent.putExtra("loggedOff", true) | |
startActivity(authIntent) | |
finish() | |
} | |
} | |
private fun receiveShareIntent() { | |
var fileUri = "" | |
d("onSharedIntent: ${intent.data}") | |
val receivedAction = intent.action | |
d("onSharedIntent action: ${intent.action}") | |
if (Intent.ACTION_SEND == intent.action && intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as Uri? != null) { | |
fileUri = (intent | |
.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as Uri).toString() | |
} | |
} | |
private fun getCurrentToken() { | |
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> | |
if (!task.isSuccessful) { | |
d("Fetching FCM registration token failed: ${task.exception}") | |
return@OnCompleteListener | |
} | |
// Get new FCM registration token | |
val token = task.result | |
d("Current Token:$token") | |
if (token != null) { | |
val tokenModel = TokenModel( | |
0, | |
Locale.getDefault().language, | |
token | |
) | |
FirebaseDatabaseManager.getDatabaseRef().child(UserRepository.getUid()) | |
.child("Token").setValue( | |
tokenModel | |
) | |
PrefPersistenceManager.saveToken(this, tokenModel) | |
} else { | |
//TODO error | |
} | |
}) | |
} | |
override fun onResume() { | |
super.onResume() | |
sharedViewModel.appIsInBackGround = false | |
SwapManager.listenForSwaps( | |
swapReceivedCallback, | |
sharedViewModel.appUser.value!!.uid, | |
this, | |
sharedViewModel | |
) | |
val tokenModel = PrefPersistenceManager.getToken(this) | |
if (tokenModel != null) | |
tokenModel.currentBadge = 0 | |
FirebaseDatabaseManager.getDatabaseRef().child(UserRepository.getUid()).child("Token") | |
.setValue( | |
tokenModel | |
) | |
(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager)?.cancelAll() | |
} | |
override fun onPause() { | |
super.onPause() | |
sharedViewModel.appIsInBackGround = true | |
SwapManager.stopListeningForSwaps(sharedViewModel.appUser.value!!.uid) | |
} | |
private fun listenForDynamicLinks() { | |
if (!NetworkUtil.checkConnection(this)) return | |
Firebase.dynamicLinks | |
.getDynamicLink(intent) | |
.addOnSuccessListener(this) { pendingDynamicLinkData -> | |
var deepLink: Uri? = null | |
if (pendingDynamicLinkData != null) { | |
deepLink = pendingDynamicLinkData.link | |
} | |
d("DeepLink received: $deepLink") | |
if (deepLink != null) { | |
LoadingFullscreenDialog.displayLoadingDialog(this) | |
val readyToFetchCallback = object : FbShareManager.ContactReadyToFetchCallback { | |
override fun onCallback(uid: String, hashMapAuth: HashMap<String, Any>) { | |
val sharedUserDbRef = | |
FirebaseDatabaseManager.getDatabaseRef().child(uid) | |
d("SHARE dbref: $sharedUserDbRef") | |
val contact = FbFetchContactsManager.getEmptyContact(uid) | |
contact.timeStampInMillis = DocUtil.getTimeStamp() | |
val hashMapUser = | |
hashMapAuth[uid] as HashMap<String, HashMap<String, Any>> | |
shareCounterGoal = | |
hashMapUser[LibFirebaseStrings.User.Data.DATA_PERSONAL]!!.size | |
shareCounterGoal += 1 | |
contact.data.imgUrl = "" | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_IMAGE) | |
.addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
d("SHARE pi item onDataChanged") | |
if (snapshot.hasChild("img")) | |
contact.data.imgUrl = | |
snapshot.child("img").value as String | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
d("SHARE current counter changed to: ${shareCounterCurrent.value}") | |
d("SHARE img added entry") | |
} | |
override fun onCancelled(error: DatabaseError) { | |
d("SHARE pi item error") | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
for (item in hashMapUser[LibFirebaseStrings.User.Data.DATA_PERSONAL]!!.entries) { | |
d("SHARE pi item entry: $item") | |
d( | |
"SHARE pi item entry dbref: ${ | |
sharedUserDbRef.child( | |
LibFirebaseStrings.User.USER_DATA | |
).child(LibFirebaseStrings.User.Data.DATA_PERSONAL) | |
.child(item.key) | |
}" | |
) | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_PERSONAL) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
d("SHARE pi item onDataChanged") | |
val entry = LtvModel( | |
item.key, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_LABEL).value as String, | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.LTV_MODEL_T_LABEL)) LibFirebaseStrings.BaseDb.LTV_MODEL_T_VALUE else null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataPrivate.add(entry) | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
d("SHARE current counter changed to: ${shareCounterCurrent.value}") | |
d("SHARE PI added entry: $entry") | |
} | |
override fun onCancelled(error: DatabaseError) { | |
d("SHARE pi item error") | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.Data.DATA_BUSINESS].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.Data.DATA_BUSINESS]!!.size | |
for (item in hashMapUser[LibFirebaseStrings.User.Data.DATA_BUSINESS]!!.entries) { | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_BUSINESS) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
val entry = LtvModel( | |
item.key, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_LABEL).value as String, | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.LTV_MODEL_T_LABEL)) LibFirebaseStrings.BaseDb.LTV_MODEL_T_VALUE else null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataBusiness.add(entry) | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS]!!.size | |
for (item in hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS]!!.entries) { | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_ADDRESS) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
d("CRASHSNAP MAP: ${hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS]}") | |
d("CRASHSNAP MAPWORK: ${hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS_WORK]}") | |
d("CRASHSNAP: $snapshot") | |
val entry = LtvModel( | |
item.key, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_LABEL).value as String, | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.LTV_MODEL_T_LABEL)) LibFirebaseStrings.BaseDb.LTV_MODEL_T_VALUE else null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataAddressPrivate.add(entry) | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS_WORK].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS_WORK]!!.size | |
for (item in hashMapUser[LibFirebaseStrings.User.Data.DATA_ADDRESS_WORK]!!.entries) { | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_ADDRESS_WORK) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
val entry = LtvModel( | |
item.key, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_LABEL).value as String, | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.LTV_MODEL_T_LABEL)) LibFirebaseStrings.BaseDb.LTV_MODEL_T_VALUE else null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataAddressBusiness.add(entry) | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.Data.DATA_SOCIAL_MEDIA].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.Data.DATA_SOCIAL_MEDIA]!!.size | |
for (item in hashMapUser[LibFirebaseStrings.User.Data.DATA_SOCIAL_MEDIA]!!.entries) { | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_SOCIAL_MEDIA) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
d("NOT About me snap $snapshot") | |
val entry = LtvModel( | |
item.key, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_LABEL).value as String, | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.LTV_MODEL_T_LABEL)) LibFirebaseStrings.BaseDb.LTV_MODEL_T_VALUE else null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataSocialMedia.add(entry) | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.Data.DATA_ABOUT_ME].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.Data.DATA_ABOUT_ME]!!.size | |
d("About me map: ${hashMapUser[LibFirebaseStrings.User.Data.DATA_ABOUT_ME]}") | |
d( | |
"About me path: ${ | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_ABOUT_ME) | |
.child("00") | |
}" | |
) | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DATA) | |
.child(LibFirebaseStrings.User.Data.DATA_ABOUT_ME) | |
.child("00").addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
d("About me snap: $snapshot") | |
val entry = LtvModel( | |
"00", | |
LibDefaultDataLabels.ABOUTME, | |
null, | |
snapshot.child(LibFirebaseStrings.BaseDb.LTV_MODEL_VALUE).value as String, | |
null | |
) | |
contact.data.dataAboutMe = entry | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
if (!hashMapUser[LibFirebaseStrings.User.USER_DOCS].isNullOrEmpty()) { | |
shareCounterGoal += hashMapUser[LibFirebaseStrings.User.USER_DOCS]!!.size | |
for (item in hashMapUser[LibFirebaseStrings.User.USER_DOCS]!!.entries) { | |
sharedUserDbRef.child(LibFirebaseStrings.User.USER_DOCS) | |
.child(item.key).addListenerForSingleValueEvent( | |
object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
val name = | |
snapshot.child("name").value as String | |
val extension = File(name).extension | |
val doc = UserDocument( | |
item.key.toInt(), | |
item.key, | |
File(name).nameWithoutExtension, | |
extension, | |
MimeTypeMap.getSingleton() | |
.getMimeTypeFromExtension(extension) | |
.toString(), | |
snapshot.child("size").value as Long, | |
snapshot.child("url").value as String, | |
null, | |
isOwn = false, | |
isDownloaded = false, | |
isUploading = false, | |
isDownloading = false, | |
gotFrom = contact.data.getFullName(), | |
LibBluetoothValues.Share.DEFAULT_SHARE_DOC, | |
null | |
) | |
contact.data.documents.add(doc) | |
d("SHARE addded doc: $doc") | |
shareCounterCurrent.value = | |
shareCounterCurrent.value!!.plus(1) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
if (!NetworkUtil.isOnline(this@MainActivity)) | |
CustomToast.displayToast( | |
this@MainActivity, | |
getString(R.string.errorUnknownMsg), | |
isSuccess = false | |
) | |
} | |
} | |
) | |
} | |
} | |
d("SHARE Total data to get: $shareCounterGoal") | |
shareCounterCurrent.observe(this@MainActivity, { | |
if (it == shareCounterGoal) { | |
LoadingFullscreenDialog.removeLoadingDialog() | |
d("SHARE counter FINISHED") | |
val contactAcceptedCallback = | |
object : ShareDialog.ContactAcceptedCallback { | |
override fun onCallback(contact: UserContact) { | |
if (sharedViewModel.appUser.value!!.contacts == null) { | |
sharedViewModel.appUser.value!!.contacts = | |
arrayListOf(contact) | |
} else { | |
if (sharedViewModel.appUser.value!!.contacts!!.filter { userContact -> userContact.data.uid == contact.data.uid } | |
.isNullOrEmpty()) { | |
sharedViewModel.appUser.value!!.contacts!!.add( | |
contact | |
) | |
} | |
} | |
FbAddContactManager.saveContactToFirebase(contact) | |
sharedViewModel.updateUser(this@MainActivity) | |
notifyAboutShareSuccess() | |
} | |
} | |
ShareDialog.displayShareDialog( | |
this@MainActivity, | |
contact, | |
contactAcceptedCallback | |
) | |
shareCounterCurrent.removeObservers(this@MainActivity) | |
shareCounterCurrent.value = 0 | |
shareCounterGoal = 0 | |
} | |
}) | |
} | |
} | |
ShareManager.processLink(deepLink.toString(), readyToFetchCallback) | |
} | |
} | |
.addOnFailureListener(this) { e -> d("getDynamicLink:onFailure: $e") } | |
} | |
private fun listenForContactUpdates() { | |
FbUpdateContactManager.listenForImageChanges(sharedViewModel, this) | |
FbUpdateContactManager.listenForContactChanges(sharedViewModel, this) | |
} | |
private fun getContactImages() { | |
for (contact in sharedViewModel.appUser.value!!.contacts!!) { | |
val imgUrl = | |
PrefPersistenceManager.getContactImgUrl( | |
this, | |
CryptUtil.encryptString(contact.data.uid.take(LibFirebaseValues.Uid.SHORT_UID_LENGHT)) | |
) | |
if (imgUrl.isEmpty()) { | |
FirebaseDatabaseManager.getDatabaseRef() | |
.child(LibFirebaseStrings.MAIN_TH) | |
.child(contact.data.uid.take(LibFirebaseValues.Uid.SHORT_UID_LENGHT)) | |
.addListenerForSingleValueEvent(object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
val image = | |
if (snapshot.hasChild(LibFirebaseStrings.BaseDb.IMG_MODEL_LABEL)) snapshot.child( | |
LibFirebaseStrings.BaseDb.IMG_MODEL_LABEL | |
).value as String else "" | |
contact.data.imgUrl = image | |
PrefPersistenceManager.saveContactImgUrl( | |
this@MainActivity, | |
image, | |
CryptUtil.encryptString(contact.data.uid.take(LibFirebaseValues.Uid.SHORT_UID_LENGHT)) | |
) | |
Timber.d("ContactImage: $contact") | |
} | |
override fun onCancelled(error: DatabaseError) { | |
Timber.d(error.message) | |
} | |
}) | |
} else contact.data.imgUrl = imgUrl | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
// Remove BroadcastReceiver | |
LocalBroadcastManager.getInstance(this).unregisterReceiver(bluetoothBroadcastReceiver) | |
} | |
override fun onSupportNavigateUp(): Boolean { | |
return navController.navigateUp() | |
} | |
override fun onBackPressed() { | |
Timber.d("BackPress detected") | |
if (navController.currentDestination!!.id != R.id.bnv_home) { | |
Timber.d("Going back") | |
super.onBackPressed() | |
} else { | |
Timber.d("Asking user if he wishes to leave.") | |
val confirmDialogBuilder = | |
ConfirmDialogs.createConfirmExitDialog(this) | |
confirmDialogBuilder.setPositiveButton(getString(R.string.confirmYes)) { dialog, _ -> | |
Timber.d("Goodbye.") | |
dialog.dismiss() | |
super.onBackPressed() | |
} | |
ConfirmDialogs.displayConfirmDialog( | |
confirmDialogBuilder.create(), | |
this | |
) | |
} | |
} | |
private fun initiateActivity() { | |
setContentView(mainView) | |
setSupportActionBar(mainView.main_toolbar) | |
// disable up buttons | |
val appBarConfiguration = if (Const.HAS_DOCS) AppBarConfiguration( | |
setOf( | |
R.id.bnv_home, | |
R.id.bnv_contacts, | |
R.id.bnv_docs, | |
R.id.bnv_profile | |
) | |
) else AppBarConfiguration( | |
setOf( | |
R.id.bnv_contacts, | |
R.id.bnv_home, | |
R.id.bnv_profile | |
) | |
) | |
setupActionBarWithNavController(navController, appBarConfiguration) | |
} | |
private fun prepareViews() { | |
binding.setVariable( | |
BR.sharedViewModel, | |
sharedViewModel | |
) | |
binding.setVariable( | |
BR.settingsViewModel, | |
settingsViewModel | |
) | |
// TODO bnv nav transition animation | |
mainView.main_bottom_nav_view.apply { | |
itemIconTintList = null | |
setupWithNavController(navController) | |
} | |
if (isNewUser) showNewUserBadges() | |
} | |
private fun setNavListener() { | |
navController.addOnDestinationChangedListener { _, destination, _ -> | |
setToolbarVisibility(destination.id) | |
setBottomNavVisibility(destination.id) | |
setUpIconDrawable(destination.id) | |
removeBadges(destination.id) | |
when (destination.id) { | |
R.id.bnv_home -> { | |
supportActionBar?.title = "" | |
if (sharedViewModel.receivedSwaps.value!!.isNotEmpty()) | |
popSwapDialog() | |
} | |
R.id.bnv_profile -> { | |
supportActionBar?.title = "" | |
} | |
} | |
} | |
} | |
private fun setToolbarVisibility(currentDestinationId: Int) { | |
val hideActionBar = when (currentDestinationId) { | |
R.id.profileImageFragment, R.id.contactImageFragment, R.id.bnv_home -> { | |
true | |
} | |
R.id.defaultDataFragment, R.id.swapSelectDataFragment, R.id.swapSingleFragment, R.id.swapMultipleFragment -> { | |
supportActionBar?.title = "" | |
false | |
} | |
else -> { | |
false | |
} | |
} | |
if (hideActionBar) supportActionBar?.hide() | |
else supportActionBar?.show() | |
} | |
private fun setBottomNavVisibility(currentDestinationId: Int) { | |
val hideBnv = when (currentDestinationId) { | |
R.id.profileImageFragment, R.id.contactImageFragment, R.id.profileEditFragment, R.id.swapSingleFragment, R.id.swapMultipleFragment, R.id.settingsFragment, R.id.swapSelectDataFragment -> { | |
true | |
} | |
else -> { | |
false | |
} | |
} | |
if (hideBnv) mainView.main_bottom_nav_view.visibility = View.GONE | |
else mainView.main_bottom_nav_view.visibility = View.VISIBLE | |
} | |
private fun setUpIconDrawable(currentDestinationId: Int) { | |
val upIconId = when (currentDestinationId) { | |
R.id.profileEditFragment -> { | |
R.drawable.ic_save_and_up | |
} | |
R.id.swapSingleFragment, R.id.swapMultipleFragment -> { | |
R.drawable.home_navbar | |
} | |
else -> { | |
R.drawable.back_button | |
} | |
} | |
supportActionBar?.setHomeAsUpIndicator(upIconId) | |
} | |
private fun removeBadges(currentDestinationId: Int) { | |
when (currentDestinationId) { | |
R.id.profileEditFragment -> { | |
removeNewUserBadges() | |
} | |
} | |
if (currentDestinationId == R.id.bnv_home && mainView.main_bottom_nav_view.getBadge(R.id.bnv_home) != null) mainView.main_bottom_nav_view.removeBadge( | |
R.id.bnv_home | |
) | |
if (currentDestinationId == R.id.bnv_contacts) { | |
val contactBadge = mainView.main_bottom_nav_view.getBadge( | |
R.id.bnv_contacts | |
) | |
if (contactBadge != null | |
) { | |
contactBadge.number = 0 | |
mainView.main_bottom_nav_view.removeBadge( | |
R.id.bnv_contacts | |
) | |
} | |
} | |
if (currentDestinationId == R.id.bnv_docs) { | |
val docBadge = mainView.main_bottom_nav_view.getBadge( | |
R.id.bnv_docs | |
) | |
if (docBadge != null) { | |
docBadge.number = 0 | |
mainView.main_bottom_nav_view.removeBadge( | |
R.id.bnv_docs | |
) | |
} | |
} | |
} | |
private fun showNewUserBadges() { | |
Timber.d("Handling new user") | |
val badge = mainView.main_bottom_nav_view.getOrCreateBadge(R.id.bnv_profile) | |
badge.backgroundColor = getColor(R.color.colorAccent) | |
badge.isVisible = true | |
} | |
private fun removeNewUserBadges() { | |
if (mainView.main_bottom_nav_view.getBadge(R.id.bnv_profile) != null) | |
mainView.main_bottom_nav_view.removeBadge(R.id.bnv_profile) | |
} | |
private fun registerBluetoothBroadcastReceiver() { | |
registerReceiver( | |
bluetoothBroadcastReceiver, | |
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) | |
) | |
} | |
private fun enableAdvertisement() { | |
bleAdvertiser.stringToAdvertise = | |
bleAdvertiser.createStringToAdvertise(this, sharedViewModel.appUser.value!!.uid) | |
bluetoothViewModel.hasBluetooth.observe(this, { hasBluetooth -> | |
if (hasBluetooth) { | |
bleAdvertiser.startAdvertising() | |
} else { | |
bleAdvertiser.stopAdvertising() | |
} | |
}) | |
} | |
private fun updateBleAdvertisedString() { | |
bleAdvertiser.updateAdvertiseString(this, sharedViewModel.appUser.value!!.uid) | |
} | |
private fun notifyAboutSwap(isDoc: Boolean) { | |
if (navController.currentDestination?.id != R.id.bnv_home && !isDoc) | |
mainView.main_bottom_nav_view.getOrCreateBadge(R.id.bnv_home).backgroundColor = | |
getColor(R.color.colorAccent) | |
SoundVibrateUtil.vibrateAndSound(this) | |
} | |
private fun notifyAboutShareSuccess() { | |
if (navController.currentDestination?.id != R.id.bnv_contacts) | |
mainView.main_bottom_nav_view.getOrCreateBadge(R.id.bnv_contacts).backgroundColor = | |
getColor(R.color.colorAccent) | |
SoundVibrateUtil.vibrateShort(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment