Change time format of RoomItems

This commit is contained in:
Danish Ahmed Mirza 2022-06-03 02:50:46 +05:30
parent 8b4cc16548
commit 7df61a8621
13 changed files with 37 additions and 43 deletions

View File

@ -46,31 +46,22 @@ const styles = StyleSheet.create({
interface IMessageBoxReplyPreview { interface IMessageBoxReplyPreview {
replying: boolean; replying: boolean;
message: IMessage; message: IMessage;
Message_TimeFormat: string;
close(): void; close(): void;
baseUrl: string; baseUrl: string;
username: string; username: string;
getCustomEmoji: Function; getCustomEmoji: Function;
useRealName: boolean; useRealName: boolean;
timeFormat: number; timeFormat: string;
} }
const ReplyPreview = React.memo( const ReplyPreview = React.memo(
({ message, Message_TimeFormat, replying, close, useRealName, timeFormat }: IMessageBoxReplyPreview) => { ({ message, replying, close, useRealName, timeFormat }: IMessageBoxReplyPreview) => {
const { theme } = useTheme(); const { theme } = useTheme();
const getTimeFormat = () => {
const timeFormats = ['h:mm A', 'H:mm'];
if (timeFormat) {
return timeFormats[timeFormat - 1];
}
return Message_TimeFormat;
};
if (!replying) { if (!replying) {
return null; return null;
} }
const time = moment(message.ts).format(getTimeFormat()); const time = moment(message.ts).format(timeFormat);
return ( return (
<View style={[styles.container, { backgroundColor: themes[theme].messageboxBackground }]}> <View style={[styles.container, { backgroundColor: themes[theme].messageboxBackground }]}>
<View style={[styles.messageContainer, { backgroundColor: themes[theme].chatComponentBackground }]}> <View style={[styles.messageContainer, { backgroundColor: themes[theme].chatComponentBackground }]}>
@ -91,7 +82,6 @@ const ReplyPreview = React.memo(
); );
const mapStateToProps = (state: IApplicationState) => ({ const mapStateToProps = (state: IApplicationState) => ({
Message_TimeFormat: state.settings.Message_TimeFormat as string,
baseUrl: state.server.server, baseUrl: state.server.server,
useRealName: state.settings.UI_Use_Real_Name as boolean useRealName: state.settings.UI_Use_Real_Name as boolean
}); });

View File

@ -120,13 +120,14 @@ class RoomItemContainer extends React.Component<IRoomItemContainerProps, any> {
swipeEnabled, swipeEnabled,
autoJoin, autoJoin,
showAvatar, showAvatar,
displayMode displayMode,
timeFormat
} = this.props; } = this.props;
const name = getRoomTitle(item); const name = getRoomTitle(item);
const testID = `rooms-list-view-item-${name}`; const testID = `rooms-list-view-item-${name}`;
const avatar = getRoomAvatar(item); const avatar = getRoomAvatar(item);
const isRead = getIsRead(item); const isRead = getIsRead(item);
const date = item.roomUpdatedAt && formatDate(item.roomUpdatedAt); const date = item.roomUpdatedAt && formatDate(item.roomUpdatedAt, timeFormat);
const alert = item.alert || item.tunread?.length; const alert = item.alert || item.tunread?.length;
let accessibilityLabel = name; let accessibilityLabel = name;

View File

@ -92,6 +92,7 @@ export interface IRoomItemContainerProps {
autoJoin: boolean; autoJoin: boolean;
showAvatar: boolean; showAvatar: boolean;
displayMode: string; displayMode: string;
timeFormat: string;
} }
export interface IRoomItemProps { export interface IRoomItemProps {

View File

@ -21,7 +21,7 @@ export interface ILoggedUser {
showMessageInMainThread?: boolean; showMessageInMainThread?: boolean;
isFromWebView?: boolean; isFromWebView?: boolean;
enableMessageParserEarlyAdoption: boolean; enableMessageParserEarlyAdoption: boolean;
timeFormat: number; timeFormat: string;
} }
export interface ILoggedUserResultFromServer export interface ILoggedUserResultFromServer

View File

@ -109,7 +109,7 @@ export default schemaMigrations({
steps: [ steps: [
addColumns({ addColumns({
table: 'users', table: 'users',
columns: [{ name: 'time_format', type: 'number', isOptional: true }] columns: [{ name: 'time_format', type: 'string', isOptional: true }]
}) })
] ]
} }

View File

@ -18,7 +18,7 @@ export default appSchema({
{ name: 'avatar_etag', type: 'string', isOptional: true }, { name: 'avatar_etag', type: 'string', isOptional: true },
{ name: 'is_from_webview', type: 'boolean', isOptional: true }, { name: 'is_from_webview', type: 'boolean', isOptional: true },
{ name: 'enable_message_parser_early_adoption', type: 'boolean', isOptional: true }, { name: 'enable_message_parser_early_adoption', type: 'boolean', isOptional: true },
{ name: 'time_format', type: 'number', isOptional: true } { name: 'time_format', type: 'string', isOptional: true }
] ]
}), }),
tableSchema({ tableSchema({

View File

@ -296,7 +296,7 @@ export default function subscribeRooms() {
store.dispatch(setUser({ showMessageInMainThread: diff['settings.preferences.showMessageInMainThread'] })); store.dispatch(setUser({ showMessageInMainThread: diff['settings.preferences.showMessageInMainThread'] }));
} }
if ((['settings.preferences.clockMode'] as any) in diff) { if ((['settings.preferences.clockMode'] as any) in diff) {
store.dispatch(setUser({ timeFormat: diff['settings.preferences.clockMode'] })); store.dispatch(setUser({ timeFormat: diff['settings.preferences.clockMode'] === 2 ? 'H:mm' : 'h:mm A' }));
} }
} }
if (/subscriptions/.test(ev)) { if (/subscriptions/.test(ev)) {

View File

@ -286,7 +286,7 @@ async function login(credentials: ICredentials, isFromWebView = false): Promise<
isFromWebView, isFromWebView,
showMessageInMainThread: result.me.settings?.preferences?.showMessageInMainThread ?? true, showMessageInMainThread: result.me.settings?.preferences?.showMessageInMainThread ?? true,
enableMessageParserEarlyAdoption: result.me.settings?.preferences?.enableMessageParserEarlyAdoption ?? true, enableMessageParserEarlyAdoption: result.me.settings?.preferences?.enableMessageParserEarlyAdoption ?? true,
timeFormat: result.me.settings?.preferences?.clockMode ?? 0 timeFormat: result.me.settings?.preferences?.clockMode === 2 ? 'H:mm' : 'h:mm A'
}; };
return user; return user;
} }

View File

@ -23,10 +23,10 @@ export const capitalize = (s: string): string => {
return s.charAt(0).toUpperCase() + s.slice(1); return s.charAt(0).toUpperCase() + s.slice(1);
}; };
export const formatDate = (date: string | Date): string => export const formatDate = (date: string | Date, timeFormat: string | undefined): string =>
moment(date).calendar(null, { moment(date).calendar(null, {
lastDay: `[${I18n.t('Yesterday')}]`, lastDay: `[${I18n.t('Yesterday')}]`,
sameDay: 'LT', sameDay: timeFormat || 'LT',
lastWeek: 'dddd', lastWeek: 'dddd',
sameElse: 'L' sameElse: 'L'
}); });

