Skip to content

Commit

Permalink
feat(connectivity): shared codebase for apple products
Browse files Browse the repository at this point in the history
Signed-off-by: George Kutsurua <[email protected]>
  • Loading branch information
suquant committed Feb 24, 2024
1 parent d527366 commit 2d628dd
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 286 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if TARGET_OS_IOS
#import <Flutter/Flutter.h>
#elif TARGET_OS_OSX
#import <FlutterMacOS/FlutterMacOS.h>
#endif

@interface ConnectivityPlugin : NSObject <FlutterPlugin>
@end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "ConnectivityPlusPlugin.h"
#import "ConnectivityPlugin.h"
#if __has_include(<connectivity_plus/connectivity_plus-Swift.h>)
#import <connectivity_plus/connectivity_plus-Swift.h>
#else
Expand All @@ -8,8 +8,8 @@
#import "connectivity_plus-Swift.h"
#endif

@implementation ConnectivityPlusPlugin
@implementation ConnectivityPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[SwiftConnectivityPlusPlugin registerWithRegistrar:registrar];
[SwiftConnectivityPlugin registerWithRegistrar:registrar];
}
@end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Network

@available(iOS 12, *)
@available(iOS 12, macOS 10.14, *)
public class PathMonitorConnectivityProvider: NSObject, ConnectivityProvider {

private let queue = DispatchQueue.global(qos: .background)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
// Use of this source is governed by a BSD-style license that can
// be found in the LICENSE file.

#if os(iOS)
import Flutter
#elseif os(macOS)
import Cocoa
import FlutterMacOS
#endif

public class SwiftConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
public class SwiftConnectivityPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
private let connectivityProvider: ConnectivityProvider
private var eventSink: FlutterEventSink?

Expand All @@ -15,22 +20,28 @@ public class SwiftConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStream
}

public static func register(with registrar: FlutterPluginRegistrar) {
#if os(iOS)
let binaryMessenger = registrar.messenger()
#elseif os(macOS)
let binaryMessenger = registrar.messenger
#endif

let channel = FlutterMethodChannel(
name: "dev.fluttercommunity.plus/connectivity",
binaryMessenger: registrar.messenger())
binaryMessenger: binaryMessenger)

let streamChannel = FlutterEventChannel(
name: "dev.fluttercommunity.plus/connectivity_status",
binaryMessenger: registrar.messenger())
binaryMessenger: binaryMessenger)

let connectivityProvider: ConnectivityProvider
if #available(iOS 12, *) {
if #available(iOS 12, macOS 10.14, *) {
connectivityProvider = PathMonitorConnectivityProvider()
} else {
connectivityProvider = ReachabilityConnectivityProvider()
}

let instance = SwiftConnectivityPlusPlugin(connectivityProvider: connectivityProvider)
let instance = SwiftConnectivityPlugin(connectivityProvider: connectivityProvider)
streamChannel.setStreamHandler(instance)

registrar.addMethodCallDelegate(instance, channel: channel)
Expand Down
33 changes: 33 additions & 0 deletions packages/connectivity_plus/connectivity_plus/apple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Flutter Project Restructurization Guide

## Overview

This guide outlines the process of restructuring our Flutter project to improve code organization and maintainability. We are introducing a new directory structure to consolidate common classes used across Apple's operating systems (iOS, macOS, and potentially others in the future). This approach aims to keep similar code in one place, making our codebase cleaner and more manageable.

## New Directory Structure

We have created a new folder named `apple` within our project's directory. This folder will contain all the classes and code snippets that are common to Apple's operating systems. By centralizing this code, we can easily manage and update it without duplicating effort across multiple files.

### Implementing the `apple` Folder

- **Location:** The `apple` folder is located at the root of the project's directory structure.
- **Contents:** This folder will include any Dart files that contain code relevant to both iOS and macOS platforms. If the code is potentially applicable to other Apple operating systems in the future, it should also reside here.

## Conditional Compilation

To accommodate the differences between operating systems without needing to completely overwrite files, we will utilize conditional compilation statements. These statements allow us to compile code conditionally based on the target operating system. Examples include `#if os(macOS)` for macOS-specific code and `#if os(iOS)` for iOS-specific code.

### Using Conditional Compilation

1. **Identify Common Code:** First, identify code that is common across different Apple platforms but requires slight modifications based on the target OS.
2. **Apply Conditional Compilation:** Use conditional compilation statements to include or exclude code blocks based on the OS. This method ensures that only relevant code is compiled for each target platform, reducing bloat and improving performance.

## Benefits

- **Improved Code Organization:** By keeping similar code in one place, we make our codebase more organized and navigable.
- **Easier Maintenance:** Centralizing code for Apple's platforms simplifies updates and maintenance, as changes only need to be made in one location.
- **Efficient Development:** Conditional compilation allows for a more efficient development process, as it eliminates the need for completely separate codebases for each platform.

## Conclusion

The restructurization of our Flutter project, including the introduction of the `apple` folder and the use of conditional compilation, is a strategic move to improve our codebase's maintainability and efficiency. By following this guide, we can ensure a smooth transition to the new structure and a more streamlined development process moving forward.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ Downloaded by pub (not CocoaPods).
s.author = { 'Flutter Community Team' => '[email protected]' }
s.source = { :http => 'https://github.com/fluttercommunity/plus_plugins/tree/main/packages/connectivity_plus' }
s.documentation_url = 'https://pub.dev/packages/connectivity_plus'
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.source_files = [
'Classes/**/*'
]
s.public_header_files = [
'Classes/**/*.h'
]
s.ios.dependency 'Flutter'
s.osx.dependency 'FlutterMacOS'
s.dependency 'ReachabilitySwift'
s.platform = :ios, '12.0'
s.platform = {
:ios => '12.0',
:osx => '10.14'
}
s.ios.deployment_target = '12.0'
s.osx.deployment_target = '10.14'
s.swift_version = '5.0'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.resource_bundles = {'connectivity_plus_privacy' => ['PrivacyInfo.xcprivacy']}
s.ios.resource_bundles = {'connectivity_plus_privacy' => ['PrivacyInfo.xcprivacy']}
end
1 change: 1 addition & 0 deletions packages/connectivity_plus/connectivity_plus/ios

This file was deleted.

1 change: 1 addition & 0 deletions packages/connectivity_plus/connectivity_plus/macos

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2d628dd

Please sign in to comment.