Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

[WIP]refs#104 mission3 #109

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chii/iOS-client/My1DayApp/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class APIRequest {
task.resume()
}

class func postMessage(message: String, username: String, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) {
class func postMessage(message: String, username: String, image: NSData, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) {
let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: APIRequest.baseURLString + Endpoint.Messages.rawValue)!)
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

var error: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(["username":username, "body":message], options: NSJSONWritingOptions.allZeros, error: &error)
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(["username":username, "body":message, "image":image], options: NSJSONWritingOptions.allZeros, error: &error)

if error != nil{
println(error)
Expand Down
49 changes: 41 additions & 8 deletions chii/iOS-client/My1DayApp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="pQh-c6-WXV">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="pQh-c6-WXV">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<scenes>
<!--Root View Controller-->
<scene sceneID="IDU-m6-Lyb">
<objects>
<tableViewController id="JOZ-JG-2Hn" customClass="MessageTableViewController" customModule="My1DayApp" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="70" sectionHeaderHeight="22" sectionFooterHeight="22" id="Aeu-LH-nIF">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="114" sectionHeaderHeight="22" sectionFooterHeight="22" id="Aeu-LH-nIF">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.96078431369999995" green="0.96078431369999995" blue="0.96078431369999995" alpha="1" colorSpace="calibratedRGB"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="MessageCell" rowHeight="70" id="6IH-Ch-NUR" customClass="MessageTableViewCell" customModule="My1DayApp" customModuleProvider="target">
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="MessageCell" rowHeight="114" id="6IH-Ch-NUR" customClass="MessageTableViewCell" customModule="My1DayApp" customModuleProvider="target">
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="6IH-Ch-NUR" id="Ucl-m3-jPt">
<autoresizingMask key="autoresizingMask"/>
Expand All @@ -22,16 +22,30 @@
<rect key="frame" x="5" y="5" width="60" height="60"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="メッセージ" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RPy-Rd-KYT">
<rect key="frame" x="78" y="13" width="234" height="21"/>
<rect key="frame" x="78" y="46" width="234" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="時刻" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="k16-q2-dro">
<rect key="frame" x="78" y="77" width="234" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="名前" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YVg-cb-GJE">
<rect key="frame" x="78" y="17" width="234" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<outlet property="iconImageView" destination="rB6-1N-vrf" id="qKn-zx-yfW"/>
<outlet property="messageLabel" destination="RPy-Rd-KYT" id="6X3-KT-Lfn"/>
<outlet property="nameLabel" destination="YVg-cb-GJE" id="MNJ-wY-RSN"/>
<outlet property="timeLabel" destination="k16-q2-dro" id="cbS-AC-rxD"/>
</connections>
</tableViewCell>
</prototypes>
Expand Down Expand Up @@ -67,18 +81,35 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="4EN-Fz-DKa">
<rect key="frame" x="0.0" y="0.0" width="320" height="200"/>
<view contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4EN-Fz-DKa">
<rect key="frame" x="0.0" y="0.0" width="320" height="353"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Lhg-Dc-uxP">
<rect key="frame" x="74" y="67" width="246" height="128"/>
<rect key="frame" x="74" y="122" width="246" height="40"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" type="system" pointSize="18"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="logo.png" translatesAutoresizingMaskIntoConstraints="NO" id="71b-O0-MO6">
<rect key="frame" x="3" y="67" width="63" height="63"/>
</imageView>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="zL6-Gr-VnR">
<rect key="frame" x="74" y="74" width="238" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wJJ-Bk-7Nq">
<rect key="frame" x="115" y="162" width="90" height="30"/>
<state key="normal" title="Select Image">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="didTouchUpSelectImageButton:" destination="tul-4M-Dgp" eventType="touchUpInside" id="4jV-a1-WH5"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="OWR-hi-XFU">
<rect key="frame" x="40" y="205" width="240" height="128"/>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
Expand Down Expand Up @@ -120,6 +151,8 @@
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
<connections>
<outlet property="messageTextView" destination="Lhg-Dc-uxP" id="Y5X-Cg-E1p"/>
<outlet property="myImageView" destination="OWR-hi-XFU" id="Aji-qY-p0q"/>
<outlet property="userNameTextView" destination="zL6-Gr-VnR" id="gYR-cn-JMj"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="P5w-qQ-jfU" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down
8 changes: 7 additions & 1 deletion chii/iOS-client/My1DayApp/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ import UIKit
class Message {
let body: String!
let icon: UIImage?
let created_at: String?
// Mission1-1. created_at 用のインスタンス変数を追加
let username: String?

init?(dictionary: [String: AnyObject]) {
// Mission1-1 Dictionary から key:created_at の値を取得
if let body: String = dictionary["body"] as? String, let icon: String = dictionary["icon"] as? String {
if let body: String = dictionary["body"] as? String, let icon: String = dictionary["icon"] as? String, let created_at: String = dictionary["created_at"] as? String, let username: String = dictionary["username"] as? String {
self.body = body
self.icon = ImageHelper.imageWithBase64EncodedString(icon)
self.created_at = created_at
// Mission1-1 Dictionary から取得した値を created_at 用のインスタンス変数に追加
self.username = username
} else {
self.body = nil
self.icon = nil
self.created_at = nil
// Mission1-1 インスタンス変数を nil で初期化
self.username = nil
return nil
}
}
Expand Down
6 changes: 6 additions & 0 deletions chii/iOS-client/My1DayApp/MessageTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ import UIKit
class MessageTableViewCell: UITableViewCell {
@IBOutlet weak private var iconImageView: UIImageView!
@IBOutlet weak private var messageLabel: UILabel!
@IBOutlet weak private var timeLabel: UILabel!
// Mission1-1 UILabel のインスタンス変数を追加
@IBOutlet weak var nameLabel: UILabel!

override func prepareForReuse() {
self.iconImageView.image = nil
self.messageLabel.text = nil
// Mission1-1 UILabel のインスタンス変数を初期化
self.timeLabel.text = nil
self.nameLabel.text = nil
}

func setupComponentsWithMessage(message: Message) {
self.iconImageView.image = message.icon
self.messageLabel.text = message.body
// Mission1-1 UILabel のインスタンス変数に created_at の値を代入
self.timeLabel.text = message.created_at
self.nameLabel.text = message.username
}
}
36 changes: 34 additions & 2 deletions chii/iOS-client/My1DayApp/PostViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,45 @@ protocol PostViewControllerDelagate : NSObjectProtocol {
func postViewController(viewController : PostViewController, didTouchUpCloseButton: AnyObject)
}

class PostViewController: UIViewController {
class PostViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var myImagePicker: UIImagePickerController!

@IBOutlet weak private var messageTextView: UITextView!
weak var delegate: PostViewControllerDelagate?
// Mission1-2 Storyboard から UITextField のインスタンス変数を追加
@IBOutlet weak private var userNameTextView: UITextField!

@IBOutlet weak var myImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
self.messageTextView.becomeFirstResponder()
}

// MARK: - IBAction
@IBAction func didTouchUpSelectImageButton(sender: AnyObject) {
self.pickImageFromLibrary()
}

// ライブラリから写真を選択する
func pickImageFromLibrary() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(controller, animated: true, completion: nil)
}
}

// 写真を選択した時に呼ばれる
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if info[UIImagePickerControllerOriginalImage] != nil {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
myImageView.image = image
}
picker.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func didTouchUpCloseButton(sender: AnyObject) {
self.messageTextView.resignFirstResponder()
self.delegate?.postViewController(self, didTouchUpCloseButton: sender)
Expand All @@ -34,9 +61,14 @@ class PostViewController: UIViewController {

let message: String = self.messageTextView.text ?? ""
// Mission1-2 UITextField のインスタンス変数から値を取得
let userName: String = self.userNameTextView.text ?? ""
let image: UIImage? = self.myImageView.image

var imgData: NSData = NSData(data: UIImageJPEGRepresentation(image, 0.5))
imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

// Mission1-2 posetMessage の第2引数に 任意の値を渡す
APIRequest.postMessage(message, username: "名前はまだない") {
APIRequest.postMessage(message, username: userName, image: imgData) {
[weak self] (data, response, error) -> Void in

self?.delegate?.postViewController(self!, didTouchUpCloseButton: sender)
Expand Down
14 changes: 14 additions & 0 deletions chii/web-server/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@
$username = isset($data['username']) ? $data['username'] : '';
$body = isset($data['body']) ? $data['body'] : '';

$botname = "bot";

$createdMessage = $app->createMessage($username, $body, base64_encode(file_get_contents($app['icon_image_path'])));

$uranai = function () use ($body) {
if ($body === "uranai") {
$result = ["daikichi", "kichi", "kyo"];
$body = $result[rand(0,2)];
return $body;
} else {
return $body;
}
};

$createdMessage = $app->createMessage($botname, $uranai(), base64_encode(file_get_contents($app['icon_image_path'])));

return $app->json($createdMessage);
});

Expand Down