View File

@ -144,7 +144,6 @@ interface IRoomViewProps extends IBaseScreen<ChatsStackParamList, 'RoomView'> {
useRealName?: boolean; useRealName?: boolean;
isAuthenticated: boolean; isAuthenticated: boolean;
Message_GroupingPeriod?: number; Message_GroupingPeriod?: number;
Message_TimeFormat?: string;
Message_Read_Receipt_Enabled?: boolean; Message_Read_Receipt_Enabled?: boolean;
Hide_System_Messages?: string[]; Hide_System_Messages?: string[];
baseUrl: string; baseUrl: string;
@ -1183,19 +1182,9 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
}); });
}; };
getMessageTimeFormat = () => {
const { user, Message_TimeFormat } = this.props;
const timeFormats = ['h:mm A', 'H:mm'];
if (user.timeFormat) {
return timeFormats[user.timeFormat - 1];
}
return Message_TimeFormat;
};
renderItem = (item: TAnyMessageModel, previousItem: TAnyMessageModel, highlightedMessage?: string) => { renderItem = (item: TAnyMessageModel, previousItem: TAnyMessageModel, highlightedMessage?: string) => {
const { room, lastOpen, canAutoTranslate } = this.state; const { room, lastOpen, canAutoTranslate } = this.state;
const { user, Message_GroupingPeriod, useRealName, baseUrl, Message_Read_Receipt_Enabled, theme } = this.props; const { user, Message_GroupingPeriod, useRealName, baseUrl, Message_Read_Receipt_Enabled, theme } = this.props;
const Message_TimeFormat = this.getMessageTimeFormat();
let dateSeparator = null; let dateSeparator = null;
let showUnreadSeparator = false; let showUnreadSeparator = false;
@ -1246,7 +1235,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
isSystemMessage={room.sysMes as boolean} isSystemMessage={room.sysMes as boolean}
baseUrl={baseUrl} baseUrl={baseUrl}
Message_GroupingPeriod={Message_GroupingPeriod} Message_GroupingPeriod={Message_GroupingPeriod}
timeFormat={Message_TimeFormat} timeFormat={user.timeFormat}
useRealName={useRealName} useRealName={useRealName}
isReadReceiptEnabled={Message_Read_Receipt_Enabled} isReadReceiptEnabled={Message_Read_Receipt_Enabled}
autoTranslateRoom={canAutoTranslate && 'id' in room && room.autoTranslate} autoTranslateRoom={canAutoTranslate && 'id' in room && room.autoTranslate}
@ -1455,7 +1444,6 @@ const mapStateToProps = (state: IApplicationState) => ({
useRealName: state.settings.UI_Use_Real_Name as boolean, useRealName: state.settings.UI_Use_Real_Name as boolean,
isAuthenticated: state.login.isAuthenticated, isAuthenticated: state.login.isAuthenticated,
Message_GroupingPeriod: state.settings.Message_GroupingPeriod as number, Message_GroupingPeriod: state.settings.Message_GroupingPeriod as number,
Message_TimeFormat: state.settings.Message_TimeFormat as string,
customEmojis: state.customEmojis, customEmojis: state.customEmojis,
baseUrl: state.server.server, baseUrl: state.server.server,
serverVersion: state.server.version, serverVersion: state.server.version,

View File

@ -936,7 +936,7 @@ class RoomsListView extends React.Component<IRoomsListViewProps, IRoomsListViewS
const { item: currentItem } = this.state; const { item: currentItem } = this.state;
const { const {
user: { username }, user: { username, timeFormat },
StoreLastMessage, StoreLastMessage,
useRealName, useRealName,
theme, theme,
@ -972,6 +972,7 @@ class RoomsListView extends React.Component<IRoomsListViewProps, IRoomsListViewS
swipeEnabled={swipeEnabled} swipeEnabled={swipeEnabled}
showAvatar={showAvatar} showAvatar={showAvatar}
displayMode={displayMode} displayMode={displayMode}
timeFormat={timeFormat}
/> />
); );
}; };

View File

@ -17,7 +17,7 @@ import RoomHeader from '../containers/RoomHeader';
import SafeAreaView from '../containers/SafeAreaView'; import SafeAreaView from '../containers/SafeAreaView';
import SearchHeader from '../containers/SearchHeader'; import SearchHeader from '../containers/SearchHeader';
import StatusBar from '../containers/StatusBar'; import StatusBar from '../containers/StatusBar';
import { IApplicationState, IBaseScreen, TSubscriptionModel } from '../definitions'; import { IApplicationState, IBaseScreen, TSubscriptionModel, IUser } from '../definitions';
import { ERoomType } from '../definitions/ERoomType'; import { ERoomType } from '../definitions/ERoomType';
import { withDimensions } from '../dimensions'; import { withDimensions } from '../dimensions';
import I18n from '../i18n'; import I18n from '../i18n';
@ -88,6 +88,7 @@ interface ITeamChannelsViewProps extends IBaseScreen<ChatsStackParamList, 'TeamC
showActionSheet: (options: TActionSheetOptions) => void; showActionSheet: (options: TActionSheetOptions) => void;
showAvatar: boolean; showAvatar: boolean;
displayMode: DisplayMode; displayMode: DisplayMode;
user: IUser;
} }
class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChannelsViewState> { class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChannelsViewState> {
private teamId: string; private teamId: string;
@ -496,7 +497,15 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
}; };
renderItem = ({ item }: { item: IItem }) => { renderItem = ({ item }: { item: IItem }) => {
const { StoreLastMessage, useRealName, theme, width, showAvatar, displayMode } = this.props; const {
user: { timeFormat },
StoreLastMessage,
useRealName,
theme,
width,
showAvatar,
displayMode
} = this.props;
return ( return (
<RoomItem <RoomItem
item={item} item={item}
@ -513,6 +522,7 @@ class TeamChannelsView extends React.Component<ITeamChannelsViewProps, ITeamChan
autoJoin={item.teamDefault} autoJoin={item.teamDefault}
showAvatar={showAvatar} showAvatar={showAvatar}
displayMode={displayMode} displayMode={displayMode}
timeFormat={timeFormat}
/> />
); );
}; };

