Commit 6db645b6 authored by Stepan Khapugin's avatar Stepan Khapugin Committed by Commit Bot

[multiball] Create the SceneController and make it an observer.

Adds a SceneState observer protocol, and makes the SceneController be
created by the SceneDelegate and listen to this protocol.

Bug: none
Change-Id: I10378f6034f13a4b47687fcbcec6a38d5c078666
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1789585
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Auto-Submit: Stepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697979}
parent f5e86a26
......@@ -7,8 +7,18 @@
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/main/scene_state.h"
// The controller object for a scene. Reacts to scene state changes.
@interface SceneController : NSObject
@interface SceneController : NSObject <SceneStateObserver>
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithSceneState:(SceneState*)sceneState
NS_DESIGNATED_INITIALIZER;
// The state of the scene controlled by this object.
@property(nonatomic, weak, readonly) SceneState* sceneState;
@end
#endif // IOS_CHROME_BROWSER_UI_MAIN_SCENE_CONTROLLER_H_
......@@ -10,4 +10,19 @@
@implementation SceneController
- (instancetype)initWithSceneState:(SceneState*)sceneState {
self = [super init];
if (self) {
_sceneState = sceneState;
[_sceneState addObserver:self];
}
return self;
}
#pragma mark - SceneStateObserver
- (void)sceneState:(SceneState*)sceneState
transitionedToActivationLevel:(SceneActivationLevel)level {
}
@end
......@@ -16,8 +16,9 @@ namespace {
class SceneControllerTest : public PlatformTest {
protected:
SceneControllerTest() {
scene_controller_ = [[SceneController alloc] init];
scene_state_ = [[SceneState alloc] init];
scene_controller_ =
[[SceneController alloc] initWithSceneState:scene_state_];
}
SceneController* scene_controller_;
......
......@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/main/scene_controller.h"
#import "ios/chrome/browser/ui/main/scene_state.h"
// An object acting as a scene delegate for UIKit. Updates the scene state.
......@@ -17,6 +18,9 @@
// The object that holds the state of the scene associated with this delegate.
@property(nonatomic, strong) SceneState* sceneState;
// The controller created and owned by this object.
@property(nonatomic, strong) SceneController* sceneController;
@end
#endif // IOS_CHROME_BROWSER_UI_MAIN_SCENE_DELEGATE_H_
......@@ -13,6 +13,7 @@
- (SceneState*)sceneState {
if (!_sceneState) {
_sceneState = [[SceneState alloc] init];
_sceneController = [[SceneController alloc] initWithSceneState:_sceneState];
}
return _sceneState;
}
......
......@@ -7,6 +7,8 @@
#import <UIKit/UIKit.h>
@class SceneState;
// Describes the possible scene states.
// This is an iOS 12 compatible version of UISceneActivationState enum.
typedef NS_ENUM(NSUInteger, SceneActivationLevel) {
......@@ -23,6 +25,17 @@ typedef NS_ENUM(NSUInteger, SceneActivationLevel) {
SceneActivationLevelForegroundActive
};
@protocol SceneStateObserver <NSObject>
@optional
// Called whenever the scene state transitions between different activity
// states.
- (void)sceneState:(SceneState*)sceneState
transitionedToActivationLevel:(SceneActivationLevel)level;
@end
// An object containing the state of a UIWindowScene. One state object
// corresponds to one scene.
@interface SceneState : NSObject
......@@ -33,6 +46,13 @@ typedef NS_ENUM(NSUInteger, SceneActivationLevel) {
// Window for the associated scene, if any.
@property(nonatomic, weak) UIWindow* window;
// Adds an observer to this scene state. The observers will be notified about
// scene state changes per SceneStateObserver protocol.
- (void)addObserver:(id<SceneStateObserver>)observer;
// Removes the observer. It's safe to call this at any time, including from
// SceneStateObserver callbacks.
- (void)removeObserver:(id<SceneStateObserver>)observer;
@end
#endif // IOS_CHROME_BROWSER_UI_MAIN_SCENE_STATE_H_
......@@ -4,9 +4,85 @@
#import "ios/chrome/browser/ui/main/scene_state.h"
#import "base/ios/crb_protocol_observers.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface SceneStateObserverList : CRBProtocolObservers <SceneStateObserver>
@end
@implementation SceneStateObserverList
@end
#pragma mark - SceneState
@interface SceneState ()
// Container for this object's observers.
@property(nonatomic, strong) SceneStateObserverList* observers;
@end
@implementation SceneState
- (instancetype)init {
self = [super init];
if (self) {
_observers = [SceneStateObserverList
observersWithProtocol:@protocol(SceneStateObserver)];
}
return self;
}
#pragma mark - public
- (void)addObserver:(id<SceneStateObserver>)observer {
[self.observers addObserver:observer];
}
- (void)removeObserver:(id<SceneStateObserver>)observer {
[self.observers removeObserver:observer];
}
#pragma mark - Setters & Getters.
- (void)setActivationLevel:(SceneActivationLevel)newLevel {
if (_activationLevel == newLevel) {
return;
}
_activationLevel = newLevel;
[self.observers sceneState:self transitionedToActivationLevel:newLevel];
}
#pragma mark - debug
- (NSString*)description {
NSString* activityString = nil;
switch (self.activationLevel) {
case SceneActivationLevelUnattached: {
activityString = @"Unattached";
break;
}
case SceneActivationLevelBackground: {
activityString = @"Background";
break;
}
case SceneActivationLevelForegroundInactive: {
activityString = @"Foreground, Inactive";
break;
}
case SceneActivationLevelForegroundActive: {
activityString = @"Active";
break;
}
}
return
[NSString stringWithFormat:@"SceneState %p (%@)", self, activityString];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment