feat: iOS add ssr demo

This commit is contained in:
pengfei.zhou
2022-11-07 13:57:41 +08:00
committed by osborn
parent d416e9dc81
commit 1c306bd3f6
5 changed files with 88 additions and 10 deletions

View File

@@ -0,0 +1,15 @@
//
// DemoSSRVC.h
// Example
//
// Created by pengfei.zhou on 2022/11/7.
// Copyright © 2022 pengfei.zhou. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DemoSSRVC : UIViewController
- (instancetype)initWithPath:(NSString *)filePath;
@end

View File

@@ -0,0 +1,52 @@
//
// DemoSSRVC.m
// Example
//
// Created by pengfei.zhou on 2022/11/7.
// Copyright © 2022 pengfei.zhou. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "DemoSSRVC.h"
#import <DoricCore/Doric.h>
#import <JavaScriptCore/JavaScriptCore.h>
@interface DemoSSRVC ()
@property(nonatomic, copy) NSString *filePath;
@end
@implementation DemoSSRVC
- (instancetype)initWithPath:(NSString *)filePath {
if (self = [self init]) {
_filePath = filePath;
}
return self;
}
- (void)viewDidLoad {
self.title = self.filePath;
self.view.backgroundColor = [UIColor whiteColor];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *demoPath = [path stringByAppendingPathComponent:@"src"];
NSString *fullPath = [demoPath stringByAppendingPathComponent:self.filePath];
NSString *jsContent = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:nil];
DoricContext *context = [[DoricContext alloc] initWithScript:@"" source:@"" extra:@""];
DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:context];
[rootNode setupRootView:[[DoricRootView new] also:^(DoricRootView *it) {
it.width = self.view.width;
it.height = self.view.height;
it.clipsToBounds = YES;
it.frameChangedBlock = ^(CGSize oldSize, CGSize newSize) {
NSLog(@"frameChangedBlock:%@,%@", @(oldSize), @(newSize));
};
[self.view addSubview:it];
}]];
[context setRootNode:rootNode];
NSData *data = [jsContent dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *viewModels = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
[rootNode blend:[viewModels optObject:@"props"]];
[rootNode requestLayout];
}
@end

View File

@@ -8,11 +8,11 @@
#import <DoricCore/Doric.h>
#import <DoricDevkit/DoricDev.h>
#import "ViewController.h"
#import "DemoLibrary.h"
#import "DoricPanelListViewController.h"
#import "DoricEmbeddedExampleVC.h"
#import "DemoSSRVC.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, copy) NSArray <NSString *> *demoFilePaths;
@@ -113,14 +113,19 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
return;
}
NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row];
DoricViewController *doricViewController = [[DoricViewController alloc]
initWithSource:[NSString stringWithFormat:@"assets://src/%@", file]
alias:@"__dev__"//self.demoFilePaths[(NSUInteger) indexPath.row]
extra:nil
];
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Devkit" style:UIBarButtonItemStylePlain target:self action:@selector(onOpenDevkit)];
doricViewController.navigationItem.rightBarButtonItem = rightBarItem;
[self.navigationController pushViewController:doricViewController animated:NO];
if ([file hasSuffix:@".json"]) {
DemoSSRVC *vc = [[DemoSSRVC alloc] initWithPath:file];
[self.navigationController pushViewController:vc animated:NO];
} else {
DoricViewController *doricViewController = [[DoricViewController alloc]
initWithSource:[NSString stringWithFormat:@"assets://src/%@", file]
alias:@"__dev__"//self.demoFilePaths[(NSUInteger) indexPath.row]
extra:nil
];
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Devkit" style:UIBarButtonItemStylePlain target:self action:@selector(onOpenDevkit)];
doricViewController.navigationItem.rightBarButtonItem = rightBarItem;
[self.navigationController pushViewController:doricViewController animated:NO];
}
}
@end