-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStoneCameraApp.kt
More file actions
196 lines (177 loc) · 7.19 KB
/
StoneCameraApp.kt
File metadata and controls
196 lines (177 loc) · 7.19 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// StoneCameraApp.kt
package co.stonephone.stonecamera
import android.annotation.SuppressLint
import android.graphics.Rect
import androidx.annotation.OptIn
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import co.stonephone.stonecamera.plugins.AspectRatioPlugin
import co.stonephone.stonecamera.plugins.FlashPlugin
import co.stonephone.stonecamera.plugins.FocusBasePlugin
import co.stonephone.stonecamera.plugins.PhotoModePlugin
import co.stonephone.stonecamera.plugins.PinchToZoomPlugin
import co.stonephone.stonecamera.plugins.QRScannerPlugin
import co.stonephone.stonecamera.plugins.SettingLocation
import co.stonephone.stonecamera.plugins.SettingsTrayPlugin
import co.stonephone.stonecamera.plugins.ShutterFlashPlugin
import co.stonephone.stonecamera.plugins.TapToFocusPlugin
import co.stonephone.stonecamera.plugins.VolumeControlsPlugin
import co.stonephone.stonecamera.plugins.VideoModePlugin
import co.stonephone.stonecamera.plugins.ZoomBarPlugin
import co.stonephone.stonecamera.plugins.ZoomBasePlugin
import co.stonephone.stonecamera.ui.RenderPluginSetting
import co.stonephone.stonecamera.ui.ResponsiveOrientation
import co.stonephone.stonecamera.ui.StoneCameraPreview
import co.stonephone.stonecamera.utils.calculateImageCoverageRegion
import co.stonephone.stonecamera.utils.getAllCamerasInfo
// Order here is important, they are loaded and initialised in the order they are listed
// ZoomBar depends on ZoomBase, etc.
val PLUGINS = listOf(
QRScannerPlugin(),
ZoomBasePlugin(),
ZoomBarPlugin(),
FocusBasePlugin(),
TapToFocusPlugin(),
PinchToZoomPlugin(),
FlashPlugin(),
AspectRatioPlugin(),
ShutterFlashPlugin(),
VolumeControlsPlugin(),
PhotoModePlugin(),
VideoModePlugin(),
SettingsTrayPlugin()
// DebugPlugin()
)
@OptIn(
ExperimentalCamera2Interop::class
)
@SuppressLint("ClickableViewAccessibility")
@Composable
fun StoneCameraApp(
cameraProvider: ProcessCameraProvider,
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val stoneCameraViewModel = viewModel<StoneCameraViewModel>(
factory = StoneCameraViewModelFactory(context, lifecycleOwner, PLUGINS)
)
val selectedMode = stoneCameraViewModel.selectedMode
val plugins by remember { stoneCameraViewModel::plugins }
val modePlugins = plugins.filter { it.modeLabel != null }
val previewView = stoneCameraViewModel.previewView
val imageCapture = stoneCameraViewModel.imageCapture
val activeModePlugin = modePlugins.find { it.modeLabel == selectedMode }
var viewfinderDimensions by remember { mutableStateOf<Rect?>(null) }
LaunchedEffect(cameraProvider, lifecycleOwner) {
stoneCameraViewModel.onCameraProvider(cameraProvider)
stoneCameraViewModel.onLifecycleOwner(lifecycleOwner)
}
LaunchedEffect(previewView, imageCapture) {
if (previewView != null && imageCapture != null) {
viewfinderDimensions = calculateImageCoverageRegion(
previewView, imageCapture
)
}
}
// We can load cameras once (or whenever context changes) and pass them to the ViewModel
LaunchedEffect(Unit) {
val allCameras = getAllCamerasInfo(context)
stoneCameraViewModel.loadCameras(allCameras)
}
Box(modifier = Modifier.fillMaxSize()) {
// Camera preview behind everything
StoneCameraPreview(
onPreviewView = { pView -> stoneCameraViewModel.onPreviewView(pView) },
)
Row(
modifier = Modifier
.fillMaxWidth()
.align(Alignment.TopCenter)
.windowInsetsPadding(WindowInsets.systemBars),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
stoneCameraViewModel.pluginSettings.filter { it.renderLocation == SettingLocation.TOP }
.map { setting ->
ResponsiveOrientation {
RenderPluginSetting(
setting, stoneCameraViewModel, modifier = Modifier.padding(4.dp)
)
}
}
}
viewfinderDimensions?.let {
Box(
modifier = Modifier
.width(it.width().dp)
.height(it.height().dp)
.offset(x = it.left.toFloat().dp, y = it.top.toFloat().dp)
) {
plugins.map {
it.renderViewfinder(stoneCameraViewModel, it)
}
}
}
// Bottom controls
Column(
modifier = Modifier
.fillMaxWidth()
.align(Alignment.BottomCenter),
verticalArrangement = Arrangement.SpaceBetween
) {
Box(
modifier = Modifier.fillMaxWidth(),
) {
plugins.map {
it.renderTray(stoneCameraViewModel, it)
}
}
// Translucent overlay for mode switch & shutter
Box(
modifier = Modifier
.fillMaxWidth()
.background(Color.Black.copy(alpha = 0.5f))
.padding(16.dp), contentAlignment = Alignment.BottomCenter
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Bottom
) {
// Mode selector
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 8.dp),
horizontalArrangement = Arrangement.SpaceEvenly
) {
modePlugins.forEach { modePlugin ->
val modeLabel = modePlugin.modeLabel!!
Text(text = modeLabel.resolve().uppercase(),
color = if (modeLabel.resolve() == selectedMode.resolve()) Color(0xFFFFCC00) else Color.White,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.clickable {
stoneCameraViewModel.selectMode(modeLabel)
})
}
}
activeModePlugin?.renderModeControl?.invoke()
}
}
}
}
}