> For the complete documentation index, see [llms.txt](https://boernai290.gitbook.io/reimagine/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boernai290.gitbook.io/reimagine/documentation/carbonite-modules.md).

# Carbonite Modules

## Creating a new module

{% hint style="warning" %}
Modules built for **Carbonite** need to subclass *UIView*, each new module is added into an array of the *ModulesView* which the collection view uses to display those modules.

Modules must use AutoLayout to help them fit multiple screen sizes/window sizes. Using -initWithFrame: may result in issues and support will not be provided.
{% endhint %}

```objectivec
// MyModuleView.h

@interface MyModuleView : UIView
@end
```

The *@interface* in the *\*.h* file is where we can store any variables, properties or functions for use inside or outside the class. Properties and methods can be accessed outside the class while variables (places in curly brackets) cannot be seen and thus not be used by outside classes.

```objectivec
// MyModuleView.m
#import "MyModuleView.h"

@implementation MyModuleView
-(id) initWithFrame:(CGRect)frame {
  if(self = [super initWithFrame:frame]) {
    [self setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    // code starts here
  } return self;
}
@end
```

The *@implementation* in the *\*.m* file is where the magic happens, this is where we code the majority of the class, starting with the -initWithFrame: method, if ever using .nib or .xib files (Xcode), you should also use -initWithCoder:

## Adding a module to Carbonite

```objectivec
// Tweak.x

@interface ModulesView : UIView
-(void) addModuleWithView:(UIView *)arg1;
@end

@interface SBReachabilityWindow : UIWindow
@property (nonatomic, retain) ModulesView *modulesView;
@end
```

Carbonite uses the *SBReachabilityWindow* to add a property of *ModulesView* which holds all modules, *CarboniteView* which is the main module is added initially and any further modules are added after.

```objectivec
// Tweak.x

%hook SBReachabilityWindow
-(void) layoutSubviews { // not recommended but Carbonite handles duplication issues
  %orig;
  
  MyModuleView *myModuleView = [[MyModuleView alloc] init];
  [self.modulesView addModuleWithView:myModuleView];
  // ModulesView should handle the array correctly by only adding a new view once
}
%end
```

{% hint style="warning" %}
-layoutSubviews is never recommended when making tweaks. Carbonite is made in a way that it doesn't duplicate itself when a layout call is made.
{% endhint %}

Running this code as-is, with Carbonite installed will add it to the modules, this is your blank slate, add to it what you want and start coding an amazing module for use in an innovative tweak!

## Suggestions

* Weather Module
* Maps Module
* Calculator Module (suggested by a user on Reddit)
