<commit_before>//
//  MobileMessaging.swift
//  MobileMessaging
//
//  Created by <NAME>. on 17/02/16.
//
//

import Foundation
import MMAFNetworking

public final class MobileMessaging: NSObject {
	
	//MARK: Public
	public class func withApplicationCode(code: String, notificationType: UIUserNotificationType) -> MobileMessaging {
		sharedInstance = MobileMessaging(applicationCode: code, notificationType: notificationType)
		return sharedInstance!
	}
	
	public func withBackendBaseURL(urlString: String) -> MobileMessaging {
		remoteAPIBaseURL = urlString
		return self
	}
	
	public func withGeofencingServiceDisabled(disabled: Bool) -> MobileMessaging {
		MMGeofencingService.geoServiceEnabled = !disabled
		return self
	}
	
	/// Starts a new Mobile Messaging session.
	///
	/// This method should be called form AppDelegate's `application(_:didFinishLaunchingWithOptions:)` callback.
	/// - remark: For now, Mobile Messaging SDK doesn't support Badge. You should handle the badge counter by yourself.
	/// - parameter userNotificationType: Preferable notification types that indicating how the app alerts the user when a  push notification arrives.
	/// - parameter applicationCode: The application code of your Application from Push Portal website.
	/// - parameter backendBaseURL: Your backend server base URL, optional parameter. Default is http://oneapi.infobip.com.
	public func start(completion: (Void -> Void)? = nil) {
		MMLogDebug("Starting MobileMessaging service...")
		
		MobileMessaging.singletonQueue.executeAsync {
			do {
				var storage: MMCoreDataStorage?
				switch self.storageType {
				case .InMemory:
					storage = try MMCoreDataStorage.makeInMemoryStorage()
				case .SQLite:
					storage = try MMCoreDataStorage.makeSQLiteStorage()
				}
				if let storage = storage {
					self.storage = storage
					let installation = MMInstallation(storage: storage, baseURL: self.remoteAPIBaseURL, applicationCode: self.applicationCode)
					self.currentInstallation = installation
					let user = MMUser(installation: installation)
					self.currentUser = user
					let messageHandler = MMMessageHandler(storage: storage, baseURL: self.remoteAPIBaseURL, applicationCode: self.applicationCode)
					self.messageHandler = messageHandler
					self.appListener = MMApplicationListener(messageHandler: messageHandler, installation: installation, user: user)
					
					MMGeofencingService.sharedInstance.start()
					
					MMLogInfo("MobileMessaging SDK service successfully initialized.")
				}
			} catch {
				MMLogError("Unable to initialize Core Data stack. MobileMessaging SDK service stopped because of the fatal error.")
			}

			if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() && self.currentInstallation?.deviceToken == nil {
				MMLogDebug("The application is registered for remote notifications but MobileMessaging lacks of device token. Unregistering...")
				UIApplication.sharedApplication().unregisterForRemoteNotifications()
			}
			
			if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() == false {
				MMLogDebug("Registering for remote notifications...")
				UIApplication.sharedApplication().registerForRemoteNotifications()
			}
		}
	}
	
	/// Stops the currently running Mobile Messaging session.
	public class func stop(cleanUpData: Bool = false) {
		if cleanUpData {
			MobileMessaging.sharedInstance?.cleanUpAndStop()
		} else {
			MobileMessaging.sharedInstance?.stop()
		}
	}
	
	/// Logging utility is used for:
	/// - setting up the logging options and logging levels.
	/// - obtaining a path to the logs file in case the Logging utility is set up to log in file (logging options contains `.file` option).
	public internal(set) static var loggingUtil = MMLoggingUtil()
	
	/// This service manages geofencing areas, emits geografical regions entering/exiting notifications.
	///
	/// You access the Geofencing service APIs through this property.
	public internal(set) static var geofencingService = MMGeofencingService.sharedInstance<commit_msg>Fix: registration for push settings was accidentally removed Fix: docs for public API<commit_after>//
//  MobileMessaging.swift
//  MobileMessaging
//
//  Created by <NAME>. on 17/02/16.
//
//

import Foundation
import MMAFNetworking

public final class MobileMessaging: NSObject {
	
