Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVEMENT] Close announcement banner #2064

Merged
merged 8 commits into from
May 8, 2020
2 changes: 2 additions & 0 deletions app/lib/database/model/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export default class Subscription extends Model {

@field('announcement') announcement;

@field('banner_closed') bannerClosed;

@field('topic') topic;

@field('blocked') blocked;
Expand Down
11 changes: 11 additions & 0 deletions app/lib/database/model/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export default schemaMigrations({
]
})
]
},
{
toVersion: 8,
steps: [
addColumns({
table: 'subscriptions',
columns: [
{ name: 'banner_closed', type: 'boolean', isOptional: true }
]
})
]
}
]
});
3 changes: 2 additions & 1 deletion app/lib/database/schema/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { appSchema, tableSchema } from '@nozbe/watermelondb';

export default appSchema({
version: 7,
version: 8,
tables: [
tableSchema({
name: 'subscriptions',
Expand All @@ -25,6 +25,7 @@ export default appSchema({
{ name: 'last_message', type: 'string', isOptional: true },
{ name: 'description', type: 'string', isOptional: true },
{ name: 'announcement', type: 'string', isOptional: true },
{ name: 'banner_closed', type: 'boolean', isOptional: true },
{ name: 'topic', type: 'string', isOptional: true },
{ name: 'blocked', type: 'boolean', isOptional: true },
{ name: 'blocker', type: 'boolean', isOptional: true },
Expand Down
1 change: 1 addition & 0 deletions app/lib/methods/helpers/findSubscriptionsRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default async(subscriptions = [], rooms = []) => {
lastOpen: s.lastOpen,
description: s.description,
announcement: s.announcement,
bannerClosed: s.bannerClosed,
topic: s.topic,
blocked: s.blocked,
blocker: s.blocker,
Expand Down
6 changes: 6 additions & 0 deletions app/lib/methods/subscriptions/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const createOrUpdateSubscription = async(subscription, room) => {
lastOpen: s.lastOpen,
description: s.description,
announcement: s.announcement,
bannerClosed: s.bannerClosed,
topic: s.topic,
blocked: s.blocked,
blocker: s.blocker,
Expand Down Expand Up @@ -121,6 +122,11 @@ const createOrUpdateSubscription = async(subscription, room) => {
try {
const update = sub.prepareUpdate((s) => {
Object.assign(s, tmp);
if (subscription.announcement) {
if (subscription.announcement !== sub.announcement) {
s.bannerClosed = false;
}
}
Comment on lines +125 to +129
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need some logic like this on findSubscriptionsRooms, try the follow steps and you'll able to see an error:

  • Close the banner
  • Close the app
  • Change the announcement on other client (Web/Desktop)
  • Open the app again
  • Banner isn't showed

});
batch.push(update);
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions app/sagas/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const handleRoomsRequest = function* handleRoomsRequest({ params }) {
...subsToUpdate.map((subscription) => {
const newSub = subscriptions.find(s => s._id === subscription._id);
return subscription.prepareUpdate(() => {
if (newSub.announcement) {
if (newSub.announcement !== subscription.announcement) {
subscription.bannerClosed = false;
}
}
Object.assign(subscription, newSub);
});
}),
Expand Down
19 changes: 15 additions & 4 deletions app/views/RoomView/Banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import Modal from 'react-native-modal';

import Markdown from '../../containers/markdown';

import { CustomIcon } from '../../lib/Icons';
import { themes } from '../../constants/colors';
import styles from './styles';

const Banner = React.memo(({
text, title, theme
text, title, theme, bannerClosed, closeBanner
}) => {
const [showModal, openModal] = useState(false);

const toggleModal = () => openModal(prevState => !prevState);

if (text) {
if (text && !bannerClosed) {
return (
<>
<BorderlessButton
Expand All @@ -28,8 +29,16 @@ const Banner = React.memo(({
msg={text}
theme={theme}
numberOfLines={1}
style={[styles.bannerText]}
preview
/>
<BorderlessButton onPress={closeBanner}>
<CustomIcon
color={themes[theme].auxiliaryText}
name='cross'
size={20}
/>
</BorderlessButton>
</BorderlessButton>
<Modal
onBackdropPress={toggleModal}
Expand All @@ -54,12 +63,14 @@ const Banner = React.memo(({
}

return null;
}, (prevProps, nextProps) => prevProps.text === nextProps.text && prevProps.theme === nextProps.theme);
}, (prevProps, nextProps) => prevProps.text === nextProps.text && prevProps.theme === nextProps.theme && prevProps.bannerClosed === nextProps.bannerClosed);

Banner.propTypes = {
text: PropTypes.string,
title: PropTypes.string,
theme: PropTypes.string
theme: PropTypes.string,
bannerClosed: PropTypes.bool,
closeBanner: PropTypes.func
};

export default Banner;
24 changes: 21 additions & 3 deletions app/views/RoomView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const stateAttrsUpdate = [
'readOnly',
'member'
];
const roomAttrsUpdate = ['f', 'ro', 'blocked', 'blocker', 'archived', 'muted', 'jitsiTimeout', 'announcement', 'sysMes', 'topic', 'name', 'fname', 'roles'];
const roomAttrsUpdate = ['f', 'ro', 'blocked', 'blocker', 'archived', 'muted', 'jitsiTimeout', 'announcement', 'sysMes', 'topic', 'name', 'fname', 'roles', 'bannerClosed'];

class RoomView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => {
Expand Down Expand Up @@ -810,6 +810,20 @@ class RoomView extends React.Component {
}
});

closeBanner = async() => {
const { room } = this.state;
try {
const db = database.active;
await db.action(async() => {
await room.update((r) => {
r.bannerClosed = true;
});
});
} catch {
// do nothing
}
};

renderItem = (item, previousItem) => {
const { room, lastOpen, canAutoTranslate } = this.state;
const {
Expand Down Expand Up @@ -988,7 +1002,9 @@ class RoomView extends React.Component {
const {
user, baseUrl, theme, navigation, Hide_System_Messages
} = this.props;
const { rid, t, sysMes } = room;
const {
rid, t, sysMes, bannerClosed, announcement
} = room;

return (
<SafeAreaView
Expand All @@ -1003,7 +1019,9 @@ class RoomView extends React.Component {
<Banner
rid={rid}
title={I18n.t('Announcement')}
text={room.announcement}
text={announcement}
bannerClosed={bannerClosed}
closeBanner={this.closeBanner}
theme={theme}
/>
<List
Expand Down
4 changes: 4 additions & 0 deletions app/views/RoomView/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export default StyleSheet.create({
bannerContainer: {
paddingVertical: 12,
paddingHorizontal: 15,
flexDirection: 'row',
alignItems: 'center'
},
bannerText: {
flex: 1
},
bannerModalTitle: {
fontSize: 16,
...sharedStyles.textMedium
Expand Down