Commit 310abcf6 authored by lmj_521aiau@163.com's avatar lmj_521aiau@163.com

Merge branch 'branch_1.0.1' of gitlab.huolea.com:lmi/shorthandmaster into branch_1.0.1

parents ff4cdcd3 3a2189a1
......@@ -12,8 +12,19 @@ import Photos
class SHInputController: SHBaseViewController
{
// MARK: - property
private var viewModel : SHInputViewModel = SHInputViewModel()
private var shInputView : SHInputView!
/// 文件夹model
var folderModel : SHRecordFolderModel!
/// 保存成功回调
var saveSuccessCallBack:((SHRecordFolderModel)->Void)?
/// 存储数据的model
private var dataModel: SHRecordModel = SHRecordModel()
/// 未填写标题时的默认标题
private let defaultTitle: String = "新建笔记"
private var shInputView: SHInputView!
private let recorder_mp3: SHMp3RecordManager = SHMp3RecordManager.shared()
......@@ -52,62 +63,67 @@ class SHInputController: SHBaseViewController
self.shInputView.bottomView.stopAudioButton.addTarget(self, action: #selector(didSelectedStopAudioButton(sender:)), for: UIControl.Event.touchUpInside)
self.shInputView.bottomView.keyboardButton.addTarget(self, action: #selector(didSelectedKeyboardButton(sender:)), for: UIControl.Event.touchUpInside)
// 有记录显示记录
if UserDefaults.standard.value(forKey: "InputRecord") != nil
{
self.shInputView.placeholder.isHidden = true
let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any]
let attributeString : NSMutableAttributedString = NSMutableAttributedString()
let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
attributeString.append(titleString)
self.viewModel.title = titleString.string
let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])
attributeString.append(contentString)
let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
for imageDictionary : [String : Any] in imageArray
{
let imageDataString : String = imageDictionary["imageData"] as! String
let imageData : Data = NSData(base64Encoded: imageDataString, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data
let image : UIImage = UIImage(data: imageData)!
// 调整图片大小, 宽相同, 高随比例变化
let newWidth = kScreenWidth - 20 * 2
let newHeight = newWidth * image.size.height / image.size.width
let newSize = CGSize(width: newWidth, height: newHeight)
let imageLocation : Int = imageDictionary["imageLocation"] as! Int
let attachment = NSTextAttachment.init()
attachment.image = image
attachment.bounds = CGRect(origin: CGPoint.zero, size: newSize)
let imageString = NSMutableAttributedString(attachment: attachment)
// 设置图片文字属性
imageString.addAttributes((SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])!, range: NSRange(location: 0, length: imageString.length))
// 设置图片间距
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 18
imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length))
attributeString.insert(imageString, at: imageLocation)
}
// TODO: 先做新增, 查看和编辑稍后再做
// // 有记录显示记录
// if UserDefaults.standard.value(forKey: "InputRecord") != nil
// {
// self.shInputView.placeholder.isHidden = true
//
// let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any]
//
// let attributeString : NSMutableAttributedString = NSMutableAttributedString()
//
// let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
// attributeString.append(titleString)
//// self.viewModel.title = titleString.string
// self.dataModel.rename = titleString.string
//
// let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])
// attributeString.append(contentString)
//
// let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
//
// for imageDictionary : [String : Any] in imageArray
// {
// let imageDataString : String = imageDictionary["imageData"] as! String
//
// let imageData : Data = NSData(base64Encoded: imageDataString, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data
// let image : UIImage = UIImage(data: imageData)!
//
// // 调整图片大小, 宽相同, 高随比例变化
// let newWidth = kScreenWidth - 20 * 2
// let newHeight = newWidth * image.size.height / image.size.width
// let newSize = CGSize(width: newWidth, height: newHeight)
//
// let imageLocation : Int = imageDictionary["imageLocation"] as! Int
//
//
// let attachment = NSTextAttachment.init()
// attachment.image = image
// attachment.bounds = CGRect(origin: CGPoint.zero, size: newSize)
// let imageString = NSMutableAttributedString(attachment: attachment)
//
// // 设置图片文字属性
// imageString.addAttributes((SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])!, range: NSRange(location: 0, length: imageString.length))
// // 设置图片间距
// let paragraphStyle = NSMutableParagraphStyle()
// paragraphStyle.lineSpacing = 18
// imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length))
//
// attributeString.insert(imageString, at: imageLocation)
// }
//
// self.shInputView.textView.attributedText = attributeString
// }
// else
// {
// // 默认文字样式
// self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
// }
self.shInputView.textView.attributedText = attributeString
}
else
{
// 默认文字样式
self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
}
}
// MARK: 设置控件位置
func setUpSubviewsLocation()
......@@ -131,49 +147,55 @@ class SHInputController: SHBaseViewController
// MARK: 点击navigationBar右侧按钮
@objc func didSelectedNavRightButton()
{
self.viewModel.imageArray = SHInputViewModel.getImageArrayWithTextView(textView: self.shInputView.textView)
self.dataModel.recordingType = 1
self.viewModel.content = self.shInputView.textView.text.substring(self.viewModel.title.length, length: self.shInputView.textView.text.length - self.viewModel.title.length)!
self.dataModel.createDate = Date()
self.dataModel.modifyDate = Date()
var dic : [String : Any] = Dictionary()
dic.updateValue(self.viewModel.title, forKey: "title")
dic.updateValue(self.viewModel.content, forKey: "content")
dic.updateValue(self.viewModel.imageArray, forKey: "imageArray")
let imageArray = SHInputViewModel.setUpImageArrayWithTextView(textView: self.shInputView.textView)
UserDefaults.standard.setValue(dic, forKey: "InputRecord")
UserDefaults.standard.synchronize()
self.dataModel.imagesPath = imageArray.imagesPathArray
self.dataModel.imagesIndex = imageArray.imagesIndexArray
self.navigationController?.popViewController(animated: true)
if self.dataModel.rename.length == 0
{
self.dataModel.rename = defaultTitle
}
// MARK: 获取textView中的图片数据, 保存成一个数组
// func getImageArrayWithTextView(textView : UITextView) -> Array<Any>
// {
// var imageArray = Array<Any>()
//
// let attributedString = textView.attributedText
//
// attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
//
// var imageDictionary : Dictionary<String, Any> = Dictionary()
//
// if attributes != nil
// {
// let imageAttachment = attributes as! NSTextAttachment
//
// if imageAttachment.image != nil
// {
// imageDictionary.updateValue(UIImage.jpegData(imageAttachment.image!), forKey: "imageData")
// imageDictionary.updateValue(range.location, forKey: "imageLocation")
//
// imageArray.append(imageDictionary)
// }
// }
//
// })
//
// return imageArray
// }
self.dataModel.txt = self.shInputView.textView.text.substring(self.dataModel.rename.length, length: self.shInputView.textView.text.length - self.dataModel.rename.length)!
// 保存到沙盒
var recordList = CRUserDefaults.recordList
var i = 0;
// var folderModel = SHRecordFolderModel()
for dict in recordList!
{
var model = SHRecordFolderModel()
model = getDataDictWith(dict: dict)
if model.id == self.folderModel.id
{
model.dataSources.append(self.dataModel)
self.folderModel = model
break
}
i += 1
}
recordList![i] = getDictWith(obj: self.folderModel!)
CRUserDefaults.recordList = recordList
self.saveSuccessCallBack?(self.folderModel)
self.navigationController?.popViewController(animated: true)
}
// MARK: 点击键盘按钮
@objc func didSelectedKeyboardButton(sender : UIButton)
......@@ -203,7 +225,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
// 获取标题文字位置
let titleRange = NSRange(location: 0, length: self.viewModel.title.length)
let titleRange = NSRange(location: 0, length: self.dataModel.rename.length)
// 在标题位置输入的内容为标题
if range.location <= titleRange.location + titleRange.length
......@@ -224,7 +246,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate
textViewString.insert(text, at: range.location)
// 保存标题文字
self.viewModel.title = textViewString.substring(with: NSRange(location: 0, length: range.location + text.length))
self.dataModel.rename = textViewString.substring(with: NSRange(location: 0, length: range.location + text.length))
}
}
else
......@@ -317,18 +339,21 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro
let newSize = CGSize(width: newWidth, height: newHeight)
// 如果没有标题添加"未命名"作为标题
if self.viewModel.title.length == 0
// 如果没有标题添加默认标题
if self.dataModel.rename.length == 0
{
// 标题
let titleString = NSMutableAttributedString(string: "未命名", attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
let titleString = NSMutableAttributedString(string: self.defaultTitle, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
self.shInputView.textView.attributedText = titleString
self.shInputView.placeholder.isHidden = true
self.viewModel.title = titleString.string
self.dataModel.rename = titleString.string
}
// 如果占位文字未隐藏, 隐藏占位文字
self.shInputView.placeholder.isHidden = true
// 前文
let textString = NSMutableAttributedString(attributedString: self.shInputView.textView.attributedText)
......
......@@ -11,12 +11,15 @@ import UIKit
class SHInputViewModel: NSObject {
// MARK: - property
/// 标题文字
var title : String = ""
/// 内容文字
var content : String = ""
/// 图片数组
var imageArray : Array = Array<Any>()
// /// 标题文字
// var title : String = ""
// /// 内容文字
// var content : String = ""
// /// 图片数组
// var imageArray : Array = Array<Any>()
// var recordModel : SHRecordModel = SHRecordModel()
......@@ -50,34 +53,79 @@ class SHInputViewModel: NSObject {
return titleAttributes
}
// MARK: 获取图片数组
class func getImageArrayWithTextView(textView : UITextView) -> Array<Any>
// MARK: 获取textView中的图片, 保存到本地, 并生成数组
class func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>)
{
/// 图片路径数组
var imagesPathArray = Array<String>()
/// 图片位置数组
var imagesIndexArray = Array<Int>()
// 创建文件夹
/// 文件夹路径
let subFile = Date().format("yyyyMMdd")
let time = Date.init().timeStamp
let filePath = DocumentPath + "/" + subFile + time
do
{
try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
}
catch let error
{
var imageArray = Array<Any>()
print(error)
return (imagesPathArray, imagesIndexArray)
}
// 遍历textView文字中的NSTextAttachment
let attributedString = textView.attributedText
attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
var imageDictionary : Dictionary<String, Any> = Dictionary()
if attributes != nil
{
let imageAttachment = attributes as! NSTextAttachment
// 找到图片
if imageAttachment.image != nil
{
// 将图片保存在本地
// 把图片转成Data类型
let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))!
imageDictionary.updateValue(imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters), forKey: "imageData")
imageDictionary.updateValue(range.location, forKey: "imageLocation")
// 保存的本地路径
let imagePath = filePath + "/" + "image.jpeg" + imagesPathArray.count.string
imageArray.append(imageDictionary)
let imageUrl : URL = URL(fileURLWithPath: imagePath)
var saveSucceed : Bool = Bool(true)
// 将图片保存到沙盒
do
{
try imageData.write(to: imageUrl)
}
catch let error
{
saveSucceed = false
print(error)
}
if saveSucceed
{
// 保存成功后存储保存的路径
imagesPathArray.append(imagePath)
// 存储图片位置
imagesIndexArray.append(range.location)
}
}
}
})
return imageArray
return (imagesPathArray, imagesIndexArray)
}
}
......@@ -346,7 +346,14 @@ extension SHRecordDetailsVC {
switch sender.tag {
case 200:
let input = SHInputController.init()
input.folderModel = currentModel
self.navigationController?.pushViewController(input, animated: true)
input.saveSuccessCallBack = { model in
self.currentModel = model
self.updateocloudUI()
}
break
default:
let record = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment