Giter Club home page Giter Club logo

runtime-objc's Introduction

Material Motion Runtime for Apple Devices

Build Status codecov CocoaPods Compatible Platform Docs

The Material Motion Runtime is a tool for describing motion declaratively.

Declarative motion: motion as data

This library does not do much on its own. What it does do, however, is enable the expression of motion as discrete units of data that can be introspected, composed, and sent over a wire.

This library encourages you to describe motion as data, or what we call plans. Plans are committed to a motion runtime, or runtime for short. A runtime coordinates the creation of performers, objects responsible for translating plans into concrete execution.

Installation

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C and Swift libraries. CocoaPods automates the process of using third-party libraries in your projects. See the Getting Started guide for more information. You can install it with the following command:

gem install cocoapods

Add MaterialMotionRuntime to your Podfile:

pod 'MaterialMotionRuntime'

Then run the following command:

pod install

Usage

Import the Material Motion Runtime framework:

@import MaterialMotionRuntime;

You will now have access to all of the APIs.

Example apps/unit tests

Check out a local copy of the repo to access the Catalog application by running the following commands:

git clone https://github.com/material-motion/runtime-objc.git
cd runtime-objc
pod install
open MaterialMotionRuntime.xcworkspace

Guides

  1. Architecture
  2. How to define a new plan and performer type
  3. How to commit a plan to a runtime
  4. How to commit a named plan to a runtime
  5. How to handle multiple plan types in Swift
  6. How to configure performers with named plans
  7. How to use composition to fulfill plans
  8. How to indicate continuous performance
  9. How to trace internal runtime events
  10. How to log runtime events to the console
  11. How to observe timeline events

Architecture

The Material Motion Runtime consists of two groups of APIs: a runtime/transaction object and a constellation of protocols loosely consisting of plan and performing types.

MotionRuntime

The MotionRuntime object is a coordinating entity whose primary responsibility is to fulfill plans by creating performers. You can create many runtimes throughout the lifetime of your application. A good rule of thumb is to have one runtime per interaction or transition.

Plan + Performing types

The Plan and Performing protocol each define the minimal characteristics required for an object to be considered either a plan or a performer, respectively, by the runtime.

Plans and performers have a symbiotic relationship. A plan is executed by the performer it defines. Performer behavior is configured by the provided plan instances.

Learn more about the Material Motion Runtime by reading the Starmap.

How to create a new plan and performer type

The following steps provide copy-pastable snippets of code.

Step 1: Define the plan type

Questions to ask yourself when creating a new plan type:

  • What do I want my plan/performer to accomplish?
  • Will my performer need many plans to achieve the desired outcome?
  • How can I name my plan such that it clearly communicates either a behavior or a change in state?

As general rules:

  1. Plans with an -able suffix alter the behavior of the target, often indefinitely. Examples: Draggable, Pinchable, Tossable.
  2. Plans that are verbs describe some change in state, often over a period of time. Examples: FadeIn, Tween, SpringTo.

Code snippets:

In Objective-C:

@interface <#Plan#> : NSObject
@end

@implementation <#Plan#>
@end

In Swift:

class <#Plan#>: NSObject {
}

Step 2: Define the performer type

Performers are responsible for fulfilling plans. Fulfillment is possible in a variety of ways:

See the associated links for more details on each performing type.

Note: only one instance of a type of performer per target is ever created. This allows you to register multiple plans to the same target in order to configure a performer. See How to configure performers with plans for more details.

Code snippets:

In Objective-C:

@interface <#Performer#> : NSObject <MDMPerforming>
@end

@implementation <#Performer#> {
  UIView *_target;
}

- (instancetype)initWithTarget:(id)target {
  self = [super init];
  if (self) {
    assert([target isKindOfClass:[UIView class]]);
    _target = target;
  }
  return self;
}

- (void)addPlan:(id<MDMPlan>)plan {
  <#Plan#>* <#casted plan instance#> = plan;

  // Do something with the plan.
}

@end

In Swift:

class <#Performer#>: NSObject, Performing {
  let target: UIView
  required init(target: Any) {
    self.target = target as! UIView
    super.init()
  }

  func addPlan(_ plan: Plan) {
    let <#casted plan instance#> = plan as! <#Plan#>

    // Do something with the plan.
  }
}

Step 3: Make the plan type a formal Plan

Conforming to Plan requires:

  1. that you define the type of performer your plan requires, and
  2. that your plan be copyable.

Code snippets:

In Objective-C:

@interface <#Plan#> : NSObject <MDMPlan>
@end

@implementation <#Plan#>

- (Class)performerClass {
  return [<#Plan#> class];
}

- (id)copyWithZone:(NSZone *)zone {
  return [[[self class] allocWithZone:zone] init];
}

@end

In Swift:

class <#Plan#>: NSObject, Plan {
  func performerClass() -> AnyClass {
    return <#Performer#>.self
  }
  func copy(with zone: NSZone? = nil) -> Any {
    return <#Plan#>()
  }
}

How to commit a plan to a runtime

Step 1: Create and store a reference to a runtime instance

Code snippets:

In Objective-C:

@interface MyClass ()
@property(nonatomic, strong) MDMMotionRuntime* runtime;
@end

- (instancetype)init... {
  ...
  self.runtime = [MDMMotionRuntime new];
  ...
}

In Swift:

class MyClass {
  let runtime = MotionRuntime()
}

Step 2: Associate plans with targets

Code snippets:

In Objective-C:

[runtime addPlan:<#Plan instance#> to:<#View instance#>];

In Swift:

runtime.addPlan(<#Plan instance#>, to:<#View instance#>)

How to commit a named plan to a runtime

Step 1: Create and store a reference to a runtime instance

Code snippets:

In Objective-C:

@interface MyClass ()
@property(nonatomic, strong) MDMMotionRuntime* runtime;
@end

- (instancetype)init... {
  ...
  self.runtime = [MDMMotionRuntime new];
  ...
}

In Swift:

class MyClass {
  let runtime = MotionRuntime()
}

Step 2: Associate named plans with targets

Code snippets:

In Objective-C:

[runtime addPlan:<#Plan instance#> named:<#name#> to:<#View instance#>];

In Swift:

runtime.addPlan(<#Plan instance#>, named:<#name#>, to:<#View instance#>)

How to handle multiple plan types in Swift

Make use of Swift's typed switch/casing to handle multiple plan types.

func addPlan(_ plan: Plan) {
  switch plan {
  case let <#plan instance 1#> as <#Plan type 1#>:
    ()

  case let <#plan instance 2#> as <#Plan type 2#>:
    ()

  case is <#Plan type 3#>:
    ()

  default:
    assert(false)
  }
}

How to configure performers with named plans

Code snippets:

In Objective-C:

@interface <#Performer#> (NamedPlanPerforming) <MDMNamedPlanPerforming>
@end

@implementation <#Performer#> (NamedPlanPerforming)

- (void)addPlan:(id<MDMNamedPlan>)plan named:(NSString *)name {
  <#Plan#>* <#casted plan instance#> = plan;

  // Do something with the plan.
}

- (void)removePlanNamed:(NSString *)name {
  // Remove any configuration associated with the given name.
}

@end

In Swift:

extension <#Performer#>: NamedPlanPerforming {
  func addPlan(_ plan: NamedPlan, named name: String) {
    let <#casted plan instance#> = plan as! <#Plan#>

    // Do something with the plan.
  }

  func removePlan(named name: String) {
    // Remove any configuration associated with the given name.
  }
}

How to use composition to fulfill plans

A composition performer is able to emit new plans using a plan emitter. This feature enables the reuse of plans and the creation of higher-order abstractions.

Step 1: Conform to ComposablePerforming and store the plan emitter

Code snippets:

In Objective-C:

@interface <#Performer#> ()
@property(nonatomic, strong) id<MDMPlanEmitting> planEmitter;
@end

@interface <#Performer#> (Composition) <MDMComposablePerforming>
@end

@implementation <#Performer#> (Composition)

- (void)setPlanEmitter:(id<MDMPlanEmitting>)planEmitter {
  self.planEmitter = planEmitter;
}

@end

In Swift:

// Store the emitter in your class' definition.
class <#Performer#>: ... {
  ...
  var emitter: PlanEmitting!
  ...
}

extension <#Performer#>: ComposablePerforming {
  var emitter: PlanEmitting!
  func setPlanEmitter(_ planEmitter: PlanEmitting) {
    emitter = planEmitter
  }
}

Step 2: Emit plans

Performers are only able to emit plans for their associated target.

Code snippets:

In Objective-C:

[self.planEmitter emitPlan:<#(nonnull id<MDMPlan>)#>];

In Swift:

emitter.emitPlan<#T##Plan#>)

How to indicate continuous performance

Performers will often perform their actions over a period of time or while an interaction is active. These types of performers are called continuous performers.

A continuous performer is able to affect the active state of the runtime by generating activity tokens. The runtime is considered active so long as an activity token is active. Continuous performers are expected to activate and deactivate tokens when ongoing work starts and finishes, respectively.

For example, a performer that registers a platform animation might activate a token when the animation starts. When the animation completes the token would be deactivated.

Step 1: Conform to ContinuousPerforming and store the token generator

Code snippets:

In Objective-C:

@interface <#Performer#> ()
@property(nonatomic, strong) id<MDMPlanTokenizing> tokenizer;
@end

@interface <#Performer#> (Composition) <MDMComposablePerforming>
@end

@implementation <#Performer#> (Composition)

- (void)givePlanTokenizer:(id<MDMPlanTokenizing>)tokenizer {
  self.tokenizer = tokenizer;
}

@end

In Swift:

// Store the emitter in your class' definition.
class <#Performer#>: ... {
  ...
  var tokenizer: PlanTokenizing!
  ...
}

extension <#Performer#>: ContinuousPerforming {
  func givePlanTokenizer(_ tokenizer: PlanTokenizing) {
    self.tokenizer = tokenizer
  }
}