View File

@ -29,16 +29,19 @@ const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Ele
interface ITimeFormats { interface ITimeFormats {
label: string; label: string;
value: number; value: number;
format: string;
} }
const timeFormats: ITimeFormats[] = [ const timeFormats: ITimeFormats[] = [
{ {
label: '12_Hour', label: '12_Hour',
value: 1 value: 1,
format: 'h:mm A'
}, },
{ {
label: '24_Hour', label: '24_Hour',
value: 2 value: 2,
format: 'H:mm'
} }
]; ];
@ -70,7 +73,7 @@ const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Ele
const onChangeTimeFormat = async (item: ITimeFormats) => { const onChangeTimeFormat = async (item: ITimeFormats) => {
try { try {
dispatch(setUser({ timeFormat: item.value })); dispatch(setUser({ timeFormat: item.format }));
await Services.saveUserPreferences({ id, clockMode: item.value }); await Services.saveUserPreferences({ id, clockMode: item.value });
} catch (e) { } catch (e) {
log(e); log(e);
@ -105,13 +108,13 @@ const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Ele
<List.Item <List.Item
title={timeFormats[0].label} title={timeFormats[0].label}
onPress={() => onChangeTimeFormat(timeFormats[0])} onPress={() => onChangeTimeFormat(timeFormats[0])}
right={() => (timeFormat === timeFormats[0].value ? renderIcon() : null)} right={() => (timeFormat === timeFormats[0].format ? renderIcon() : null)}
/> />
<List.Separator /> <List.Separator />
<List.Item <List.Item
title={timeFormats[1].label} title={timeFormats[1].label}
onPress={() => onChangeTimeFormat(timeFormats[1])} onPress={() => onChangeTimeFormat(timeFormats[1])}
right={() => (timeFormat === timeFormats[1].value ? renderIcon() : null)} right={() => (timeFormat === timeFormats[1].format ? renderIcon() : null)}
/> />
<List.Separator /> <List.Separator />
</List.Section> </List.Section>