• This repository has been archived on 11/Jan/2019
  • Stars
    star
    238
  • Rank 169,306 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Light Realm-powered content management system

RealmContent

CI Status Version License Platform

This library is powered by RealmSwift ( Realm Docs ) and automatically provides a CMS capabilities to an iOS app.

Table of contents:

  1. Code Example
  2. Usage
  3. Installation
  4. Credits

Quick Showcase (~3 min)

Video Thumbnail

Links to video and blog article about RealmContent.

Code Example

The demo app in this repo includes a full working example of an app using RealmContent. Here's the crash-course:

1) Import RealmSwift and RealmContent

import RealmSwift
import RealmContent

Your app would normally have a list of objects it syncs from the Realm Object Server:

Once you import RealmContent it will expose two new models which Realm will add to your default schema.

If you're using multiple Realm files, add ContentPage and ContentElement to the desired object schema.

2) Create a content data source

To display a list of the available content in your app you can use the ContentListDataSource class that RealmContent provides you with:

let items = ContentListDataSource(style: .sectionsByTag)

Use .plain for a plain list or .sectionsByTag for a list with sections.

3) Initialize the data source

Tell the data source, which realm you will be using (this is where, in case you have more than one, you indicate which one).

items.loadContent(from: try! Realm())

You can also have the data source automatically tell your table or collection view to reload the data whenever changes come in from the Realm Object Server:

items.updating(view: tableView)

4) Implement your table view or collection view data source methods like usual, but use the content data source like so:

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return items.numberOfSections
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.numberOfItemsIn(section: section)
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return items.titleForSection(section: section)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = items.itemAt(indexPath: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!

        cell.accessoryType = .disclosureIndicator
        cell.textLabel?.text = "Β»  \(item.title ?? "...")"

        return cell
    }
}

This will show the list of available content.

The next step is to show the "pages" of content on screen, which you do by presenting a custom view controller.

5) Presenting a content "page"

Present a ContentViewController instance to display content. You can do this from a table/collection view tap delegate method or from an arbitrary piece of code. Here's an example of how would it work after a tap on a table cell:

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let item = items.itemAt(indexPath: indexPath)
        let vc = ContentViewController(page: item)

        navigationController!.pushViewController(vc, animated: true)
    }
}

The presented view controller uses a table view to dynamically show the content from the given ContentPage element. In case you change the content remotely the changes will be reflected in real-time on screen.

Which brings us to...

Usage

You work with two types of objects ContentPage and ContentElement. The former represents a news article, blog post, product page, and whatever other content you have. The latter - a piece of content within a page, this could be a paragraph of text, a heading, a sub-heading, an image, or a link.

You can create all of this content via the Realm Browser app and update the content on all your app's client devices in real-time.

Pages

You can add new ContentPage objects via the Realm Browser or from code:

  • title - the page title, used autom. in the content list (optional)
  • priority - integer used to order automatically the content (optional)
  • mainColor - an accent color for headings and links (optional)
  • lang - used by developer to filter content by language (optional)
  • tag - used by developer to filter content and automatically group in the content list (optional)
  • id - used by developer to assign id to page (optional)
  • elements - an ordered list of ContentElements

Elements

When you click (in the Realm Browser) on the elements cell you will see the content elements of the content page. You can delete, add, or re-order them (via drag'n'drop).

1) Headings

Four levels of headings from h1 to h4, which get the system heading and title font sizes, so they automatically adjust size in case your users use the usability features of iOS. The headings will automatically adopt the mainColor value (hex #004433 or named color pink) or fallback on the TextContentCell.defaultTextColor color value, which you can set from code.

2) Text and links

An element of type p creates a paragraph of text.

3) Image

An element of type img fetches an image from the web and caches it locally. The image will be downscaled when wider than the view controller's view width.

4) Links

If you set the value of the url column to a valid url address, the text or image will become tappable elements and open the target url either in a safari view controller or directly in Safari (check the docs for details).

If a link starts with the app:// scheme it will, upon a tap, call your custom URL handling closure so you have the chance to react to link taps in your app's code. Here's a quick example:

// example link: "app://cart/product/id/123"