Step 2: Generate a token

If your work completes in a callback then you will likely need to store the token in order to be able to reference it at a later point.

Code snippets:

In Objective-C:

id<MDMTokenized> token = [self.tokenizer tokenForPlan:<#plan#>];
tokenMap[animation] = token;

In Swift:

let token = tokenizer.generate(for: <#plan#>)!
tokenMap[animation] = token

Step 3: Activate the token when work begins

Code snippets:

In Objective-C:

id<MDMIsActiveTokenable> token = tokenMap[animation];
token.active = true;

In Swift:

tokenMap[animation].isActive = true

Step 4: Deactivate the token when work has completed

Code snippets:

In Objective-C:

id<MDMTokenized> token = tokenMap[animation];
token.active = false;

In Swift:

tokenMap[animation].isActive = false

How to trace internal runtime events

Tracing allows you to observe internal events occurring within a runtime. This information may be used for the following purposes:

  • Debug logging.
  • Inspection tooling.

Use for other purposes is unsupported.

Step 1: Create a tracer class

Code snippets:

In Objective-C:

@interface <#Custom tracer#> : NSObject <MDMTracing>
@end

@implementation <#Custom tracer#>
@end

In Swift:

class <#Custom tracer#>: NSObject, Tracing {
}

Step 2: Implement methods

The documentation for the Tracing protocol enumerates the available methods.

Code snippets:

In Objective-C:

@implementation <#Custom tracer#>

- (void)didAddPlan:(id<MDMPlan>)plan to:(id)target {

}

@end

In Swift:

class <#Custom tracer#>: NSObject, Tracing {
  func didAddPlan(_ plan: Plan, to target: Any) {

  }
}

How to log runtime events to the console

Code snippets:

In Objective-C:

[runtime addTracer:[MDMConsoleLoggingTracer new]];

In Swift:

runtime.addTracer(ConsoleLoggingTracer())

How to observe timeline events

Step 1: Conform to the TimelineObserving protocol

Code snippets:

In Objective-C:

@interface <#SomeClass#> () <MDMTimelineObserving>
@end

@implementation <#SomeClass#>

- (void)timeline:(MDMTimeline *)timeline didAttachScrubber:(MDMTimelineScrubber *)scrubber {

}

- (void)timeline:(MDMTimeline *)timeline didDetachScrubber:(MDMTimelineScrubber *)scrubber {

}

- (void)timeline:(MDMTimeline *)timeline scrubberDidScrub:(NSTimeInterval)timeOffset {

}

@end

In Swift:

extension <#SomeClass#>: TimelineObserving {
  func timeline(_ timeline: Timeline, didAttach scrubber: TimelineScrubber) {
  }

  func timeline(_ timeline: Timeline, didDetach scrubber: TimelineScrubber) {
  }

  func timeline(_ timeline: Timeline, scrubberDidScrub timeOffset: TimeInterval) {
  }
}

Step 2: Add your observer to a timeline

Code snippets:

In Objective-C:

