Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⬅️Merge pull request from develop branch #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions AMRouter.podspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#!/usr/local/bin/ruby
Pod::Spec.new do |s|
s.name = 'AMRouter'
s.module_name = 'AMRouter'
s.version = '0.3.0'
s.version = '0.4.0'
s.summary = 'AMRouter'
s.description = <<-DESC
This is AMRouter.
DESC
s.homepage = 'https://github.com/archmagees/AMRouter'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Arthur' => 'archmagees@gmail.com' }
s.author = { 'Arthur' => 'archmagees.dev@gmail.com' }
s.platform = :ios, '9.0'
s.swift_version = '4.1'
s.source = { :git => 'https://github.com/archmagees/AMRouter.git', :tag => s.version.to_s }
s.source_files = 'AMRouter/AMRouter/**/*.{h,m}'
# s.vendored_framework = "Carthage/Build/iOS/AMRouter.framework"
s.requires_arc = true

s.dependency 'CocoaLumberjack', '~>3.4.2'
end
129 changes: 108 additions & 21 deletions AMRouter.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreviewsEnabled</key>
<false/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion AMRouter/AMRouter/AMComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ - (nullable id)targetForKey:(NSString *)key

dispatch_sync(operate_target_queue, ^{
dispatch_semaphore_wait(self->target_semaphore,
DISPATCH_TIME_NOW);
dispatch_time(DISPATCH_TIME_NOW, 3));

target = [self.cachedTargets objectForKey:key];

Expand Down
52 changes: 52 additions & 0 deletions AMRouter/AMRouter/AMMacro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// AMMacro.h
// AMRouter
//
// Created by archmagees on 2018/06/11.
// Copyright © 2018 archmagees. All rights reserved.
//

#import <Foundation/Foundation.h>


//#if __has_include(<CocoaLumberjack/CocoaLumberjack.h>)
// #import <CocoaLumberjack/CocoaLumberjack.h>
//
// #ifdef DEBUG
//
// #undef LOG_LEVEL_DEF
// #define LOG_LEVEL_DEF AMRouterLogLevel
//
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Wunused-const-variable"
//
// #define AMSetDDLogLevel(debugDDLogLevel, noDebugDDLogLevel) \
// static const DDLogLevel AMRouterLogLevel = debugDDLogLevel; \
// DDLogLevel AMRouterLogLevelUnused = noDebugDDLogLevel;
//
//#pragma clang diagnostic pop
//
// #else
//
// #undef LOG_LEVEL_DEF
// #define LOG_LEVEL_DEF AMRouterLogLevel
//
// #define AMSetDDLogLevel(debugDDLogLevel, noDebugDDLogLevel) \
// DDLogLevel AMRouterLogLevelUnused = debugDDLogLevel; \
// static const DDLogLevel AMRouterLogLevel = noDebugDDLogLevel;
//
// #endif
//
//#endif
//
//
//#if __has_include(<CocoaLumberjack/CocoaLumberjack.h>)
//#else
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Wunused-const-variable"
//
// #define AMSetDDLogLevel(debugDDLogLevelNoEffect, noDebugDDLogLevelNoEffect)
//
//#pragma clang diagnostic pop
//
//#endif
4 changes: 4 additions & 0 deletions AMRouter/AMRouter/AMRouter-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

3 changes: 2 additions & 1 deletion AMRouter/AMRouter/AMRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ FOUNDATION_EXPORT const unsigned char AMRouterVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <AMRouter/PublicHeader.h>
#import <AMRouter/AMComponent.h>

#import <AMRouter/NSString+AMTrick.h>
#import <AMRouter/AMUrlSchemeSynthesizer.h>
38 changes: 38 additions & 0 deletions AMRouter/AMRouter/AMUrlSchemeSynthesizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AMUrlSchemeSynthesizer.h
// AMRouter
//
// Created by archmagees on 2018/06/10.
// Copyright © 2018 archmagees. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AMUrlSchemeSynthesizer : NSObject

/**
* @brief Create a string by replacing the placeholders in pattern.
* @discussion
* Pattern format is just like the url components. That is:
* `scheme://host/path.pathextension?query#fragment`, and usually the pattern
* format is passed as `:scheme://:action?key1=:value1&key2=:value2`.
*
* @param pattern
* A string value indicates the template format.
*
* @param replacements
* An array which elements is NSString type, each of them will be used to
* replace the placeholders in template string.
*
* @return
* A string that has been processed. If it is nil, means there maybe an error
* in one of the parameters.
*/
+ (nullable NSString *)synthesizedStringWithPattern:(NSString *)pattern
replacements:(NSArray<NSString *> *)replacements;

@end

NS_ASSUME_NONNULL_END
134 changes: 134 additions & 0 deletions AMRouter/AMRouter/AMUrlSchemeSynthesizer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// AMUrlSchemeSynthesizer.m
// AMRouter
//
// Created by archmagees on 2018/06/10.
// Copyright © 2018 archmagees. All rights reserved.
//

#import "AMUrlSchemeSynthesizer.h"
#import "NSString+AMTrick.h"

NS_ASSUME_NONNULL_BEGIN

static NSString * const kColonSymbol = @":";
static NSString * const kDelimiterSymbols = @"/?&.";
static NSString * const kSeparatorSymbols = @"://";

@implementation AMUrlSchemeSynthesizer

#pragma mark - Public

+ (nullable NSString *)synthesizedStringWithPattern:(NSString *)pattern
replacements:(NSArray<NSString *> *)replacements {
if (!pattern.length || !replacements.count) {
return nil;
}

NSMutableArray *tempReplacements = [replacements mutableCopy];

// failsafe check if it has a scheme part
NSArray<NSString *> *components =
[pattern componentsSeparatedByString:kSeparatorSymbols];

NSMutableArray<NSString *> *synthesizedStrings = [NSMutableArray array];


void(^replacePlaceholder)(NSString * _Nonnull, NSArray<NSString *> * _Nonnull, BOOL * _Nonnull, BOOL * _Nonnull) = ^(NSString * _Nonnull stringWithPlaceholder, NSArray<NSString *> * _Nonnull matchedReplacements, BOOL * _Nonnull formatError, BOOL * _Nonnull stop) {

NSString *actualComponent = [self stringByReplacingPlaceholdersInString:stringWithPlaceholder withReplacements:matchedReplacements];

if (!actualComponent.length) {
*stop = YES;
*formatError = YES;
return;
}
[synthesizedStrings addObject:actualComponent];
};

__block BOOL formatError = NO;

[components enumerateObjectsUsingBlock:^(NSString * _Nonnull stringComponent, NSUInteger idx, BOOL * _Nonnull stop) {

if (![stringComponent containsString:kColonSymbol]) {
[synthesizedStrings addObject:stringComponent];
}
else {
// TODO: still can optimize it by using loop count
if (idx == 0 && components.count == 2) {
replacePlaceholder(stringComponent,
@[ replacements[0] ],
&formatError,
stop);
[tempReplacements removeObjectAtIndex:0];
}
else {
replacePlaceholder(stringComponent,
tempReplacements,
&formatError,
stop);
}
}
}];


return formatError ?
nil : [synthesizedStrings componentsJoinedByString:kSeparatorSymbols];
}

#pragma mark - Private

+ (nullable NSString *)stringByReplacingPlaceholdersInString:(NSString *)string
withReplacements:(const NSArray<NSString *> *)replacements {


NSUInteger count = [string am_countOfOccurrencesOfString:kColonSymbol];
NSUInteger loopCount = replacements.count;

if (loopCount == 0 ||
loopCount != count) {
return nil;
}

NSCharacterSet *delimiterCharacterSet = [NSCharacterSet characterSetWithCharactersInString:kDelimiterSymbols];

NSMutableString *mutableString = [string mutableCopy];

__block BOOL formatError = NO;

[replacements enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

// failsafe there is a @"" in the replacements array
if (!obj.length) {
formatError = YES;
*stop = YES;
}
NSRange wholeRange = NSMakeRange(0, mutableString.length);

NSRange colonRange = [mutableString rangeOfString:kColonSymbol];
NSRange searchRange = NSMakeRange(NSMaxRange(colonRange), wholeRange.length);
// failsafe, otherwise it will crash because of the length is out of
// the bound
searchRange = NSIntersectionRange(searchRange, wholeRange);

NSUInteger delimiterSymbolRangeLocation =
[mutableString rangeOfCharacterFromSet:delimiterCharacterSet
options:NSLiteralSearch
range:searchRange].location;
// the last placeholder is not end with delimiter symbols
if (delimiterSymbolRangeLocation == NSNotFound) {
delimiterSymbolRangeLocation = wholeRange.length;
}
NSRange replaceRange =
NSMakeRange(colonRange.location, delimiterSymbolRangeLocation);
replaceRange = NSIntersectionRange(replaceRange, wholeRange);

[mutableString replaceCharactersInRange:replaceRange withString:obj];
}];

return formatError ? nil : [mutableString copy];
}

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions AMRouter/AMRouter/Component.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Component.swift
// AMRouter
//
// Created by archmagees on 2018/07/20.
// Copyright © 2018 archmagees. All rights reserved.
//

