< Summary

Class:DCL.DCLVoiceChatController
Assembly:VoiceChatController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/VoiceChat/DCLVoiceChatController.cs
Covered lines:20
Uncovered lines:43
Coverable lines:63
Total lines:159
Line coverage:31.7% (20 of 63)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:16
Method coverage:31.2% (5 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLVoiceChatController()0%110100%
Awake()0%110100%
VoiceChatTriggered(...)0%2100%
VoiceChatHoldActionStart(...)0%2100%
VoiceChatHoldActionFinish(...)0%2100%
OnApplicationFocus(...)0%6200%
OnDestroy()0%110100%
OnKernelConfigChanged(...)0%2100%
EnableVoiceChat(...)0%220100%
VoiceChatStatus(...)0%6200%
IsVoiceChatRecordingChanged(...)0%12300%
StartRecording()0%6200%
StopRecording(...)0%20400%
CreateSocialAnalyticsIfNeeded()0%6200%
SendFirstTimeMetricIfNeeded()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/VoiceChat/DCLVoiceChatController.cs

#LineLine coverage
 1using DCL.Interface;
 2using Newtonsoft.Json;
 3using SocialFeaturesAnalytics;
 4using System;
 5using System.Collections.Generic;
 6using UnityEngine;
 7
 8namespace DCL
 9{
 10    public class DCLVoiceChatController : MonoBehaviour
 11    {
 12        [Header("InputActions")]
 13        public InputAction_Hold voiceChatHoldAction;
 14        public InputAction_Trigger voiceChatToggleAction;
 15
 34116        private bool firstTimeVoiceRecorded = true;
 17        private ISocialAnalytics socialAnalytics;
 18        private UserProfileWebInterfaceBridge userProfileWebInterfaceBridge;
 19        private double voiceMessageStartTime = 0;
 20
 21        private bool isRecording = false;
 22
 81123        private DataStore_VoiceChat voiceChatDataStore => DataStore.i.voiceChat;
 24
 25        private void Awake()
 26        {
 32427            userProfileWebInterfaceBridge = new UserProfileWebInterfaceBridge();
 28
 32429            voiceChatHoldAction.OnStarted += VoiceChatHoldActionStart;
 32430            voiceChatHoldAction.OnFinished += VoiceChatHoldActionFinish;
 32431            voiceChatToggleAction.OnTriggered += VoiceChatTriggered;
 32
 64833            KernelConfig.i.EnsureConfigInitialized().Then(config => EnableVoiceChat(config.comms.voiceChatEnabled));
 32434            KernelConfig.i.OnChange += OnKernelConfigChanged;
 32435            voiceChatDataStore.isRecording.OnChange += IsVoiceChatRecordingChanged;
 32436        }
 37
 38        private void VoiceChatTriggered(DCLAction_Trigger action)
 39        {
 040            voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(!isRecording, true));
 041        }
 42
 43        private void VoiceChatHoldActionStart(DCLAction_Hold _)
 44        {
 045            voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(true, true));
 046        }
 47
 48        private void VoiceChatHoldActionFinish(DCLAction_Hold _)
 49        {
 050            voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(false, true));
 051        }
 52
 53        private void OnApplicationFocus(bool hasFocus)
 54        {
 055            if (!hasFocus)
 56            {
 057                StopRecording(true);
 058                voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(false, true));
 59            }
 060        }
 61
 62        private void OnDestroy()
 63        {
 32464            voiceChatHoldAction.OnStarted -= VoiceChatHoldActionStart;
 32465            voiceChatHoldAction.OnFinished -= VoiceChatHoldActionFinish;
 32466            voiceChatToggleAction.OnTriggered -= VoiceChatTriggered;
 67
 32468            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 32469            voiceChatDataStore.isRecording.OnChange -= IsVoiceChatRecordingChanged;
 32470        }
 71
 072        private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { EnableVoiceChat(curr
 73
 74        private void EnableVoiceChat(bool enable)
 75        {
 32476            CommonScriptableObjects.voiceChatDisabled.Set(!enable);
 77
 32478            if (!enable)
 79            {
 16380                voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(false, true));
 81            }
 32482        }
 83
 84        public void VoiceChatStatus(string voiceChatStatusPayload)
 85        {
 086            VoiceChatStatusPayload voiceChatStatus = JsonConvert.DeserializeObject<VoiceChatStatusPayload>(voiceChatStat
 087            voiceChatDataStore.isJoinedToVoiceChat.Set(voiceChatStatus.isConnected);
 88
 089            if (!voiceChatStatus.isConnected)
 90            {
 091                voiceChatDataStore.isRecording.Set(new KeyValuePair<bool, bool>(false, true));
 92            }
 093        }
 94
 95        private void IsVoiceChatRecordingChanged(KeyValuePair<bool, bool> current, KeyValuePair<bool, bool> previous)
 96        {
 097            if (!voiceChatDataStore.isJoinedToVoiceChat.Get())
 098                return;
 99
 0100            if (current.Key)
 0101                StartRecording();
 102            else
 0103                StopRecording(current.Value);
 0104        }
 105
 106        private void StartRecording()
 107        {
 0108            if (isRecording) return;
 109
 0110            WebInterface.SendSetVoiceChatRecording(true);
 111
 0112            CreateSocialAnalyticsIfNeeded();
 0113            SendFirstTimeMetricIfNeeded();
 0114            voiceMessageStartTime = Time.realtimeSinceStartup;
 115
 0116            isRecording = true;
 0117        }
 118
 119        private void StopRecording(bool usedShortcut)
 120        {
 0121            if (!isRecording) return;
 122
 0123            WebInterface.SendSetVoiceChatRecording(false);
 124
 0125            CreateSocialAnalyticsIfNeeded();
 0126            socialAnalytics.SendVoiceMessage(
 127                Time.realtimeSinceStartup - voiceMessageStartTime,
 128                usedShortcut ? VoiceMessageSource.Shortcut : VoiceMessageSource.Button,
 129                userProfileWebInterfaceBridge.GetOwn().userId);
 130
 0131            isRecording = false;
 0132        }
 133
 134        private void CreateSocialAnalyticsIfNeeded()
 135        {
 0136            if (socialAnalytics != null)
 0137                return;
 138
 0139            socialAnalytics = new SocialAnalytics(
 140                Environment.i.platform.serviceProviders.analytics,
 141                userProfileWebInterfaceBridge);
 0142        }
 143
 144        private void SendFirstTimeMetricIfNeeded()
 145        {
 0146            if (firstTimeVoiceRecorded)
 147            {
 0148                socialAnalytics.SendVoiceMessageStartedByFirstTime();
 0149                firstTimeVoiceRecorded = false;
 150            }
 0151        }
 152    }
 153
 154    [Serializable]
 155    public class VoiceChatStatusPayload
 156    {
 157        public bool isConnected;
 158    }
 159}