Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
485 changes: 485 additions & 0 deletions docs/superpowers/plans/2026-08-26-android-adaptive-layout-fixes.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Android 多机型自适应布局修复设计

## 背景

测试中已确认四个移动端自适应缺陷:日历页日期标题在窄屏被截断、底部语音控件遮挡日程、滚动内容进入 Android 状态栏,以及权限页底部操作区遮挡或挤压权限列表。

仓库中已有一轮安全区和底部留白处理,但当前实现仍把顶部安全区放在可滚动内容里,并用滚动内容末尾的固定留白补偿绝对定位语音栏。这两种方式只改善了初始位置和列表末尾可达性,不能保证滚动过程中的内容始终避开系统栏和语音栏。

当前 USB 真机的物理分辨率为 1440×3200、密度为 560 dpi,顶部挖孔/状态栏区域为 138 px,底部手势导航区域为 56 px。修复不得依赖这些具体数值,而应使用运行时安全区和可用宽度。

## 目标

- 日历、账号操作、语音栏和权限列表在不同 Android 手机宽度、高度、屏幕比例和系统安全区下不截断、不越界、不互相遮挡。
- 支持系统字体缩放,至少覆盖 1.0、1.3 和 1.5 倍字体。
- 保持日历、权限申请和语音交互的现有业务行为。
- 使用自动化布局测试覆盖无法同时获得的多种真机尺寸,并在 USB 真机上做最终视觉回归。

## 非目标

- 不重新设计语音交互、账号入口或权限流程。
- 不修改权限申请条件、权限文案和系统设置跳转行为。
- 不处理与自适应无关的返回键、语音识别或提醒逻辑问题。
- 不专门设计平板双栏界面;平板和横屏只要求不遮挡、不越界并保持可操作。

## 根因

### 日期标题截断

日期标题与用户信息、退出按钮固定在同一行。标题允许收缩,账号区域虽然设置了最大宽度,但在窄屏或字体放大后仍会占用标题所需空间,最终触发日期省略。

### 语音栏遮挡日程

`AssistantVoiceOverlay` 相对整个主屏绝对定位。日历目前只在 `ScrollView` 的内容末尾增加与语音栏近似等高的 `paddingBottom`。该留白能让最后一项继续向上滚动,却没有缩小滚动视口,因此任意处于语音栏下方的日程仍会被遮住。

### 内容进入状态栏

Android 顶部 inset 被添加到 `ScrollView` 的 `contentContainerStyle`。当列表滚动时,这段顶部留白会随内容一起滚走,之后的日历内容便可以进入状态栏和挖孔区域。

### 权限页底部遮挡

权限页的 `ScrollView` 没有明确占据除底部操作区之外的剩余高度,底部操作区也没有使用底部安全区。小高度、手势导航或字体放大时,列表和操作区会竞争高度,最后几项不能稳定滚动到按钮上方。

## 方案选择

采用“安全区边界 + 独立布局区域”的方案,不按设备型号或固定分辨率写补丁。

未选择纯断点方案,因为它需要持续维护屏宽阈值,仍可能遗漏特殊比例、字体缩放和系统栏组合。未选择把语音栏完全改为普通页面组件,因为这会扩大语音气泡、免提展开层和全局交互的改动范围。

## 详细设计

### 日历顶部安全区

- 顶部安全区作用于不可滚动的页面边界或滚动视口,而不是滚动内容本身。
- Android 明确使用 `react-native-safe-area-context` 返回的顶部 inset;iOS 保持系统自动调整或等效的单一调整策略,避免自动和手动 inset 叠加。
- 滚动内容的顶部只保留产品自身间距。滚动到任意位置时,内容都不能出现在状态栏或挖孔下面。

### 日历标题与账号区域

- 常规宽度保持日期和账号操作同一行。
- 当可用宽度不足或字体放大导致内容无法完整显示时,账号操作整体换到下一行并右对齐,日期标题独占首行。
- 日期标题不使用省略号;日期文本必须完整显示。用户名仍允许在账号胶囊内部省略,退出按钮保持固定且触控区域不小于 44 dp。
- 通过弹性布局和内容最小宽度触发换行,不依赖具体手机品牌;如 React Native 布局行为需要确定性阈值,则阈值只基于可用逻辑宽度和字体缩放计算,并集中在可测试的布局函数中。

### 日历与浮动语音栏