let vc = ContentViewController(page: myContentPageObject)
vc.openCustomURL = { url in 
  if url.host == "cart" {
    myCart.addProductWithId(url.lastPathComponent)
  }
}
present(vc, animated: true)

Markdown

The library includes class for rendering Markdown content. If you're using CocoaPods make sure to include the sub-spec called RealmContent.Markdown.

MarkdownViewController is an alternative to ContentViewController; instead of using a native table view to render all elements interactively it simply uses a full-screen web view and renders all elements as markdown and/or HTML.

This approach enables you to push HTML from the server inside any ContentElement and render anything you want. You gain that at the cost of some of the performance gains for larger documents and change animations that you get with the native table view controller.

You can either use MarkdownViewController as the class of a controller in your storyboard or you can create an instance manually and present it on screen:

let vc = MarkdownViewController(page: myContentPageObject)
vc.openCustomURL = { customUrl in ... }
vc.usesSafariController = true
vc.customCssStyle = "h1 {color: purple;}"
present(vc, animated: true)

Installation

RealmContent is available through CocoaPods. To install it, simply add the following line to your Podfile:

# to include the core library
pod "RealmContent"

# or to include markdown and core
pod "RealmContent.Core"
pod "RealmContent.Markdown"

Alternatively, include the source code files directly into your own project.

Credits

License

MIT licensed.

About

The names and logos for Realm are trademarks of Realm Inc.

We ❀️ open source software!

See our other open source projects, read our blog, or say hi on twitter (@realm).

analytics

More Repositories

1

SwiftLint

A tool to enforce Swift style and conventions.
Swift
18,607
star
2

realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
Objective-C
16,280
star
3

realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
Java
11,451
star
4

jazzy

Soulful docs for Swift & Objective-C
Ruby
7,341
star
5

realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
TypeScript
5,758
star
6

realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
C#
1,243
star
7

realm-core

Core database component for the Realm Mobile Database SDKs
C++
1,016
star
8

realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Kotlin
942
star
9

realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Dart
758
star
10

SwiftCov

A tool to generate test code coverage information for Swift.
Swift
562
star
11

realm-browser-osx

DEPRECATED - Realm Browser for Mac OS X has been replaced by realm-studio which is cross platform.
Objective-C
501
star
12

realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
Java
414
star
13

realm-tasks

To Do app built with Realm, inspired by Clear for iOS
Swift
369
star
14

summer-of-swift

An ephemeral contest to learn Swift by doing
308
star
15

realm-studio

Realm Studio
TypeScript
300
star
16

realm-object-server

Tracking of issues related to the Realm Object Server and other general issues not related to the specific SDK's
Shell
293
star
17

realm-cocoa-converter

A library that provides the ability to import/export Realm files from a variety of data container formats.
Swift
219
star
18

realm-draw

The official Realm Draw app used in promotional videos
C#
163
star
19

github-gantt

Generate Gantt Charts From Github Issues!
JavaScript
156
star
20

realm-object-store

Cross-platform abstractions used within Realm products
C++
119
star
21

realm-kotlin-samples

Samples demonstrating the usage of Realm-Kotlin SDK
Kotlin
91
star
22

RChat

Swift
88
star
23

realm-graphql

GraphQL client for Realm Object Server
TypeScript
80
star
24

realm-loginkit

A generic interface for logging in to Realm Mobile Platform apps
Swift
74
star
25

realm-cpp

Realm C++
C++
73
star
26

realm-dart-samples

Samples for Realm Flutter and Realm Dart SDKs
C++
68
star
27

EventKit

A template conference app, featuring real-time schedule and data changes & running on Realm πŸš€
Swift
63
star
28

realm-scanner

A scanning app that can analyze and report on any photos it is given
Java
54
star
29

react-realm-context

Components that simplifies using Realm with React
TypeScript
50
star
30

realm-graphql-service

GraphQL service for Realm Object Server
TypeScript
43
star
31

RealmPop

Java
39
star
32

my-first-realm-app

ToDo demo app using Realm and Realm Object Server to synchronize tasks.
Java
38
star
33

task-tracker-swiftui

Simple task manager using Realm and SwiftUI
Swift
36
star
34

realm-dvdrental

Demo inventory application which synchronizes data originating in Postgres via the Realm Postgres data connector.
Swift
36
star
35

