質問編集履歴

4

文章の修正

2018/08/11 04:37

投稿

takuya_22
takuya_22

スコア12

test CHANGED
File without changes
test CHANGED
@@ -200,186 +200,78 @@
200
200
 
201
201
  ```
202
202
 
203
- ```
204
-
205
- guard let uid = data?.user.uid else { return }
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
- self.uploadUserData(uid: uid, username: username, email: email, imageData: imageData, onSuccess: onSuccess)
216
-
217
- }
218
-
219
-
220
-
221
- }
222
-
223
- ```
224
-
225
-
226
-
227
- ```
228
-
229
- var delegate: ProfilHeaderCollectionViewCellDelegate?
230
-
231
-
232
-
233
- var user: UserModel? {
234
-
235
- didSet {
236
-
237
-
238
-
239
- guard let _user = user else { return }
240
-
241
- setupUserInformation(user: _user)
242
-
243
- }
244
-
245
- }
246
-
247
-
248
-
249
- func setupUserInformation(user: UserModel) {
250
-
251
- usernameLabel.text = user.username
252
-
253
-
254
-
255
- guard let url = URL(string: user.profilImageUrl!) else { return }
256
-
257
- profilImageView.sd_setImage(with: url) { (_, _, _, _) in
258
-
259
- }
260
-
261
-
262
-
263
- PostApi.shared.fetchCountPosts(withUid: user.uid!) { (postCount) in
264
-
265
- self.postCountLabel.text = "(postCount)"
266
-
267
- }
268
-
269
-
270
-
271
- FollowApi.shared.fetchFollowingCount(withUser: user.uid!) { (followingCount) in
272
-
273
- self.followingCountLabel.text = "(followingCount)"
274
-
275
- }
276
-
277
-
278
-
279
- FollowApi.shared.fetchFollowerCount(withUser: user.uid!) { (followerCount) in
280
-
281
- self.followersCountLabel.text = "(followerCount)"
282
-
283
- }
284
-
285
-
286
-
287
- if user.uid == UserApi.shared.CURRENT_USER_UID {
288
-
289
- editButton.setTitle("設定", for: .normal)
290
-
291
-
292
-
293
- editButton.addTarget(self, action: #selector(goToSettings), for: .touchUpInside)
294
-
295
-
296
-
297
- } else {
298
-
299
- if user.isFollowing! == true {
300
-
301
- setupFollowButton()
302
-
303
- } else {
304
-
305
- setupUnFollowButton()
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ ```
212
+
213
+ import Foundation
214
+
215
+ import FirebaseDatabase
216
+
217
+ import FirebaseAuth
218
+
219
+
220
+
221
+ class UserApi {
222
+
223
+ var REF_USERS = Database.database().reference().child("users")
224
+
225
+
226
+
227
+ static var shared: UserApi = UserApi()
228
+
229
+ private init() {
230
+
231
+ }
232
+
233
+
234
+
235
+ var CURRENT_USER_UID: String? {
236
+
237
+ if let currentUserUid = Auth.auth().currentUser?.uid {
238
+
239
+ return currentUserUid
240
+
241
+ }
242
+
243
+ return nil
244
+
245
+ }
246
+
247
+
248
+
249
+ var CURRENT_USER: User? {
250
+
251
+ if let currentUserUid = Auth.auth().currentUser {
252
+
253
+ return currentUserUid
254
+
255
+ }
256
+
257
+ return nil
258
+
259
+ }
260
+
261
+
262
+
263
+ func observeUser(uid: String, completion: @escaping (UserModel) -> Void) {
264
+
265
+ REF_USERS.child(uid).observeSingleEvent(of: .value) { (snapshot) in
266
+
267
+ guard let dic = snapshot.value as? [String: Any] else { return }
268
+
269
+ let newUser = UserModel(dictionary: dic)
270
+
271
+ completion(newUser)
306
272
 
307
273
  }
308
274
 
309
- }
310
-
311
- }
312
-
313
-
314
-
315
- ```
316
-
317
-
318
-
319
- ```
320
-
321
- import Foundation
322
-
323
- import FirebaseDatabase
324
-
325
- import FirebaseAuth
326
-
327
-
328
-
329
- class UserApi {
330
-
331
- var REF_USERS = Database.database().reference().child("users")
332
-
333
-
334
-
335
- static var shared: UserApi = UserApi()
336
-
337
- private init() {
338
-
339
- }
340
-
341
-
342
-
343
- var CURRENT_USER_UID: String? {
344
-
345
- if let currentUserUid = Auth.auth().currentUser?.uid {
346
-
347
- return currentUserUid
348
-
349
- }
350
-
351
- return nil
352
-
353
- }
354
-
355
-
356
-
357
- var CURRENT_USER: User? {
358
-
359
- if let currentUserUid = Auth.auth().currentUser {
360
-
361
- return currentUserUid
362
-
363
- }
364
-
365
- return nil
366
-
367
- }
368
-
369
-
370
-
371
- func observeUser(uid: String, completion: @escaping (UserModel) -> Void) {
372
-
373
- REF_USERS.child(uid).observeSingleEvent(of: .value) { (snapshot) in
374
-
375
- guard let dic = snapshot.value as? [String: Any] else { return }
376
-
377
- let newUser = UserModel(dictionary: dic)
378
-
379
- completion(newUser)
380
-
381
- }
382
-
383
275
  }
384
276
 
385
277
 

3

user情報の取得について追記しました。

2018/08/11 04:37

投稿

takuya_22
takuya_22

スコア12

test CHANGED
File without changes
test CHANGED
@@ -316,6 +316,164 @@
316
316
 
317
317
 
318
318
 
319
+ ```
320
+
321
+ import Foundation
322
+
323
+ import FirebaseDatabase
324
+
325
+ import FirebaseAuth
326
+
327
+
328
+
329
+ class UserApi {
330
+
331
+ var REF_USERS = Database.database().reference().child("users")
332
+
333
+
334
+
335
+ static var shared: UserApi = UserApi()
336
+
337
+ private init() {
338
+
339
+ }
340
+
341
+
342
+
343
+ var CURRENT_USER_UID: String? {
344
+
345
+ if let currentUserUid = Auth.auth().currentUser?.uid {
346
+
347
+ return currentUserUid
348
+
349
+ }
350
+
351
+ return nil
352
+
353
+ }
354
+
355
+
356
+
357
+ var CURRENT_USER: User? {
358
+
359
+ if let currentUserUid = Auth.auth().currentUser {
360
+
361
+ return currentUserUid
362
+
363
+ }
364
+
365
+ return nil
366
+
367
+ }
368
+
369
+
370
+
371
+ func observeUser(uid: String, completion: @escaping (UserModel) -> Void) {
372
+
373
+ REF_USERS.child(uid).observeSingleEvent(of: .value) { (snapshot) in
374
+
375
+ guard let dic = snapshot.value as? [String: Any] else { return }
376
+
377
+ let newUser = UserModel(dictionary: dic)
378
+
379
+ completion(newUser)
380
+
381
+ }
382
+
383
+ }
384
+
385
+
386
+
387
+ func observeUser(completion: @escaping (UserModel) -> Void ) {
388
+
389
+ REF_USERS.observe(.childAdded) { (snapshot) in
390
+
391
+ guard let dic = snapshot.value as? [String: Any] else { return }
392
+
393
+ let user = UserModel(dictionary: dic)
394
+
395
+ completion(user)
396
+
397
+ }
398
+
399
+ }
400
+
401
+
402
+
403
+ func observeCurrentUser(completion: @escaping (UserModel) -> Void ) {
404
+
405
+ guard let currentUserUid = CURRENT_USER_UID else { return }
406
+
407
+ REF_USERS.child(currentUserUid).observeSingleEvent(of: .value) { (snapshot) in
408
+
409
+ guard let dic = snapshot.value as? [String: Any] else { return }
410
+
411
+ let currentUser = UserModel(dictionary: dic)
412
+
413
+ completion(currentUser)
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ func queryUser(withText text: String, completion: @escaping(UserModel) -> Void ) {
422
+
423
+ REF_USERS.queryOrdered(byChild: "username_lowercase").queryStarting(atValue: text).queryEnding(atValue: text + "\u{f8ff}").queryLimited(toLast: 5).observeSingleEvent(of: .value) { (snapshot) in
424
+
425
+ snapshot.children.forEach({ (data) in
426
+
427
+ let child = data as! DataSnapshot
428
+
429
+ guard let dic = child.value as? [String: Any] else { return }
430
+
431
+ let user = UserModel(dictionary: dic)
432
+
433
+ completion(user)
434
+
435
+ })
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+ }
470
+
471
+
472
+
473
+ ```
474
+
475
+
476
+
319
477
  ### 試したこと
320
478
 
321
479
 

2

書式の改善をしました。

2018/08/10 10:11

投稿

takuya_22
takuya_22

スコア12

test CHANGED
File without changes
test CHANGED
@@ -198,7 +198,7 @@
198
198
 
199
199
  }
200
200
 
201
- -------------------------------------------------------------------------------
201
+ ```
202
202
 
203
203
  ```
204
204
 
@@ -220,7 +220,11 @@
220
220
 
221
221
  }
222
222
 
223
- --------------------------------------------------------------------------------
223
+ ```
224
+
225
+
226
+
227
+ ```
224
228
 
225
229
  var delegate: ProfilHeaderCollectionViewCellDelegate?
226
230
 
@@ -308,6 +312,10 @@
308
312
 
309
313
 
310
314
 
315
+ ```
316
+
317
+
318
+
311
319
  ### 試したこと
312
320
 
313
321
 

1

ユーザー情報取得について追記しました。

2018/08/10 07:21

投稿

takuya_22
takuya_22

スコア12

test CHANGED
File without changes
test CHANGED
@@ -198,10 +198,114 @@
198
198
 
199
199
  }
200
200
 
201
-
201
+ -------------------------------------------------------------------------------
202
202
 
203
203
  ```
204
204
 
205
+ guard let uid = data?.user.uid else { return }
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+ self.uploadUserData(uid: uid, username: username, email: email, imageData: imageData, onSuccess: onSuccess)
216
+
217
+ }
218
+
219
+
220
+
221
+ }
222
+
223
+ --------------------------------------------------------------------------------
224
+
225
+ var delegate: ProfilHeaderCollectionViewCellDelegate?
226
+
227
+
228
+
229
+ var user: UserModel? {
230
+
231
+ didSet {
232
+
233
+
234
+
235
+ guard let _user = user else { return }
236
+
237
+ setupUserInformation(user: _user)
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ func setupUserInformation(user: UserModel) {
246
+
247
+ usernameLabel.text = user.username
248
+
249
+
250
+
251
+ guard let url = URL(string: user.profilImageUrl!) else { return }
252
+
253
+ profilImageView.sd_setImage(with: url) { (_, _, _, _) in
254
+
255
+ }
256
+
257
+
258
+
259
+ PostApi.shared.fetchCountPosts(withUid: user.uid!) { (postCount) in
260
+
261
+ self.postCountLabel.text = "(postCount)"
262
+
263
+ }
264
+
265
+
266
+
267
+ FollowApi.shared.fetchFollowingCount(withUser: user.uid!) { (followingCount) in
268
+
269
+ self.followingCountLabel.text = "(followingCount)"
270
+
271
+ }
272
+
273
+
274
+
275
+ FollowApi.shared.fetchFollowerCount(withUser: user.uid!) { (followerCount) in
276
+
277
+ self.followersCountLabel.text = "(followerCount)"
278
+
279
+ }
280
+
281
+
282
+
283
+ if user.uid == UserApi.shared.CURRENT_USER_UID {
284
+
285
+ editButton.setTitle("設定", for: .normal)
286
+
287
+
288
+
289
+ editButton.addTarget(self, action: #selector(goToSettings), for: .touchUpInside)
290
+
291
+
292
+
293
+ } else {
294
+
295
+ if user.isFollowing! == true {
296
+
297
+ setupFollowButton()
298
+
299
+ } else {
300
+
301
+ setupUnFollowButton()
302
+
303
+ }
304
+
305
+ }
306
+
307
+ }
308
+
205
309
 
206
310
 
207
311
  ### 試したこと