- 将语音栏占用空间定义为共享布局契约:底部系统 inset、语音栏实际基础高度和产品间距共同组成保留高度。
- 保留高度应用于日历滚动视口或日历页面容器,使滚动视口的底边位于语音栏上方,而不是只在滚动内容末尾追加空白。
- 基础语音栏不得遮挡日程卡片。临时回复气泡仍可覆盖页面,因为它是可关闭的短时反馈层;其出现不得改变基础语音栏的安全区计算。
- 保持语音栏当前绝对定位和免提展开行为,避免扩大交互改动。

### 权限页

- 权限列表滚动区设置为占据标题和底部操作区之外的全部剩余空间。
- 底部操作区作为滚动区的普通兄弟节点,不使用绝对定位,并增加运行时底部安全区。
- 顶部标题使用顶部安全区;底部按钮和提示始终位于手势导航区上方。
- 列表最后一项必须能完整滚动到操作区上方。在低高度或字体放大时允许更多滚动,不压缩权限说明文本到不可读状态。
- 权限行的按钮和文字区域允许合理收缩;极窄屏下不得水平溢出,操作按钮保持可点击。

## 代码边界

预计修改以下区域:

- `frontend/src/shared/ui/floatingVoiceBarLayout.ts`:集中定义语音栏保留空间计算。
- `frontend/src/features/schedule/presentation/ScheduleCalendarScreen.tsx`:调整顶部安全区、响应式标题布局和滚动视口底部保留。
- `frontend/src/features/reminder/presentation/PermissionOnboardingScreen.tsx`:加入安全区并修正滚动区与底部操作区的高度关系。
- 对应的前端单元测试;只有在组件测试无法稳定表达宽度规则时,才增加纯布局计算模块。

`AssistantVoiceOverlay` 和 `HomeScreen` 只在共享布局契约无法由日历页面正确消费时做最小接口调整,不改语音状态机或业务服务。

## 测试策略

先编写会在当前实现上失败的测试,再修改实现。

### 自动化测试

- 顶部 inset 固定在滚动视口外,不随滚动内容消失。
- 底部语音栏保留空间作用于滚动视口,而非只作用于内容末尾。
- 320、360、393、430 dp 宽度下日期完整;窄屏时账号区域换行,常规屏保持单行。
- 字体缩放 1.0、1.3、1.5 时标题、权限说明和按钮不发生不可恢复截断或水平溢出。
- 顶部 inset 为 0 和非 0、底部 inset 为 0 和非 0 时,布局计算正确且不重复叠加。
- 权限页滚动区为剩余高度,底部操作区包含底部安全区,最后一项可滚动到操作区上方。
- 既有日历刷新、权限交互和语音控件测试继续通过。

### 真机回归

在当前 USB Android 真机上验证:

1. 日历初始页面和滚动到中部/底部时,内容均不进入状态栏。
2. 任意日程卡片可滚动到语音栏上方完整显示。
3. 日期完整显示,长用户名不会挤掉日期或退出按钮。
4. 权限页从顶部滚动到底部,每个权限项和“进入 App”按钮都能完整显示并点击。
5. 分别使用默认字体和放大字体检查页面;测试后恢复设备原有字体设置。

如可重新连接 MuMu 模拟器,再补充较窄分辨率和权限未授予状态的视觉回归;模拟器不可用不阻止自动化多尺寸测试和 USB 真机验收。

## 验收标准

1. 不按型号、分辨率或状态栏像素写特殊分支,布局基于逻辑尺寸和系统安全区。
2. 320 dp 及以上的 Android 手机竖屏中,日期、账号操作和权限按钮无水平越界;日期不省略。
3. 日历滚动过程中,状态栏/挖孔区域和基础语音栏区域内不出现日程内容。
4. 权限页最后一项能完整滚动到固定操作区上方,操作区不侵入底部手势导航区域。
5. 1.5 倍系统字体下仍可通过滚动访问所有内容和操作。
6. 相关前端单元测试、类型检查和 lint 通过,并在 USB 真机完成截图或控件边界验证。

## 风险与控制