realm-android-user-store

Java
35
star
36

realm-inventory

An sample inventory app demonstrating safe counters via Lists and Realm Counters
Swift
32
star
37

unity-examples

C#
30
star
38

realm-teamwork-MR

A Realm demo app showing an idealized version of a field-service type application using multiple Realms, permissions, etc
Swift
26
star
39

FindOurDevices

A React Native + MongoDB Realm application for allowing users to see location and movement of their own devices or those of people in the same private group.
JavaScript
25
star
40

node-template-project

A template for your Node and TypeScript Project with Visual Studio Code Debugging!
TypeScript
23
star
41

realm-flipper-plugin

A Flipper plugin to debug React Native applications using a Realm database.
TypeScript
20
star
42

realm-java-benchmarks

Kotlin
18
star
43

realm-drawkit

A modular drawing library that uses RMP for collaboration
Swift
17
star
44

aws-devicefarm

Github action for triggering runs on AWS devicefarm
JavaScript
17
star
45

Scrumdinger

Showing how the app from Apple's SwiftUI tutorial can be enhanced by adding Realm
Swift
16
star
46

awesome-realm

A curated list of awesome Realm resources, libraries, tools and applications
15
star
47

roc-ios

Swift
14
star
48

realm-search

An example implementation of synchronizing specific objects from a massive global Realm.
Swift
13
star
49

roc-ios-controller

A Chat Controller powered by Realm and Chatto
Swift
13
star
50

realm-puzzle

A small collaborative game where players work to complete a jigsaw puzzle.
Objective-C
12
star
51

realm-sync-demos

Demo apps for MongoDB Realm Sync
Kotlin
11
star
52

RCurrency

Swift
10
star
53

jazzy-integration-specs

Integration specs for https://github.com/realm/jazzy
HTML
10
star
54

realm-MultiUserTasksTutorial

Walk-though of constructing a multi-user example using Realm Tasks
Swift
9
star
55

charts

A Collection of Helm Charts
Smarty
9
star
56

realm-surveys

A reactive survey application powered by Realm
Swift
9
star
57

realm-connectors

Realm Object Server Data Connectors
8
star
58

realm-cloud-functions-demo

Realm & IBM Cloud Functions demo
JavaScript
8
star
59

realm-dotnet-groupedcollection

C#
8
star
60

realm-dotnet-lfs

C#
8
star
61

electron-react-samples

TypeScript
7
star
62

Realm-Sweeper

Swift
7
star
63

FindOurDevices-backend

A backend MongoDB Realm application for allowing users to see location and movement of their own devices or those of people in the same private group.
JavaScript
7
star
64

Realm-Drawing

Swift
6
star
65

feedback-manager

A simple Realm-powered feedback app that employs the Azure Text Analytics API to extract sentiment and key phrases from tickets
C#
5
star
66

BarCodes-Demo

A small demo app to show how to scan barcode and place into into a Realm
Swift
5
star
67

realm-qna

question and answer app
Swift
4
star
68

realm-dotnet-samples

C#
4
star
69

realm-swift-samples

Sample applications for realm-swift database
Swift
4
star
70

realm-tools

Various tools for testing Realm
TypeScript
3
star
71

ci-actions

A repository for commonly used GitHub Actions inside the various realm repositories
TypeScript
3
star
72

xamarin-examples-architecture

Test
C#
3
star
73

realm-flexible-sync-test-api

Sample App for A/B testing two version of the Flexible Sync API
Swift
3
star
74

realm-SharedTasks

Swift
2
star
75

unity-examples-3d-chess

Examples and tutorials for the Realm Unity SDK.
2
star
76

global-notifier-design-patterns

C++
2
star
77

realm.github.io

HTML
2
star
78

realm-object-server-cognito-auth

Cognito authentication provider for Realm Object Server
TypeScript
2
star
79

aws-devicefarm-sample-data

Sample data to use with the AWS devicefarm action
Java
1
star
80

realm-lua-bootcamp

Teaching the basics of building a Realm SDK
C++
1
star
81

realm-crowdcircle

A collaborative experimental game, played between 6 teams
Swift
1
star
82

realm-js-playground

JavaScript
1
star