about summary refs log tree commit diff
path: root/src/view/com/util/images/ImageLayoutGrid.tsx
blob: 8acab710987eeb554875cab008cabb78f24ad37b (plain) (blame)
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
import React from 'react'
import {
  Image,
  ImageStyle,
  LayoutChangeEvent,
  StyleProp,
  StyleSheet,
  TouchableWithoutFeedback,
  View,
  ViewStyle,
} from 'react-native'

interface Dim {
  width: number
  height: number
}

export type ImageLayoutGridType = 'two' | 'three' | 'four'

export function ImageLayoutGrid({
  type,
  uris,
  onPress,
  style,
}: {
  type: ImageLayoutGridType
  uris: string[]
  onPress?: (index: number) => void
  style?: StyleProp<ViewStyle>
}) {
  const [containerInfo, setContainerInfo] = React.useState<Dim | undefined>()

  const onLayout = (evt: LayoutChangeEvent) => {
    setContainerInfo({
      width: evt.nativeEvent.layout.width,
      height: evt.nativeEvent.layout.height,
    })
  }

  return (
    <View style={style} onLayout={onLayout}>
      {containerInfo ? (
        <ImageLayoutGridInner
          type={type}
          uris={uris}
          onPress={onPress}
          containerInfo={containerInfo}
        />
      ) : undefined}
    </View>
  )
}

function ImageLayoutGridInner({
  type,
  uris,
  onPress,
  containerInfo,
}: {
  type: ImageLayoutGridType
  uris: string[]
  onPress?: (index: number) => void
  containerInfo: Dim
}) {
  const size1 = React.useMemo<ImageStyle>(() => {
    if (type === 'three') {
      const size = (containerInfo.width - 10) / 3
      return {width: size, height: size, resizeMode: 'cover', borderRadius: 4}
    } else {
      const size = (containerInfo.width - 5) / 2
      return {width: size, height: size, resizeMode: 'cover', borderRadius: 4}
    }
  }, [type, containerInfo])
  const size2 = React.useMemo<ImageStyle>(() => {
    if (type === 'three') {
      const size = ((containerInfo.width - 10) / 3) * 2 + 5
      return {width: size, height: size, resizeMode: 'cover', borderRadius: 4}
    } else {
      const size = (containerInfo.width - 5) / 2
      return {width: size, height: size, resizeMode: 'cover', borderRadius: 4}
    }
  }, [type, containerInfo])

  if (type === 'two') {
    return (
      <View style={styles.flexRow}>
        <TouchableWithoutFeedback onPress={() => onPress?.(0)}>
          <Image source={{uri: uris[0]}} style={size1} />
        </TouchableWithoutFeedback>
        <View style={styles.wSpace} />
        <TouchableWithoutFeedback onPress={() => onPress?.(1)}>
          <Image source={{uri: uris[1]}} style={size1} />
        </TouchableWithoutFeedback>
      </View>
    )
  }
  if (type === 'three') {
    return (
      <View style={styles.flexRow}>
        <TouchableWithoutFeedback onPress={() => onPress?.(0)}>
          <Image source={{uri: uris[0]}} style={size2} />
        </TouchableWithoutFeedback>
        <View style={styles.wSpace} />
        <View>
          <TouchableWithoutFeedback onPress={() => onPress?.(1)}>
            <Image source={{uri: uris[1]}} style={size1} />
          </TouchableWithoutFeedback>
          <View style={styles.hSpace} />
          <TouchableWithoutFeedback onPress={() => onPress?.(2)}>
            <Image source={{uri: uris[2]}} style={size1} />
          </TouchableWithoutFeedback>
        </View>
      </View>
    )
  }
  if (type === 'four') {
    return (
      <View style={styles.flexRow}>
        <View>
          <TouchableWithoutFeedback onPress={() => onPress?.(0)}>
            <Image source={{uri: uris[0]}} style={size1} />
          </TouchableWithoutFeedback>
          <View style={styles.hSpace} />
          <TouchableWithoutFeedback onPress={() => onPress?.(1)}>
            <Image source={{uri: uris[1]}} style={size1} />
          </TouchableWithoutFeedback>
        </View>
        <View style={styles.wSpace} />
        <View>
          <TouchableWithoutFeedback onPress={() => onPress?.(2)}>
            <Image source={{uri: uris[2]}} style={size1} />
          </TouchableWithoutFeedback>
          <View style={styles.hSpace} />
          <TouchableWithoutFeedback onPress={() => onPress?.(3)}>
            <Image source={{uri: uris[3]}} style={size1} />
          </TouchableWithoutFeedback>
        </View>
      </View>
    )
  }
  return <View />
}

const styles = StyleSheet.create({
  flexRow: {flexDirection: 'row'},
  wSpace: {width: 5},
  hSpace: {height: 5},
})