Skip to content

Commit

Permalink
添付メディアのサムネイル表示を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
rinsuki committed Nov 16, 2017
1 parent 7477424 commit b1c495e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions iMast/GoodUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ var defaultValues: [String: Any] = [
"widget_filter": "",
"nowplaying_format": "#nowplaying {title} - {artist} ({albumTitle})",
"visibility_emoji": true,
"thumbnail_height": Int(50),
]

func MastodonVersionStringToInt(_ versionStr_: String) -> Int {
Expand Down
14 changes: 9 additions & 5 deletions iMast/MastodonPost.xib
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<outlet property="iconHeightConstraint" destination="jjY-4l-N6P" id="8Oz-xq-tic"/>
<outlet property="iconView" destination="sQD-aX-zO8" id="8o3-Yb-f9R"/>
<outlet property="iconWidthConstraint" destination="E3g-PX-YCf" id="YdF-2b-CxM"/>
<outlet property="imageThumbnailStackView" destination="wXc-zh-T2a" id="tYZ-0h-sPC"/>
<outlet property="textView" destination="GZJ-sR-UHI" id="gt1-fH-ubV"/>
<outlet property="timeView" destination="Gh3-yR-Njo" id="x1s-yn-PfL"/>
<outlet property="userView" destination="4nS-XE-p4g" id="Q4O-u2-HU5"/>
Expand All @@ -35,18 +36,18 @@
<rect key="frame" x="64" y="8" width="443" height="303"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="1000" verticalCompressionResistancePriority="1000" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="6ba-c3-hBS">
<rect key="frame" x="0.0" y="0.0" width="443" height="153"/>
<rect key="frame" x="0.0" y="0.0" width="443" height="20.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" verticalHuggingPriority="750" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4nS-XE-p4g">
<rect key="frame" x="0.0" y="66.5" width="443" height="20.5"/>
<rect key="frame" x="0.0" y="0.0" width="443" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</stackView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" verticalHuggingPriority="1000" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="250" scrollEnabled="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="GZJ-sR-UHI" userLabel="Post Text View">
<rect key="frame" x="0.0" y="153" width="443" height="150"/>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" horizontalHuggingPriority="1000" verticalHuggingPriority="1000" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="250" scrollEnabled="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="GZJ-sR-UHI" userLabel="Post Text View">
<rect key="frame" x="0.0" y="20.5" width="443" height="132.5"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="14" id="Oxl-yF-MWS"/>
Expand All @@ -55,10 +56,13 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="allCharacters"/>
</textView>
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" axis="vertical" distribution="fillEqually" spacing="5" translatesAutoresizingMaskIntoConstraints="NO" id="wXc-zh-T2a">
<rect key="frame" x="0.0" y="153" width="443" height="150"/>
</stackView>
</subviews>
</stackView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="750" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" text="00:00:00" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Gh3-yR-Njo">
<rect key="frame" x="435" y="74" width="71.5" height="20.5"/>
<rect key="frame" x="435" y="7.5" width="71.5" height="20.5"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="0.8967869718" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand Down
28 changes: 28 additions & 0 deletions iMast/MastodonPostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MastodonPostView: UIView, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var iconWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var iconHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var imageThumbnailStackView: UIStackView!
var json: JSON?

override init(frame: CGRect) {
Expand Down Expand Up @@ -113,6 +114,23 @@ class MastodonPostView: UIView, UITextViewDelegate {
self.isUserInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapPost)))
// /-- タッチ周り --
let thumbnail_height = UserDefaults.standard.integer(forKey: "thumbnail_height")
if thumbnail_height != 0 {
json["media_attachments"].arrayValue.enumerated().forEach({ (index, media) in
let imageView = UIImageView()
getImage(url: media["preview_url"].stringValue).then({ (image) in
imageView.image = image
})
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.layoutIfNeeded()
imageView.heightAnchor.constraint(equalToConstant: CGFloat(thumbnail_height)).isActive=true
imageView.isUserInteractionEnabled = true
imageView.tag = 100+index
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapImage)))
self.imageThumbnailStackView.addArrangedSubview(imageView)
})
}
self.layoutIfNeeded()
}

Expand All @@ -139,6 +157,16 @@ class MastodonPostView: UIView, UITextViewDelegate {
self.viewController?.navigationController?.pushViewController(newVC, animated: true)
}

func tapImage(sender: UITapGestureRecognizer) {
if self.json == nil {
return
}
let json = self.json!["reblog"].isEmpty ? self.json! : self.json!["reblog"]
let media = json["media_attachments"].arrayValue[sender.view!.tag-100]
let safari = SFSafariViewController(url: URL(string: media["url"].stringValue)!)
self.viewController?.present(safari, animated: true, completion: nil)
}

func textView(_ textView: UITextView, shouldInteractWith shareUrl: URL, in characterRange: NSRange) -> Bool {
var urlString = shareUrl.absoluteString
for mention in self.json!["mentions"].arrayValue {
Expand Down
8 changes: 8 additions & 0 deletions iMast/OtherMenuTopTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ class OtherMenuTopTableViewController: UITableViewController {
row.title = "公開範囲を絵文字で表示"
row.userDefaultsConnect(name: "visibility_emoji")
}
<<< SliderRow() { row in
row.title = "サムネイルの高さ"
row.maximumValue = 100
row.minimumValue = 0
row.steps = 100/5
row.userDefaultsConnect(name: "thumbnail_height")
}

vc.form +++ Section("ウィジェット")
<<< LabelRow() { row in
row.title = "投稿フォーマット"
Expand Down

0 comments on commit b1c495e

Please sign in to comment.