[timeline addTimelineObserver:<#(nonnull id<MDMTimelineObserving>)#>];

In Swift:

timeline.addObserver(<#T##observer: TimelineObserving##TimelineObserving#>)

Contributing

We welcome contributions!

Check out our upcoming milestones.

Learn more about our team, our community, and our contributor essentials.

License

Licensed under the Apache 2.0 license. See LICENSE for details.

runtime-objc's People

Contributors

jverkoey avatar rcameron avatar seanoshea avatar willlarche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

runtime-objc's Issues

Proof of concept: serialize a plan to disk and back again

An ideal proof of concept will be a unit test that is able to encode a plan to disk, read the plan from disk, and ultimately execute the plan in a runtime.

For the purposes of this proof of concept we can use simple plist serialization.

This proof of concept would be best realized as a unit test.

Research: State Machine Family?

How can we use state machines for complex multi-state systems with plans that are associated to each state?

Should it be a family?

100% test coverage of MDMPerformerGroup

Latest coverage results as of 3b1ddba:

52%     src/private/MDMPerformerGroup.m

Untested methods:

  • - (void)setUpFeaturesForPerformerInfo:(MDMPerformerInfo *)performerInfo delegated performance.
  • - (id<MDMPerforming>)performerForPlan:(id<MDMPlan>)plan for a plan being registered to an existing performer.

Plan/Performer testing types for ease of test writing

Create two types: TestPlan and TestPerformer.

TestPlan

  • Its performerClass is always TestPerformer.
  • Provide settable block properties to configure performer behavior and execute tests.

TestPerformer

Executes plan blocks at relevant stages.

E.g. addPlan might look like:

- addPlan:plan {
  plan.addPlan();
}

The unit test code would look like:

plan.addPlan = ^{
  // Run some logic.
};
// commit
// XCTAssert statements

Add activity state change test for MDMScheduler

This test must make the scheduler to change its activity state and inform a delegate of the change.

The scheduler's activity state can be changed by adding a plan that does some remote form of execution. For the purposes of the test this remote execution could be instantaneous.

100% test coverage of MDMScheduler

Latest report as of e7fb7fd.

 62%     src/MDMScheduler.m

Untested methods:

  • - (void)performerGroup:(MDMPerformerGroup *)performerGroup activeStateDidChange:(BOOL)isActive
  • - (MDMSchedulerActivityState)activityState

Add debug APIs for enumerating the plans and performers in a runtime

The owner of this task must first come up with an API that can be used to enumerate all plans and performers in the scheduler.

The API should be proposed as an addition to the Spec by sending an email to [email protected].

Specifically: propose as an addition to the Scheduler spec as "Feature: inspection".

Once the spec is approved, the API should be submitted for review and committed.

Intended use cases

  • Debugging
  • Console output
  • Inspection tools (LLDB, web inspector, etc.)

First draft of the readme

The repo's root readme needs:

  • Overview
  • Adding the code to your project
  • Usage
    • Life of a Plan, in code form

unit tests currently broken as of b2bf2ad2df7eda49f155d3f70ad0b115d6617ade

Output:

arc unit --everything
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/TestSchedulerDelegate.swift:20:46: error: 'MDMSchedulerDelegate' has been renamed to 'SchedulerDelegate'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/TestSchedulerDelegate.swift:23:53: error: 'MDMScheduler' has been renamed to 'Scheduler'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:37:38: error: 'MDMPlan' has been renamed to 'Plan'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:43:43: error: 'MDMPlanPerforming' has been renamed to 'PlanPerforming'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:43:62: error: 'MDMDelegatedPerforming' has been renamed to 'DelegatedPerforming'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:52:20: error: 'MDMPlan' has been renamed to 'Plan'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:23:23: error: 'MDMTransaction' has been renamed to 'Transaction'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/DelegatedPerformanceTests.swift:26:21: error: 'MDMScheduler' has been renamed to 'Scheduler'
   BROKEN  UnitTests
/Users/featherless/workbench/odeon/material-motion-runtime-objc/tests/unit/TestSchedulerDelegate.swift:20:46: error: 'MDMSchedulerDelegate' has been renamed to 'SchedulerDelegate'

Update podspec homepage

s.homepage     = "https://github.com/material-motion"

should be

s.homepage     = "https://github.com/material-motion/material-motion-runtime-objc"

Provide a simple "slide in" transition

Blocked by #11, #12.

This transition should be a simple TransitionDirector instance.

Only one plan should be registered: the right view controller "shifting up" during presentation.

Generate API documentation

Need to hook up jazzy.

Place the following file in the root of the repo:

.jazzy.yaml

module: MaterialMotionRuntime
umbrella_header: src/MaterialMotionRuntime.h
objc: true
sdk: iphonesimulator

Test activity state of the scheduler

Add a plan to the scheduler that kicks off some remote execution. Verify that the scheduler reports its activity state as "active". Similarly, verify that upon completion of the remote execution that the scheduler reports its activity state as "inactive".

This should be tested for zero, one, and many plans.

Facebook POP Family

Facebook POP SDK support.

May be similar to CoreAnimation family.

Make POP objects conform to MDMPlan?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.