import UIKit
import Foundation

@objc
class Component: NSObject {

}
19 changes: 19 additions & 0 deletions AMRouter/AMRouter/NSString+AMTrick.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSString+AMTrick.h
// AMRouter
//
// Created by archmagees on 2018/06/10.
// Copyright © 2018 archmagees. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSString (AMTrick)

- (NSUInteger)am_countOfOccurrencesOfString:(NSString *)specSymbol;

@end

NS_ASSUME_NONNULL_END
32 changes: 32 additions & 0 deletions AMRouter/AMRouter/NSString+AMTrick.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// NSString+AMTrick.m
// AMRouter
//
// Created by archmagees on 2018/06/10.
// Copyright © 2018 archmagees. All rights reserved.
//

#import "NSString+AMTrick.h"

NS_ASSUME_NONNULL_BEGIN

@implementation NSString (AMTrick)

- (NSUInteger)am_countOfOccurrencesOfString:(NSString *)specSymbol {
if (!self.length) {
return 0;
}

if (!specSymbol.length) {
return 0;
}

NSString *stringWithoutSymbol =
[self stringByReplacingOccurrencesOfString:specSymbol withString:@""];
NSUInteger diff = self.length - stringWithoutSymbol.length;
return diff;
}

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
@interface AMComponent (Message)

+ (id<MessageComponentInterface>)message;
+ (id<MessageComponentInterface>)messageModule;
//+ (id<MessageComponentInterface>)messageModule;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

- (BOOL)notificationEnabled;

- (NSInteger)unreadCount;
- (NSUInteger)unreadCount;

- (void)increaseUnreadCount:(NSInteger)count;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

#import "MessageModule.h"

static NSUInteger unreadCount = 3;

@implementation MessageModule

- (BOOL)notificationEnabled {
return YES;
}

- (NSInteger)unreadCount {
return 3;
- (NSUInteger)unreadCount {
return unreadCount;
}

- (void)increaseUnreadCount:(NSInteger)count {
unreadCount += count;
}

@end
Loading