From 5a79705fa5eb8c1ee89f1bf97e74489d0f3a8383 Mon Sep 17 00:00:00 2001 From: hua <1029559041@qq.com> Date: Thu, 20 Aug 2020 13:06:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=AA=E4=BA=BA=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Weibo.xcodeproj/project.pbxproj | 24 +++ .../xcdebugger/Breakpoints_v2.xcbkptlist | 26 +-- Weibo/AppDelegate.swift | 1 - .../arrow-right.imageset/Contents.json | 21 +++ .../arrow-right.imageset/arrow-right.png | Bin 0 -> 248 bytes .../crown.imageset/Contents.json | 21 +++ .../Assets.xcassets/crown.imageset/crown.png | Bin 0 -> 590 bytes .../earth.imageset/Contents.json | 21 +++ .../Assets.xcassets/earth.imageset/earth.png | Bin 0 -> 1378 bytes .../location.imageset/Contents.json | 21 +++ .../location.imageset/location.png | Bin 0 -> 1033 bytes Weibo/ContentView.swift | 36 ++-- .../GrowinTextView/GrowingTextInputView.swift | 64 +++++++ Weibo/GrowinTextView/TextViewWrapper.swift | 72 ++++++++ Weibo/IndexView.swift | 50 ++++-- Weibo/Info.plist | 34 ++-- Weibo/LoginView.swift | 84 +++++++-- Weibo/SendView.swift | 162 ++++++++++++++++++ Weibo/TextView.swift | 48 ++++++ .../Weibo.xcdatamodel/contents | 8 +- 20 files changed, 616 insertions(+), 77 deletions(-) create mode 100644 Weibo/Assets.xcassets/arrow-right.imageset/Contents.json create mode 100644 Weibo/Assets.xcassets/arrow-right.imageset/arrow-right.png create mode 100644 Weibo/Assets.xcassets/crown.imageset/Contents.json create mode 100644 Weibo/Assets.xcassets/crown.imageset/crown.png create mode 100644 Weibo/Assets.xcassets/earth.imageset/Contents.json create mode 100644 Weibo/Assets.xcassets/earth.imageset/earth.png create mode 100644 Weibo/Assets.xcassets/location.imageset/Contents.json create mode 100644 Weibo/Assets.xcassets/location.imageset/location.png create mode 100644 Weibo/GrowinTextView/GrowingTextInputView.swift create mode 100644 Weibo/GrowinTextView/TextViewWrapper.swift create mode 100644 Weibo/SendView.swift create mode 100644 Weibo/TextView.swift diff --git a/Weibo.xcodeproj/project.pbxproj b/Weibo.xcodeproj/project.pbxproj index 3601a33..2c3e26a 100644 --- a/Weibo.xcodeproj/project.pbxproj +++ b/Weibo.xcodeproj/project.pbxproj @@ -8,6 +8,10 @@ /* Begin PBXBuildFile section */ 43F5631C74EF810F4F999A66 /* Pods_WeiboTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 11A13FDA013FB0DA2E12E48F /* Pods_WeiboTests.framework */; }; + 7153B8FC24EDCB0D00D9C7E3 /* SendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7153B8FB24EDCB0D00D9C7E3 /* SendView.swift */; }; + 7153B90024EDDFCE00D9C7E3 /* TextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7153B8FF24EDDFCE00D9C7E3 /* TextView.swift */; }; + 7153B90624EDF5B200D9C7E3 /* GrowingTextInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7153B90424EDF5B200D9C7E3 /* GrowingTextInputView.swift */; }; + 7153B90724EDF5B200D9C7E3 /* TextViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7153B90524EDF5B200D9C7E3 /* TextViewWrapper.swift */; }; 716E9F0224E96288002CC6B8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 716E9F0124E96288002CC6B8 /* AppDelegate.swift */; }; 716E9F0424E96288002CC6B8 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 716E9F0324E96288002CC6B8 /* SceneDelegate.swift */; }; 716E9F0724E96288002CC6B8 /* Weibo.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 716E9F0524E96288002CC6B8 /* Weibo.xcdatamodeld */; }; @@ -44,6 +48,10 @@ 11A13FDA013FB0DA2E12E48F /* Pods_WeiboTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WeiboTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 27B0BA649924BF20ED711CE6 /* Pods-Weibo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Weibo.debug.xcconfig"; path = "Target Support Files/Pods-Weibo/Pods-Weibo.debug.xcconfig"; sourceTree = ""; }; 6808829B1FA2E4D6EA25A6AA /* Pods-WeiboTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WeiboTests.release.xcconfig"; path = "Target Support Files/Pods-WeiboTests/Pods-WeiboTests.release.xcconfig"; sourceTree = ""; }; + 7153B8FB24EDCB0D00D9C7E3 /* SendView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendView.swift; sourceTree = ""; }; + 7153B8FF24EDDFCE00D9C7E3 /* TextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextView.swift; sourceTree = ""; }; + 7153B90424EDF5B200D9C7E3 /* GrowingTextInputView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GrowingTextInputView.swift; sourceTree = ""; }; + 7153B90524EDF5B200D9C7E3 /* TextViewWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextViewWrapper.swift; sourceTree = ""; }; 716E9EFE24E96288002CC6B8 /* Weibo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Weibo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 716E9F0124E96288002CC6B8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 716E9F0324E96288002CC6B8 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -115,6 +123,15 @@ path = Pods; sourceTree = ""; }; + 7153B90324EDF5B200D9C7E3 /* GrowinTextView */ = { + isa = PBXGroup; + children = ( + 7153B90424EDF5B200D9C7E3 /* GrowingTextInputView.swift */, + 7153B90524EDF5B200D9C7E3 /* TextViewWrapper.swift */, + ); + path = GrowinTextView; + sourceTree = ""; + }; 716E9EF524E96288002CC6B8 = { isa = PBXGroup; children = ( @@ -140,6 +157,8 @@ 716E9F0024E96288002CC6B8 /* Weibo */ = { isa = PBXGroup; children = ( + 7153B90324EDF5B200D9C7E3 /* GrowinTextView */, + 7153B8FF24EDDFCE00D9C7E3 /* TextView.swift */, 716E9F0124E96288002CC6B8 /* AppDelegate.swift */, 716E9F0324E96288002CC6B8 /* SceneDelegate.swift */, 716E9F0824E96288002CC6B8 /* ContentView.swift */, @@ -152,6 +171,7 @@ 716E9F3B24EABF1D002CC6B8 /* Weibo-Brigding-Header.h */, 716E9F4124EB077E002CC6B8 /* UserData.swift */, 716E9F4324EB1E04002CC6B8 /* IndexView.swift */, + 7153B8FB24EDCB0D00D9C7E3 /* SendView.swift */, ); path = Weibo; sourceTree = ""; @@ -436,10 +456,14 @@ files = ( 716E9F4424EB1E04002CC6B8 /* IndexView.swift in Sources */, 716E9F4224EB077E002CC6B8 /* UserData.swift in Sources */, + 7153B90724EDF5B200D9C7E3 /* TextViewWrapper.swift in Sources */, 716E9F0224E96288002CC6B8 /* AppDelegate.swift in Sources */, 716E9F0424E96288002CC6B8 /* SceneDelegate.swift in Sources */, 716E9F0924E96288002CC6B8 /* ContentView.swift in Sources */, 716E9F3924EA9175002CC6B8 /* LoginView.swift in Sources */, + 7153B90624EDF5B200D9C7E3 /* GrowingTextInputView.swift in Sources */, + 7153B90024EDDFCE00D9C7E3 /* TextView.swift in Sources */, + 7153B8FC24EDCB0D00D9C7E3 /* SendView.swift in Sources */, 716E9F0724E96288002CC6B8 /* Weibo.xcdatamodeld in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Weibo.xcworkspace/xcuserdata/qihuapan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Weibo.xcworkspace/xcuserdata/qihuapan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 12e463e..6524fd9 100644 --- a/Weibo.xcworkspace/xcuserdata/qihuapan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Weibo.xcworkspace/xcuserdata/qihuapan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -7,32 +7,16 @@ - - - - diff --git a/Weibo/AppDelegate.swift b/Weibo/AppDelegate.swift index 4866f4e..da920e7 100644 --- a/Weibo/AppDelegate.swift +++ b/Weibo/AppDelegate.swift @@ -24,7 +24,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate,WeiboSDKDelegate,WBHttpReq // if response is WBAuthorizeResponse{ if let res=response as? WBAuthorizeResponse{ -// let message = "响应状态: \(res.statusCode)\nresponse.accessToken: \(res.accessToken)\n响应UserInfo数据: \(res.userInfo)\n原请求UserInfo数据: \(res.requestUserInfo)\nuserID:\(res.userID)" // print(message) let user = UserData.getUser() user.access_token=res.accessToken diff --git a/Weibo/Assets.xcassets/arrow-right.imageset/Contents.json b/Weibo/Assets.xcassets/arrow-right.imageset/Contents.json new file mode 100644 index 0000000..06d6f07 --- /dev/null +++ b/Weibo/Assets.xcassets/arrow-right.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "arrow-right.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Weibo/Assets.xcassets/arrow-right.imageset/arrow-right.png b/Weibo/Assets.xcassets/arrow-right.imageset/arrow-right.png new file mode 100644 index 0000000000000000000000000000000000000000..03640dd35294968c5b535c2c083afdfddd3dc07a GIT binary patch literal 248 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=ffJ{hlt4ArbCxuN~xV3J_>{INwi) zRqF!Z7Gup$Aq_`sgV_}iTCDTi_slpTIisI5e>3yk^$Uyb)E~T;+QG2;^Zr1o8I9To zjNRN5S#27gn}2!glEOGqmiNwG4%Pw=C)OK+x(wQjL=&7t7{pve1KcbaRF4QYm>Z-m zU&_2eHXx`XB%F21q16pV6|4!I`K&h%MKfG05KZ6@XSm(!VwY8YdvX!a;>OuVDMs0| vnL56g#(EjctK45~rojHE>H$kh{sZQHS8Np11C}}gy~5z>>gTe~DWM4foL5$+ literal 0 HcmV?d00001 diff --git a/Weibo/Assets.xcassets/crown.imageset/Contents.json b/Weibo/Assets.xcassets/crown.imageset/Contents.json new file mode 100644 index 0000000..defbcba --- /dev/null +++ b/Weibo/Assets.xcassets/crown.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "crown.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Weibo/Assets.xcassets/crown.imageset/crown.png b/Weibo/Assets.xcassets/crown.imageset/crown.png new file mode 100644 index 0000000000000000000000000000000000000000..19a0fe9278d4942db0f11f6c93db782ed56aed62 GIT binary patch literal 590 zcmV-U0-NU(%`l@s1db$~8IhYnCR%ZB zhmdRt0KwV>0YXtQIPdMHKnfp2fFu^nY9Ki>(HfQw;HW|#-}M>INs`Yw@;S$n;yb+N z@xr=5wnb%t3W=A@)>aA!CDPYL$W=FNDfQ)gR`YFW}N3#))y25 zX@JAU&NQZ#Ca~7$yHeO)RZ$-y8Vf&XZ7bumFw;JPx z2{8h`7cgScTTX-liw6Ua%|~A;F#^ftA4uW5rc+ev#|Wpx7&U4H)5HGoSGZmUc-~a#s literal 0 HcmV?d00001 diff --git a/Weibo/Assets.xcassets/earth.imageset/Contents.json b/Weibo/Assets.xcassets/earth.imageset/Contents.json new file mode 100644 index 0000000..e2096a5 --- /dev/null +++ b/Weibo/Assets.xcassets/earth.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "earth.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Weibo/Assets.xcassets/earth.imageset/earth.png b/Weibo/Assets.xcassets/earth.imageset/earth.png new file mode 100644 index 0000000000000000000000000000000000000000..29ff69f04f31f8629b64316d937cd7fa61615c17 GIT binary patch literal 1378 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE3?yBabR7dyEa{HEjtmSN`?>!lvI6;x#X;^) z4C~IxyacIC_6YK2V5m}KU}$JzVE6?TYIwoGP-?)y@G60U!DxXO(sh@w(_XkO?tD7!knLq8=A0O_ek6s3seb#0YKF}iZpn9$O&e`7# z`y!t;c)8Z{pW3UW^5o;f_d>6hPf}YGF!|{|Kfy~@g;OkH}&m4PtvRxShEak-ae8fV zrnPjSh@JB32^|qmflg{6OQv3n6Jp_wlB%A4DeKJ=)r`!oQQlkc&A(fnZ&aRUV`nA* zY-ah+^xeiT-zB^n&%P_L3aw7OICs-&Udg_sr`z6XK5V+aa@wLbdze0iMtxny9G1jnlChSw*11Q!bv-(Hf}iiQ1JQ3AD+=qRw~TfTFvMi7FbcMZ)Nqg zbKN^Rx390}?z-@2Pk+@fU(LlE8J|gM*NC{R-ZQx&(Stjw)W;w@tXcZCi?pTM9?cbJk6hapn8d!@Wv2h33tuAh zO|ncPJ9NL@5pJus%$jzX`D0YeTjxb?eoNe!9z6FkFi(qppFnNuj~_=OwjC=l+t!}cTNO(vw^@BewzBL8yl5yO1vj=Q2KcTSP& zU%j$fdDr!lvI6;x#X;^) z4C~IxyacIC_6YK2V5m}KU}$JzVE6?TYIwoGP-?)y@G60U!D?s&SN}Z zzS;VPu$Wxzp6D+1_S}ZMZ;rJ6+kW8deCDJ(=_d#UGd}`a#+c;o?!wT)D(eB{a29w(76WMz+k?UFWVRhp z0k5ZvV~EA+qP-BAM5eB0ZpEea4Bv!Tri%t;Ha5}0oWr}1-$U+%Y71mb) zhP(pq!Q6)~x&i;qn+-nFM&dli$h zc}QhB{}-0WnLRr1pZ3mNQd1-NNvQ6I(9(JaA3K8%Ms1F3Z714Wj2miQ)+|Q;j?Uih^aI>NCK4<78H6;t|60cKg#bfHwZt`|BqgyV)hf9t6-Y4{85kMp z8d&HW8H5;^S(zGJ8JK7rSXdbt+^W~VfTAHcKP5A*61RrcO4DXsH|GNfhZq+03g>u2QWrs^lA89o9mFSn`7Jzy2S*htI=}Co|RRx9m fIr+(nC7JnodO&kp_xmgd8q476>gTe~DWM4fN;m}0 literal 0 HcmV?d00001 diff --git a/Weibo/ContentView.swift b/Weibo/ContentView.swift index bb57cd1..c6fd58d 100644 --- a/Weibo/ContentView.swift +++ b/Weibo/ContentView.swift @@ -8,23 +8,39 @@ import SwiftUI +import CoreData struct ContentView: View { - @State var selection=1 + @Environment(\.managedObjectContext) var contenxt:NSManagedObjectContext + + @EnvironmentObject var user:User + + @State var selection=2 + + - var body: some View { TabView(selection: self.$selection) { - IndexView().tabItem { VStack { - Image("house") - Text("首页") - .foregroundColor(Color.black) - } }.tag(1) + if self.user.access_token==nil{ + IndexView().tabItem { VStack { + Image("house") + Text("首页") + .foregroundColor(Color.black) + } }.tag(1).hidden() + }else{ + IndexView().tabItem { VStack { + Image("house") + Text("首页") + .foregroundColor(Color.black) + } }.tag(1) + } + LoginView().tabItem { VStack { - Image("user") - Text("我") - } }.tag(2) + Image("user") + Text("我") + } }.tag(2) + } } } diff --git a/Weibo/GrowinTextView/GrowingTextInputView.swift b/Weibo/GrowinTextView/GrowingTextInputView.swift new file mode 100644 index 0000000..ba5d6ac --- /dev/null +++ b/Weibo/GrowinTextView/GrowingTextInputView.swift @@ -0,0 +1,64 @@ +import SwiftUI + +struct GrowingTextInputView: View { + init(text: Binding, placeholder: String) { + self._text = text + self.placeholder = placeholder + } + + @Binding var text: String? + @State var focused: Bool = false + @State var contentHeight: CGFloat = 0 + + let placeholder: String? + let minHeight: CGFloat = 39 + let maxHeight: CGFloat = 150 + + var countedHeight: CGFloat { + min(max(minHeight, contentHeight), maxHeight) + } + + var body: some View { + ZStack(alignment: .topLeading) { + Color.white + ZStack(alignment: .topLeading) { + placeholderView + TextViewWrapper(text: $text, focused: $focused, contentHeight: $contentHeight) + }.padding(.horizontal, 4) + }.frame(height: countedHeight) + } + + var placeholderView: some View { + ViewBuilder.buildIf( + showPlaceholder ? + placeholder.map { + Text($0) + .foregroundColor(.gray) + .font(.system(size: 16)) + .padding(.vertical, 8) + .padding(.horizontal, 4) + } : nil + ) + } + + var showPlaceholder: Bool { + if let text=self.text{ + return !focused&&text.isEmpty + }else{ + return false + } + } +} + +#if DEBUG +struct GrowingTextInputView_Previews: PreviewProvider { + @State static var text: String? + + static var previews: some View { + GrowingTextInputView( + text: $text, + placeholder: "Placeholder" + ) + } +} +#endif diff --git a/Weibo/GrowinTextView/TextViewWrapper.swift b/Weibo/GrowinTextView/TextViewWrapper.swift new file mode 100644 index 0000000..9bb06eb --- /dev/null +++ b/Weibo/GrowinTextView/TextViewWrapper.swift @@ -0,0 +1,72 @@ +import SwiftUI + +struct TextViewWrapper: UIViewRepresentable { + + init(text: Binding, focused: Binding, contentHeight: Binding) { + self._text = text + self._focused = focused + self._contentHeight = contentHeight + } + + @Binding var text: String? + @Binding var focused: Bool + @Binding var contentHeight: CGFloat + + // MARK: - UIViewRepresentable + + func makeUIView(context: Context) -> UITextView { + let textView = UITextView() + textView.delegate = context.coordinator + textView.font = .systemFont(ofSize: 16) + textView.backgroundColor = .clear + textView.autocorrectionType = .no + return textView + } + + func makeCoordinator() -> Coordinator { + Coordinator(text: $text, focused: $focused, contentHeight: $contentHeight) + } + + func updateUIView(_ uiView: UITextView, context: Context) { + uiView.text = text + } + + class Coordinator: NSObject, UITextViewDelegate { + + init(text: Binding, focused: Binding, contentHeight: Binding) { + self._text = text + self._focused = focused + self._contentHeight = contentHeight + } + + @Binding private var text: String? + @Binding private var focused: Bool + @Binding private var contentHeight: CGFloat + + // MARK: - UITextViewDelegate + + func textViewDidChange(_ textView: UITextView) { + text = textView.text + contentHeight = textView.contentSize.height + } + + func textViewDidBeginEditing(_ textView: UITextView) { + focused = true + } + + func textViewDidEndEditing(_ textView: UITextView) { + focused = false + contentHeight = text == nil ? 0 : textView.contentSize.height + } + } +} + +#if DEBUG +struct TextViewWrapper_Previews: PreviewProvider { + @State static var text:String? + + static var previews: some View { + TextViewWrapper(text: $text, focused: .constant(false), contentHeight: .constant(0)) + } +} +#endif diff --git a/Weibo/IndexView.swift b/Weibo/IndexView.swift index 62f1ed0..6dd49c5 100644 --- a/Weibo/IndexView.swift +++ b/Weibo/IndexView.swift @@ -57,9 +57,11 @@ struct TopView:View { @Binding var icon: String - @State var activeMenu:String="全部关注" + @State var activeMenu:String="最新微博" @State var menuIndex=0 + + @Binding var isSend:Bool var body: some View{ VStack { @@ -98,7 +100,9 @@ struct TopView:View { Spacer() HStack(){ Image("red packet") - Image("edit") + Image("edit").onTapGesture(perform: { + self.isSend=true + }) } .padding(.trailing, 18.0) .frame(width: nil) @@ -336,6 +340,10 @@ struct IndexView: View { @State var angle:Double=0 + @State var isSend=false + + @State var showTip=true + func tansformDate(dateStr:String)->String{ let formatter = DateFormatter() @@ -347,11 +355,14 @@ struct IndexView: View { let date = Date(timeIntervalSince1970: unix!) return formatter.string(from: date) } + func loadContent(){ + self.showTip=true if let access_token=self.user.access_token{ print("加载用户\(String(describing: self.user.uid))关注用户的最新微博") AF.request("https://api.weibo.com/2/statuses/friends_timeline.json",parameters: ["access_token":access_token]).responseJSON { response in + self.showTip=false if let data=response.data,let json = try? JSON(data: data){ if let statuses = json["statuses"].array { self.statuses.removeAll() @@ -376,36 +387,45 @@ struct IndexView: View { } print("解析\(self.statuses.count)条微博数据") }else{ - print("loadUserInfo error") + print("loadUserInfo error:\(response.error)") } } } - }else{ - print("加载公共微博") } } + var body: some View { + + VStack { - TopView(icon: $icon).onAppear(perform: { - self.loadContent() - }) - if self.statuses.count>0{ + if self.isSend{ + SendView(flag: { + self.isSend=false + }) + }else{ + TopView(icon: $icon, isSend: self.$isSend).onAppear(perform: { + self.loadContent() + }) + if self.showTip{ + Spacer() + LoadView() + Spacer() + + } List(self.statuses){ data in RowView(content: data) - } - }else{ - Spacer() - LoadView() - Spacer() + }.onAppear(perform: { + self.showTip=false + }) + } - } } } diff --git a/Weibo/Info.plist b/Weibo/Info.plist index e942f4a..056654c 100644 --- a/Weibo/Info.plist +++ b/Weibo/Info.plist @@ -2,22 +2,6 @@ - appkey - 568392886 - NSAppTransportSecurity - - NSAllowsArbitraryLoads - - - LSApplicationQueriesSchemes - - sinaweibo - sinaweibohd - sinaweibosso - sinaweibohdsso - weibosdk - weibosdk2.5 - CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -41,14 +25,28 @@ CFBundleURLSchemes - wb568392886 + wb1946444944 CFBundleVersion 1 + LSApplicationQueriesSchemes + + sinaweibo + sinaweibohd + sinaweibosso + sinaweibohdsso + weibosdk + weibosdk2.5 + LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UIApplicationSceneManifest UIApplicationSupportsMultipleScenes @@ -95,5 +93,7 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + appkey + 1946444944 diff --git a/Weibo/LoginView.swift b/Weibo/LoginView.swift index af0b847..db6af72 100644 --- a/Weibo/LoginView.swift +++ b/Weibo/LoginView.swift @@ -10,9 +10,6 @@ import SwiftUI import CoreData import SwiftyJSON import Alamofire -struct UserProfile { - var name:String? -} struct LoginView: View { @@ -20,10 +17,9 @@ struct LoginView: View { @EnvironmentObject var user:User - @State var userInfo=UserProfile() - let queue=OperationQueue() + @State var showView=false func login() { let request=WBAuthorizeRequest() @@ -33,10 +29,25 @@ struct LoginView: View { func loadUserInfo(){ print("toekn=\(self.user.access_token),date=\(self.user.expiration_Date)") - + self.showView=false AF.request("https://api.weibo.com/2/users/show.json",parameters: ["access_token":self.user.access_token,"uid":self.user.uid]).responseJSON { response in + if let data=response.data,let json = try? JSON(data: data){ - print(json) + if let name=json["name"].string, + let avatar_hd=json["avatar_hd"].string, + let desc=json["description"].string, + let statuses_count=json["statuses_count"].int32, + let friends_count=json["friends_count"].int32, + let followers_count=json["followers_count"].int32{ + self.user.name=name + self.user.avatar_hd=avatar_hd + self.user.desc=desc + self.user.statuses_count=statuses_count + self.user.friends_count=friends_count + self.user.followers_count=followers_count + UserData.saveContext() + self.showView=true + } }else{ print("loadUserInfo error") } @@ -57,22 +68,71 @@ struct LoginView: View { self.login() }) } - else if self.userInfo.name==nil{ + else if self.showView{ + UserView() + }else{ Text("正在获取用户信息").onAppear(perform: { - self.loadUserInfo() + self.loadUserInfo() }) - }else{ - Text("username=\(self.userInfo.name!)") } } } } +struct UserView:View { + @EnvironmentObject var user:User + + var body:some View{ + VStack { + HStack { + ImageView(imageUrl: self.user.avatar_hd!) + .frame(width: 60.0, height: 60.0) + .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/) + VStack { + Text(self.user.name!) + Text("简介:\(self.user.desc!)") + } + Spacer() + Button(action: { + print("会员") + }) { + HStack(spacing:0) { + Image("crown") + Text("会员") + Image("arrow-right") + } + } + } + Rectangle() + .frame(height: 1) + HStack(spacing: 100.0){ + VStack{ + Text("\(self.user.statuses_count)") + Text("微博") + } + VStack{ + Text("\(self.user.friends_count)") + Text("关注") + } + VStack{ + Text("\(self.user.followers_count)") + Text("粉丝") + } + } + .padding(.horizontal, 20.0) + Rectangle() + .frame(height: 10) + .foregroundColor(Color(red: 220/255, green: 220/255, blue: 220/255, opacity: 1.0)) + } + } +} + struct LoginView_Previews: PreviewProvider { static var previews: some View { - LoginView().environment(\.managedObjectContext, UserData.getContext()).environmentObject(UserData.getUser()) +// LoginView().environment(\.managedObjectContext, UserData.getContext()).environmentObject(UserData.getUser()) + UserView() } } diff --git a/Weibo/SendView.swift b/Weibo/SendView.swift new file mode 100644 index 0000000..ccb09f6 --- /dev/null +++ b/Weibo/SendView.swift @@ -0,0 +1,162 @@ +// +// SendView.swift +// Weibo +// +// Created by Qihua Pan on 2020/8/20. +// Copyright © 2020 Qihua Pan. All rights reserved. +// + +import SwiftUI +import CoreData + +import Alamofire +import SwiftyJSON +struct SendView: View { + + @Environment(\.managedObjectContext) var contenxt:NSManagedObjectContext + + @EnvironmentObject var user:User + + var flag={} + + @State var text:String?="" + @State private var textStyle = UIFont.TextStyle.body + + //全局记录高度的值 +// @State var value:CGFloat = 0 + + let domain="https://sukura.com" + + @State var showAlert=false + + @State var showTip=false + + var body: some View { + VStack { + if self.showTip{ + Spacer() + LoadView() + Spacer() + } + else{ + HStack { + Button(action: { + self.flag() + + // self.alert(isPresented: self.$showAlert, content: { + // Alert(title: Text("fuck"),primaryButton: .destructive(Text("确认")) { print("转出中...") }, secondaryButton: .cancel()) + // + // }).navigationBarTitle(Text("Alert")) + }) { + Text("取消") + .foregroundColor(Color.black) + } + Spacer() + VStack { + Text("发微博") + // Text(self.$user.name).foregroundColor(Color.gray) + Text("用户7450769290") + .foregroundColor(Color.gray) + } + Spacer() + + Button(action: { + print("send") + if let access_token=self.user.access_token,let text=self.text{ + self.showTip=true + + AF.request("https://api.weibo.com/2/statuses/share.json",method: .post,parameters: ["access_token":access_token,"status":"\(text):\(self.domain)"]).responseJSON { response in + self.showTip=false + if let data=response.data,let json = try? JSON(data: data){ + print("分享微博信息返回\(json)") + self.flag() + }else{ + print("发送微博失败") + self.alert(isPresented: self.$showAlert, content: { + Alert(title: Text("错误提示").foregroundColor(Color.red),message: Text("发送微博失败,请联系管理员")) + }) + } + } + } + }){ + Text("发送") + .foregroundColor(Color.gray) + } + .padding(.all, 5.0) + .frame(width: 50.0) + .background(/*@START_MENU_TOKEN@*/Color.orange/*@END_MENU_TOKEN@*/) + .cornerRadius(30) + + } + .padding(.horizontal) + .background(Color(red: 229/255, green: 230/255, blue: 229/255, opacity: 1.0)) + + + // MultiLineTextView(text: self.$text, textStyle: self.$textStyle) + GrowingTextInputView(text: $text, placeholder: "分享新鲜事...") + HStack { + + Button(action: { + + }) { + HStack(spacing: 0.0) { + Image("location").foregroundColor(Color(red: 148/255, green: 148/255, blue: 148/255, opacity: 1.0)) + Text("你在哪里?").foregroundColor(Color.gray) + } + } + .padding(.all, 10.0) + // .background(Color.red) + .background(Color(red: 228/255, green: 228/255, blue: 228/255, opacity: 1.0)) + .cornerRadius(20) + Spacer() + if self.text!.count>0{ + Text("\(self.text!.count)").font(.title) + } + + Button(action: { + + }) { + HStack(spacing: 0.0) { + Image("earth") + Text("公开") + } + }.padding(.all, 10.0) + // .background(Color.red) + .background(Color(red: 228/255, green: 228/255, blue: 228/255, opacity: 1.0)) + + .cornerRadius(/*@START_MENU_TOKEN@*/20.0/*@END_MENU_TOKEN@*/) + } + .padding(.horizontal) + Spacer() + } +// .offset(y : -value) +// .animation(.spring()).onAppear(perform: { + //键盘抬起 + // NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: OperationQueue.current) { (noti) in + // let value = noti.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect + // let height = value.height + // self.value = height - UIApplication.shared.windows.first!.safeAreaInsets.bottom + // } + // //键盘收起 + // NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: OperationQueue.current) { (noti) in + // self.value = 0 + // } +// }) + + } + + + + + } +} + +class KeyBoardView:UITextField{ + +} + +struct SendView_Previews: PreviewProvider { + static var previews: some View { + SendView() + } +} diff --git a/Weibo/TextView.swift b/Weibo/TextView.swift new file mode 100644 index 0000000..4d1e47d --- /dev/null +++ b/Weibo/TextView.swift @@ -0,0 +1,48 @@ +// +// TextView.swift +// SwiftUITextViewDemo +// +// Created by Simon Ng on 7/5/2020. +// Copyright © 2020 AppCoda. All rights reserved. +// + +import SwiftUI + +struct MultiLineTextView: UIViewRepresentable { + + @Binding var text: String + @Binding var textStyle: UIFont.TextStyle + + func makeUIView(context: Context) -> UITextView { + let textView = UITextView() + + textView.delegate = context.coordinator + textView.font = UIFont.preferredFont(forTextStyle: textStyle) + textView.autocapitalizationType = .sentences + textView.isSelectable = true + textView.isUserInteractionEnabled = true + + return textView + } + + func updateUIView(_ uiView: UITextView, context: Context) { + uiView.text = text + uiView.font = UIFont.preferredFont(forTextStyle: textStyle) + } + + func makeCoordinator() -> Coordinator { + Coordinator($text) + } + + class Coordinator: NSObject, UITextViewDelegate { + var text: Binding + + init(_ text: Binding) { + self.text = text + } + + func textViewDidChange(_ textView: UITextView) { + self.text.wrappedValue = textView.text + } + } +} diff --git a/Weibo/Weibo.xcdatamodeld/Weibo.xcdatamodel/contents b/Weibo/Weibo.xcdatamodeld/Weibo.xcdatamodel/contents index 6a83534..3d7a67e 100644 --- a/Weibo/Weibo.xcdatamodeld/Weibo.xcdatamodel/contents +++ b/Weibo/Weibo.xcdatamodeld/Weibo.xcdatamodel/contents @@ -2,11 +2,17 @@ + + + + + + - + \ No newline at end of file