- 不同平台对 `contentInsetAdjustmentBehavior` 的处理不同:同一方向只保留一种 inset 来源,并通过 Android/iOS 分支测试防止重复间距。
- 账号区域换行可能增加标题高度:依赖自然文档流,不设置固定 header 高度。
- 语音栏共享尺寸变更可能导致日历保留空间失配:保留高度与控件高度引用同一常量,并用单元测试锁定。
- 系统字体回归会改变测试设备设置:变更前记录原值,测试结束后恢复。
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import { ActivityIndicator, Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import type {
DeviceCapabilityPort,
Expand Down Expand Up @@ -109,6 +110,7 @@ export function PermissionOnboardingScreen({
readonly onContinue: () => void;
readonly onPermissionsUpdated: () => void;
}) {
const insets = useSafeAreaInsets();
const [status, setStatus] = useState<DeviceCapabilityStatus | null>(null);
const [busyPermission, setBusyPermission] = useState<DevicePermission | null>(null);
const [busyOemKind, setBusyOemKind] = useState<'autostart' | 'backgroundPopup' | null>(null);
Expand Down Expand Up @@ -179,8 +181,12 @@ export function PermissionOnboardingScreen({
});

return (
<View style={styles.screen}>
<ScrollView contentContainerStyle={styles.content}>
<View style={[styles.screen, { paddingTop: insets.top }]} testID="permission-onboarding-screen">
<ScrollView
contentContainerStyle={styles.content}
style={styles.scroll}
testID="permission-list-scroll"
>
<Text style={styles.title}>需要这些权限</Text>
<Text style={styles.subtitle}>
点击每一项单独开启;通知是必需的,其余可以先跳过,用到对应功能时会再提醒你。
Expand All @@ -198,7 +204,7 @@ export function PermissionOnboardingScreen({
style={[styles.row, highlighted ? styles.rowHighlighted : null]}
testID={`permission-row-${row.permission}`}
>
<View style={styles.rowText}>
<View style={styles.rowText} testID={`permission-copy-${row.permission}`}>
<View style={styles.rowTitleLine}>
<Text style={styles.rowTitle}>{row.title}</Text>
{row.required ? <Text style={styles.requiredBadge}>必需</Text> : null}
Expand Down Expand Up @@ -272,7 +278,10 @@ export function PermissionOnboardingScreen({
</>
) : null}
</ScrollView>
<View style={styles.footer}>
<View
style={[styles.footer, { paddingBottom: Math.max(spacing.lg, insets.bottom + spacing.md) }]}
testID="permission-footer"
>
{!notificationsGranted ? (
<Text style={styles.footerHint}>需要先开启通知权限才能进入</Text>
) : null}
Expand All @@ -298,6 +307,7 @@ const styles = StyleSheet.create({
actionButton: {
backgroundColor: colors.text,
borderRadius: 8,
flexShrink: 0,
minWidth: 84,
paddingHorizontal: spacing.md,
paddingVertical: spacing.sm,
Expand Down Expand Up @@ -389,9 +399,11 @@ const styles = StyleSheet.create({
rowText: {
flex: 1,
gap: spacing.xs,
minWidth: 0,
},
rowTitle: {
color: colors.text,
flexShrink: 1,
fontSize: 15,
fontWeight: '700',
},
Expand All @@ -414,6 +426,9 @@ const styles = StyleSheet.create({
backgroundColor: colors.background,
flex: 1,
},
scroll: {
flex: 1,
},
subtitle: {
color: colors.mutedText,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
ScrollView,
StyleSheet,
Text,
useWindowDimensions,
View,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import Svg, { Path } from 'react-native-svg';

import { floatingVoiceContentBottomInset } from '../../../shared/ui/floatingVoiceBarLayout';
import { floatingVoiceViewportBottomInset } from '../../../shared/ui/floatingVoiceBarLayout';
import { colors, spacing } from '../../../shared/ui/theme';
import type { ScheduleCalendarReadService, ScheduleOccurrenceView } from '../application';
import { LocationScheduleDetailSheet } from './LocationScheduleDetailSheet';
Expand Down Expand Up @@ -65,39 +66,45 @@ export function ScheduleCalendarScreen({
);
const [selectedOccurrenceKey, setSelectedOccurrenceKey] = useState<string | null>(null);
const [selectedLocationId, setSelectedLocationId] = useState<string | null>(null);
const { fontScale, width } = useWindowDimensions();
const selectedOccurrence =
calendar.selectedOccurrences.find((item) => occurrenceKey(item) === selectedOccurrenceKey) ??
null;
const selectedLocation =
calendar.locationSchedules.find((item) => item.scheduleId === selectedLocationId) ?? null;
const topSafeAreaPadding = Platform.OS === 'android' ? insets.top : 0;
const stackHeader = shouldStackScheduleHeader(width, fontScale);
const selectedLabel = SELECTED_DATE_FORMATTER.format(calendar.selectedDate);
const agendaTitle = formatAgendaSectionTitle(calendar.selectedDate);
const emptyAgenda = emptyAgendaMessage(calendar.selectedDate);
const displayUsername = username.trim() || '用户';
const avatarInitial = Array.from(displayUsername)[0]?.toLocaleUpperCase() ?? '用';

return (
<View style={styles.screen}>
<View
style={[styles.screen, { paddingTop: topSafeAreaPadding }]}
testID="schedule-calendar-screen"
>
<ScrollView
contentContainerStyle={[
styles.scrollContent,
{
paddingBottom: floatingVoiceContentBottomInset(insets.bottom),
paddingTop: topSafeAreaPadding,
},
]}
contentInsetAdjustmentBehavior="automatic"
contentContainerStyle={styles.scrollContent}
contentInsetAdjustmentBehavior={Platform.OS === 'ios' ? 'automatic' : 'never'}
showsVerticalScrollIndicator={false}
style={{ marginBottom: floatingVoiceViewportBottomInset(insets.bottom) }}
testID="schedule-calendar-scroll"
>
<View style={styles.content}>
<View style={styles.header}>
<View style={styles.headerTop}>
<Text testID="schedule-selected-date" style={styles.title}>
<View style={[styles.headerTop, stackHeader && styles.headerTopStacked]}>
<Text
testID="schedule-selected-date"
style={[styles.title, stackHeader && styles.titleStacked]}
>
{selectedLabel}
</Text>
<View style={styles.accountActions} testID="schedule-account-actions">
<View
style={[styles.accountActions, stackHeader && styles.accountActionsStacked]}
testID="schedule-account-actions"
>
<Pressable
accessibilityLabel={`当前用户 ${displayUsername},点击查看权限设置`}
accessibilityRole="button"
Expand Down Expand Up @@ -221,6 +228,12 @@ export function ScheduleCalendarScreen({
);
}

export function shouldStackScheduleHeader(width: number, fontScale: number): boolean {
if (width <= 0) return false;
const effectiveWidth = width / Math.max(fontScale, 1);
return effectiveWidth < 400;
}

function occurrenceKey(item: ScheduleOccurrenceView): string {
return `${item.scheduleId}\u0000${item.occurrenceStart ?? ''}`;
}
Expand Down Expand Up @@ -252,6 +265,11 @@ const styles = StyleSheet.create({
maxWidth: 168,
minWidth: 0,
},
accountActionsStacked: {
alignSelf: 'flex-end',
flexShrink: 0,
marginLeft: 0,
},
agenda: { paddingHorizontal: spacing.md, paddingTop: spacing.xl },
avatar: {
alignItems: 'center',
Expand Down Expand Up @@ -306,6 +324,11 @@ const styles = StyleSheet.create({
minWidth: 0,
width: '100%',
},
headerTopStacked: {
alignItems: 'stretch',
flexDirection: 'column',
gap: spacing.sm,
},
locationSection: {
borderTopColor: colors.border,
borderTopWidth: StyleSheet.hairlineWidth,
Expand Down Expand Up @@ -351,6 +374,10 @@ const styles = StyleSheet.create({
minWidth: 0,
flexShrink: 1,
},
titleStacked: {
flex: 0,
width: '100%',
},
userPill: {
alignItems: 'center',
backgroundColor: colors.surface,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/shared/ui/floatingVoiceBarLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export function floatingVoiceBarBottomOffset(bottomInset: number): number {
return Math.max(spacing.xl, bottomInset + spacing.md);
}

export function floatingVoiceContentBottomInset(bottomInset: number): number {
export function floatingVoiceViewportBottomInset(bottomInset: number): number {
return floatingVoiceBarBottomOffset(bottomInset) + FLOATING_VOICE_BAR_HEIGHT + spacing.md;
}
4 changes: 4 additions & 0 deletions frontend/tests/unit/app/AppRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import type {
import { FakeAuthSessionStore } from '../../fakes/FakeAuthSessionStore';
import { openTimeflowDatabase } from '../../../src/infrastructure/database';

jest.mock('react-native-safe-area-context', () => ({
useSafeAreaInsets: () => ({ bottom: 0, left: 0, right: 0, top: 0 }),
}));

jest.mock('../../../src/infrastructure/database', () => ({
openTimeflowDatabase: jest
.fn<() => Promise<unknown>>()
Expand Down
Loading
Loading