aboutsummaryrefslogtreecommitdiff
path: root/Qrious/ContentView.swift
blob: 2af0f96a8a627103fa9725fbe9229a856fae4c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
//  ContentView.swift
//  Qrious
//
//  Created by Navan Chauhan on 22/07/20.
//  Copyright © 2020 Navan Chauhan. All rights reserved.
//

import SwiftUI

struct Todo: Codable, Identifiable {
    public var id: Int
    public var title: String
    public var published_date: String
    public var defined_type_name: String
    public var url: String
    public var description1: String?
    public var paperURL: String?
    public var candidates: String?
    
}


// group=13668 ( Chemrxiv )
class FetchToDo: ObservableObject {
    @Published var query: String = ""
    @Published var todos = [Todo]()
     
    init() {
        //var request = URLRequest(url: URL(string: "https://api.figshare.com/v2/articles/search?search_for=\(q)&page_size=\(p)")!)
        var request = URLRequest(url: URL(string: "https://api.figshare.com/v2/articles/search?page_size=20")!)
        request.httpMethod = "POST"
        
        //let url = URL(string: "https://jsonplaceholder.typicode.com/todos")!
        URLSession.shared.dataTask(with: request) {(data, response, error) in
            do {
                if let todoData = data {
                    let decodedData = try JSONDecoder().decode([Todo].self, from: todoData)
                    DispatchQueue.main.async {
                        self.todos = decodedData
                    }
                } else {
                    print("No data")
                }
            } catch {
                print("Error")
            }
        }.resume()
    }
    func fetchAgain(q: String = "Hepatitis+B+Virus+Silico",p: String = "10",limit: Bool = false) {
        var s = "https://api.figshare.com/v2/articles/search?search_for=\(q.replacingOccurrences(of: " ", with: "+"))&page_size=\(p)"
        //var request = URLRequest(url: URL(string: "https://api.figshare.com/v2/articles/search?search_for=\(q)&page_size=\(p)")!)
        if limit {
            s += "&group=13668"
        }
        print(s, limit)
        var request = URLRequest(url: URL(string: s)!)
        request.httpMethod = "POST"
        
        //let url = URL(string: "https://jsonplaceholder.typicode.com/todos")!
        URLSession.shared.dataTask(with: request) {(data, response, error) in
            do {
                if let todoData = data {
                    let decodedData = try JSONDecoder().decode([Todo].self, from: todoData)
                    DispatchQueue.main.async {
                        self.todos = decodedData
                    }
                } else {
                    print("No data")
                }
            } catch {
                print("Error")
            }
        }.resume()
    }
}

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

class configuration: ObservableObject {
    @Published var question: String = "Results show what are the compounds?"
}

struct ContentView: View {
    @State private var selection = 0
    @State private var advance = true
    @State public var query = "Covid-19 Compounds"
    @State public var noOfArticles = "15"
    @State private var executed = false
    @EnvironmentObject var config: configuration
    @State private var ques: String = configuration().question
    //"Results show what top compounds?"
    @State public var LimitToChemrxiv: Bool = false
    @ObservedObject var fetch = FetchToDo()
    //let bert = BERTQAFP16()
    var body: some View {
        TabView(selection: $selection){
            NavigationView{
                
                VStack{
                    /*
                    HStack{
                        
                    Text("Configuration")
                        .font(.title)
                        .fontWeight(.semibold)
                        .multilineTextAlignment(.leading)
                    
                    }*/
                Form{
                    
                    Section(header: Text("Search Query")){
                            TextField("Search Query",text: $query)
                    }
                    
                    
                    Section(header: Text("Configuration")){
                        VStack{
                        HStack{
                            Text("No. Of Articles:")
                            TextField("No. Of Articles", text: $noOfArticles)
                        .keyboardType(.numberPad)
                        }.disabled(!advance)
                            HStack{
                                Toggle(isOn: $LimitToChemrxiv){
                                    Text("Limit to ChemRxiv")
                                }
                            }
                        }
                    }
                        
                        Section(header: Text("Query for AI Model")){
                            TextField("",text: $ques, onEditingChanged: {_ in self.config.question = self.ques})
                            
                        }
                        
                    
                    Section{
                        Button(action: {self.fetch.fetchAgain(q: self.query, p: String(self.noOfArticles), limit: self.LimitToChemrxiv)}){
                            Text("Get Papers")
                        }
                    }
                    
                }
                    Text("OwO")
                        .frame(height: 50.0)
                }
            .navigationBarTitle(Text("Qrious"))
            }
                .tabItem {
                    VStack {
                        Image(systemName: "gear")
                        .font(.title) //Image("first")
                        Text("Configuration")
                    }
                }
                .tag(0)
            NavigationView{
            //Text(" View").font(.title)
                VStack {
                    List(fetch.todos) { todo in NavigationLink(destination:  DetailedView(article: todo)){
                               VStack(alignment: .leading) {
                                   Text(todo.title)
                                   Text("\(todo.published_date)")
                                       .font(.system(size: 11))
                                       .foregroundColor(Color.gray)
                            }}
                           }
                       }
            .navigationBarTitle("Articles")
            }
                
                .tabItem {
                    VStack {
                         Image(systemName: "list.bullet")
                            .font(.title)
                        Text("Articles")
                    }
                }
                .tag(1)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}