	//MARK: Public
	/// - parameter userNotificationType: Preferable notification types that indicating how the app alerts the user when a  push notification arrives.
	/// - parameter applicationCode: The application code of your Application from Push Portal website.
	public class func withApplicationCode(code: String, notificationType: UIUserNotificationType) -> MobileMessaging {
		sharedInstance = MobileMessaging(applicationCode: code, notificationType: notificationType)
		return sharedInstance!
	}
	
	/// - parameter backendBaseURL: Your backend server base URL, optional parameter. Default is http://oneapi.infobip.com.
	public func withBackendBaseURL(urlString: String) -> MobileMessaging {
		remoteAPIBaseURL = urlString
		return self
	}
	
	/// - parameter disabled: the flag is used to disable the default Geofencing service startup procedure.
	public func withGeofencingServiceDisabled(disabled: Bool) -> MobileMessaging {
		MMGeofencingService.geoServiceEnabled = !disabled
		return self
	}
	
	/// Starts a new Mobile Messaging session.
	///
	/// This method should be called form AppDelegate's `application(_:didFinishLaunchingWithOptions:)` callback.
	/// - remark: For now, Mobile Messaging SDK doesn't support Badge. You should handle the badge counter by yourself.
	public func start(completion: (Void -> Void)? = nil) {
		MMLogDebug("Starting MobileMessaging service...")
		
		MobileMessaging.singletonQueue.executeAsync {
			do {
				var storage: MMCoreDataStorage?
				switch self.storageType {
				case .InMemory:
					storage = try MMCoreDataStorage.makeInMemoryStorage()
				case .SQLite:
					storage = try MMCoreDataStorage.makeSQLiteStorage()
				}
				if let storage = storage {
					self.storage = storage
					let installation = MMInstallation(storage: storage, baseURL: self.remoteAPIBaseURL, applicationCode: self.applicationCode)
					self.currentInstallation = installation
					let user = MMUser(installation: installation)
					self.currentUser = user
					let messageHandler = MMMessageHandler(storage: storage, baseURL: self.remoteAPIBaseURL, applicationCode: self.applicationCode)
					self.messageHandler = messageHandler
					self.appListener = MMApplicationListener(messageHandler: messageHandler, installation: installation, user: user)
					
					MMGeofencingService.sharedInstance.start()
					
					MMLogInfo("MobileMessaging SDK service successfully initialized.")
				}
			} catch {
				MMLogError("Unable to initialize Core Data stack. MobileMessaging SDK service stopped because of the fatal error.")
			}

			if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() && self.currentInstallation?.deviceToken == nil {
				MMLogDebug("The application is registered for remote notifications but MobileMessaging lacks of device token. Unregistering...")
				UIApplication.sharedApplication().unregisterForRemoteNotifications()
			}
			
			UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: self.userNotificationType, categories: nil))
			
			if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() == false {
				MMLogDebug("Registering for remote notifications...")
				UIApplication.sharedApplication().registerForRemoteNotifications()
			}
		}
	}
	
	/// Stops the currently running Mobile Messaging session.
	public class func stop(cleanUpData: Bool = false) {
		if cleanUpData {
			MobileMessaging.sharedInstance?.cleanUpAndStop()
		} else {
			MobileMessaging.sharedInstance?.stop()
		}
	}
	
	/// Logging utility is used for:
	/// - setting up the logging options and logging levels.
	/// - obtaining a path to the logs file in case the Logging utility is set up to log in file (logging options contains `.file` option).
	public internal(set) static var loggingUtil = MMLoggingUtil()
	
	/// This service manages geofencing areas, emits geografical regions entering/exiting notifications.
	///
	/// You access the Geofencing service APIs through this property.
	public internal(set) static var geofencingService = MMGeofencingService.sharedInstance
{'fraction_non_alphanumeric': 0.08187552064738783, 'fraction_numerical': 0.0014280614066404856, 'mean_word_length': 4.288860918816866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '951206', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2116, 'n_words': 681}