@@ -32,31 +32,13 @@ export default function App() {
3232 const [ selectedContacts , setSelectedContacts ] = useState ( { } ) ;
3333 const [ permissionStatus , setPermissionStatus ] = useState ( null ) ;
3434 const [ targetContacts , setTargetContacts ] = useState ( [ ] ) ;
35- const [ currentView , setCurrentView ] = useState ( VIEW_TYPES . SAVED_TARGETS ) ; // 현재 화면 상태
3635
3736 useEffect ( ( ) => {
3837 console . log ( "App useEffect triggered - Initial load" ) ;
39- loadSavedTargetContacts ( ) ; // 앱 시작 시 저장된 타겟 로드
40- requestInitialPermission ( ) ; // 앱 시작 시 권한 상태 확인 및 요청 (UI 블록 방지)
38+ loadSavedTargetContacts ( ) ;
39+ requestInitialPermission ( ) ;
4140 } , [ ] ) ;
4241
43- useEffect ( ( ) => {
44- // DeviceContactsScreen으로 전환될 때, 그리고 targetContacts나 contacts가 변경될 때 selectedContacts 업데이트
45- if ( currentView === VIEW_TYPES . DEVICE_CONTACTS && contacts . length > 0 ) {
46- console . log ( "Updating selectedContacts for DeviceContactsScreen" ) ;
47- const newSelected = { } ;
48- targetContacts . forEach ( target => {
49- if ( contacts . find ( c => c . id === target . id ) ) {
50- newSelected [ target . id ] = true ;
51- }
52- } ) ;
53- setSelectedContacts ( newSelected ) ;
54- } else if ( currentView !== VIEW_TYPES . DEVICE_CONTACTS ) {
55- // 다른 뷰로 전환 시 selectedContacts 초기화 (선택 사항)
56- // setSelectedContacts({});
57- }
58- } , [ targetContacts , currentView , contacts ] ) ;
59-
6042 const requestInitialPermission = async ( ) => {
6143 console . log ( "Requesting initial contacts permission..." ) ;
6244 const { status } = await Contacts . requestPermissionsAsync ( ) ;
@@ -82,8 +64,8 @@ export default function App() {
8264
8365 const loadDeviceContacts = async ( ) => {
8466 if ( permissionStatus !== 'granted' ) {
85- Alert . alert ( "Permission Required" , "Contacts permission is needed. Please check settings or grant via button. " ) ;
86- return false ;
67+ Alert . alert ( "Permission Required" , "Contacts permission is needed." ) ;
68+ return null ;
8769 }
8870 console . log ( "loadDeviceContacts function called" ) ;
8971 try {
@@ -93,15 +75,16 @@ export default function App() {
9375 console . log ( "Device contacts loaded. Count:" , data ? data . length : 0 ) ;
9476 if ( data && data . length > 0 ) {
9577 setContacts ( data ) ;
78+ return data ;
9679 } else {
9780 setContacts ( [ ] ) ;
9881 console . log ( "No contacts found on device." ) ;
82+ return [ ] ;
9983 }
100- return true ;
10184 } catch ( error ) {
10285 console . error ( "Error loading device contacts:" , error ) ;
10386 Alert . alert ( "Error" , "Failed to load device contacts. " + error . message ) ;
104- return false ;
87+ return null ;
10588 }
10689 } ;
10790
@@ -120,7 +103,6 @@ export default function App() {
120103 setTargetContacts ( newTargetContacts ) ;
121104 console . log ( 'Target contacts updated in AsyncStorage:' , newTargetContacts . length ) ;
122105 Alert . alert ( "Targets Updated" , `Successfully updated ${ newTargetContacts . length } call targets!` ) ;
123- setCurrentView ( VIEW_TYPES . SAVED_TARGETS ) ;
124106 } catch ( e ) {
125107 console . error ( "Failed to save target contacts to AsyncStorage" , e ) ;
126108 Alert . alert ( "Error" , "Failed to update target contacts." ) ;
@@ -140,21 +122,32 @@ export default function App() {
140122 }
141123 } ;
142124
143- const switchToDeviceContactsView = async ( ) => {
144- if ( permissionStatus !== 'granted' ) {
145- console . log ( "Requesting permission before switching to device contacts view..." ) ;
125+ const handlePlusButtonPress = async ( navigation ) => {
126+ let currentPermissionStatus = permissionStatus ;
127+ if ( currentPermissionStatus !== 'granted' ) {
128+ console . log ( "Requesting permission before navigating to device contacts view..." ) ;
146129 const { status } = await Contacts . requestPermissionsAsync ( ) ;
147130 setPermissionStatus ( status ) ;
148- if ( status !== 'granted' ) {
149- Alert . alert ( "Permission Denied" , "Contacts permission is required to manage targets from device list." ) ;
150- return ;
151- }
131+ currentPermissionStatus = status ;
132+ }
133+
134+ if ( currentPermissionStatus !== 'granted' ) {
135+ Alert . alert ( "Permission Denied" , "Contacts permission is required to manage targets from the device list." ) ;
136+ return ;
152137 }
153- const loaded = await loadDeviceContacts ( ) ;
154- if ( loaded ) {
155- setCurrentView ( VIEW_TYPES . DEVICE_CONTACTS ) ;
156- } else {
157- Alert . alert ( "Load Failed" , "Could not load device contacts. Please try again." ) ;
138+
139+ const loadedDeviceContacts = await loadDeviceContacts ( ) ;
140+
141+ if ( loadedDeviceContacts !== null ) {
142+ const newSelected = { } ;
143+ targetContacts . forEach ( target => {
144+ if ( Array . isArray ( loadedDeviceContacts ) && loadedDeviceContacts . find ( c => c . id === target . id ) ) {
145+ newSelected [ target . id ] = true ;
146+ }
147+ } ) ;
148+ setSelectedContacts ( newSelected ) ;
149+
150+ navigation . navigate ( 'DeviceContacts' ) ;
158151 }
159152 } ;
160153
@@ -176,42 +169,37 @@ export default function App() {
176169 component = { ResultScreen }
177170 options = { { title : '추천 결과' } }
178171 />
179- < Stack . Screen name = "SavedTargets" >
172+ < Stack . Screen
173+ name = "SavedTargets"
174+ options = { { headerShown : false } }
175+ >
180176 { ( { navigation } ) => (
181- < View style = { styles . container } >
182- < SavedTargetsScreen
183- targetContacts = { targetContacts }
184- onManageTargets = { switchToDeviceContactsView }
185- onRemoveTarget = { removeTargetContact }
186- navigation = { navigation }
187- />
188- </ View >
177+ < SavedTargetsScreen
178+ targetContacts = { targetContacts }
179+ onManageTargets = { ( ) => handlePlusButtonPress ( navigation ) }
180+ onRemoveTarget = { removeTargetContact }
181+ navigation = { navigation }
182+ />
189183 ) }
190184 </ Stack . Screen >
191- < Stack . Screen name = "DeviceContacts" >
185+ < Stack . Screen
186+ name = "DeviceContacts"
187+ options = { { headerShown : false } }
188+ >
192189 { ( { navigation } ) => (
193- < View style = { styles . container } >
194- < DeviceContactsScreen
195- contacts = { contacts }
196- selectedContacts = { selectedContacts }
197- onToggleContact = { toggleContactSelectionInDeviceScreen }
198- onSaveChanges = { ( ) => {
199- saveTargetsFromDeviceScreen ( ) ;
200- navigation . navigate ( 'SavedTargets' ) ;
201- } }
202- onGoBack = { ( ) => navigation . navigate ( 'SavedTargets' ) }
203- />
204- </ View >
190+ < DeviceContactsScreen
191+ contacts = { contacts }
192+ selectedContacts = { selectedContacts }
193+ onToggleContact = { toggleContactSelectionInDeviceScreen }
194+ onSaveChanges = { ( ) => {
195+ saveTargetsFromDeviceScreen ( ) ;
196+ navigation . navigate ( 'SavedTargets' ) ;
197+ } }
198+ onGoBack = { ( ) => navigation . navigate ( 'SavedTargets' ) }
199+ />
205200 ) }
206201 </ Stack . Screen >
207202 </ Stack . Navigator >
208203 </ NavigationContainer >
209204 ) ;
210205}
211- const styles = StyleSheet . create ( {
212- container : {
213- flex : 1 ,
214- paddingTop : Platform . OS === 'android' ? 25 : 50 , // Android StatusBar 고려
215- backgroundColor : '#f0f0f0' , // 전체 앱 배경색
216- } ,
217- } ) ;
0 commit comments