Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Custom Group Activities

Custom Group Activities

Tachibana Kaoru

June 23, 2021
Tweet

More Decks by Tachibana Kaoru

Other Decks in Technology

Transcript

  1. de fi ne activity model struct TextTogetherActivity: GroupActivity{ static let

    activityIdentifier = "org.toyship.characters.text" } extension TextTogetherActivity{ var metadata: GroupActivityMetadata{ var data = GroupActivityMetadata() data.type = .generic data.title = "character" data.fallbackURL = URL(string: "") return data } }
  2. prepareForActivation let myActivity = TextTogetherActivity() async { switch await myActivity.prepareForActivation()

    { case .activationDisabled: prepareStateLabel.text = "(disable)" case .activationPreferred: prepareStateLabel.text = "prepare OK" case .cancelled: prepareStateLabel.text = "cancelled" default: prepareStateLabel.text = "(unknown status)" } }
  3. join async{ for await session in TextTogetherActivity.sessions(){ switch session.state{ case

    GroupSession<TextTogetherActivity>.State.joined: break case GroupSession<TextTogetherActivity>.State.waiting: session.join() default: break } } }
  4. Group Activities Session FaceTime session ActivityΛ ࡞ͬͯ activate ͢Δ Group

    Activities session FactTimeͷsession͕͋Δঢ়ଶͰactivateΛ͢ΔͱɺଞͷࢀՃ ऀʹԻͰ௨஌͞Ε·͢ɻ
  5. Group Activities Session FaceTime session Group Activities session join APIͰjoinΛݺͿલʹɺsystem

    UIͰɺ ʮΞϓϦ໊ʯʹࢀՃ͠·͔͢ʁͱ͍ ͏μΠΞϩάʹOK͢Δඞཁ͋Γɻ
  6. How to send and receive messages de fi ne message

    model send message receive message
  7. de fi ne message model struct ShareText: Codable{ let id:

    UUID var name: String var position: CGPoint = CGPoint(x: 0, y: 0) var red: CGFloat = 1.0 var green: CGFloat = 1.0 var blue: CGFloat = 1.0 var alpha: CGFloat = 1.0 }
  8. send message let messenger = GroupSessionMessenger(session: session) let newData =

    ShareText(id: UUID(), name: "Hello") messenger.send(newData, completion: {error in // check error })
  9. receive message async{ for await session in TextTogetherActivity.sessions(){ let messenger

    = GroupSessionMessenger(session: session) let task: Task.Handle = detach { [weak self] in for await (message, _ ) in messenger.messages(of: ShareText.self){ await self?.checkReceivedText(message) } } tasks.insert(task) } }