JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "TabBarView.h"
#import "PhoneMainView.h"
 
@implementation TabBarView
 
#pragma mark - ViewController Functions
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(changeViewEvent:)
                                               name:kLinphoneMainViewChange
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(callUpdate:)
                                               name:kLinphoneCallUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(messageReceived:)
                                               name:kLinphoneMessageReceived
                                             object:nil];
    [self update:FALSE];
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self update:FALSE];
}
 
#pragma mark - Event Functions
 
- (void)callUpdate:(NSNotification *)notif {
    // LinphoneCall *call = [[notif.userInfo objectForKey: @"call"] pointerValue];
    // LinphoneCallState state = [[notif.userInfo objectForKey: @"state"] intValue];
    [self updateMissedCall:linphone_core_get_missed_calls_count(LC) appear:TRUE];
}
 
- (void)changeViewEvent:(NSNotification *)notif {
    UICompositeViewDescription *view = [notif.userInfo objectForKey:@"view"];
    if (view != nil) {
        [self updateSelectedButton:view];
    }
}
 
- (void)messageReceived:(NSNotification *)notif {
    [self updateUnreadMessage:TRUE];
}
 
#pragma mark - UI Update
 
- (void)update:(BOOL)appear {
    [self updateSelectedButton:[PhoneMainView.instance currentView]];
    [self updateMissedCall:linphone_core_get_missed_calls_count(LC) appear:appear];
    [self updateUnreadMessage:appear];
}
 
- (void)updateUnreadMessage:(BOOL)appear {
    int unreadMessage = [LinphoneManager unreadMessageCount];
    if (unreadMessage > 0) {
        _chatNotificationLabel.text = [NSString stringWithFormat:@"%i", unreadMessage];
        [_chatNotificationView startAnimating:appear];
    } else {
        [_chatNotificationView stopAnimating:appear];
    }
}
 
- (void)updateMissedCall:(int)missedCall appear:(BOOL)appear {
    if (missedCall > 0) {
        _historyNotificationLabel.text = [NSString stringWithFormat:@"%i", missedCall];
        [_historyNotificationView startAnimating:appear];
    } else {
        [_historyNotificationView stopAnimating:appear];
    }
}
 
- (void)updateSelectedButton:(UICompositeViewDescription *)view {
    _historyButton.selected = [view equal:HistoryListView.compositeViewDescription] ||
                              [view equal:HistoryDetailsView.compositeViewDescription];
    _contactsButton.selected = [view equal:ContactsListView.compositeViewDescription] ||
                               [view equal:ContactDetailsView.compositeViewDescription];
    _dialerButton.selected = [view equal:DialerView.compositeViewDescription];
    _chatButton.selected = [view equal:ChatsListView.compositeViewDescription] ||
                           [view equal:ChatConversationCreateView.compositeViewDescription] ||
                           [view equal:ChatConversationInfoView.compositeViewDescription] ||
                           [view equal:ChatConversationImdnView.compositeViewDescription] ||
                           [view equal:ChatConversationView.compositeViewDescription];
    CGRect selectedNewFrame = _selectedButtonImage.frame;
    if ([self viewIsCurrentlyPortrait]) {
        selectedNewFrame.origin.x =
            (_historyButton.selected
                 ? _historyButton.frame.origin.x
                 : (_contactsButton.selected
                        ? _contactsButton.frame.origin.x
                        : (_dialerButton.selected
                               ? _dialerButton.frame.origin.x
                               : (_chatButton.selected
                                      ? _chatButton.frame.origin.x
                                      : -selectedNewFrame.size.width /*hide it if none is selected*/))));
    } else {
        selectedNewFrame.origin.y =
            (_historyButton.selected
                 ? _historyButton.frame.origin.y
                 : (_contactsButton.selected
                        ? _contactsButton.frame.origin.y
                        : (_dialerButton.selected
                               ? _dialerButton.frame.origin.y
                               : (_chatButton.selected
                                      ? _chatButton.frame.origin.y
                                      : -selectedNewFrame.size.height /*hide it if none is selected*/))));
    }
 
    CGFloat delay = ANIMATED ? 0.3 : 0;
    [UIView animateWithDuration:delay
                     animations:^{
                       _selectedButtonImage.frame = selectedNewFrame;
 
                     }];
}
 
#pragma mark - Action Functions
 
- (IBAction)onHistoryClick:(id)event {
    linphone_core_reset_missed_calls_count(LC);
    [self update:FALSE];
    [PhoneMainView.instance updateApplicationBadgeNumber];
    [PhoneMainView.instance changeCurrentView:HistoryListView.compositeViewDescription];
}
 
- (IBAction)onContactsClick:(id)event {
    [ContactSelection setAddAddress:nil];
    [ContactSelection enableEmailFilter:FALSE];
    [ContactSelection setNameOrEmailFilter:nil];
    [PhoneMainView.instance changeCurrentView:ContactsListView.compositeViewDescription];
}
 
- (IBAction)onDialerClick:(id)event {
    [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription];
}
 
- (IBAction)onSettingsClick:(id)event {
    [PhoneMainView.instance changeCurrentView:SettingsView.compositeViewDescription];
}
 
- (IBAction)onChatClick:(id)event {
    [PhoneMainView.instance changeCurrentView:ChatsListView.compositeViewDescription];
}
 
@end