@@ -7,17 +7,15 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow
77import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets
88import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
99import com.google.api.client.http.HttpTransport
10- import com.google.api.client.json.jackson2.JacksonFactory
10+ import com.google.api.client.json.gson.GsonFactory
1111import com.google.api.client.util.store.FileDataStoreFactory
1212import com.google.api.services.sheets.v4.Sheets
1313import com.google.api.services.sheets.v4.SheetsScopes
1414import java.io.*
1515import java.time.LocalDate
1616import java.time.format.DateTimeFormatter
17- import java.util.*
1817import java.util.logging.Level
1918import java.util.logging.Logger
20- import kotlin.collections.ArrayList
2119
2220
2321const val lectureFunKey = " Увлекательность подачи материала"
@@ -72,19 +70,17 @@ object SurveyData {
7270 * Update data from server
7371 */
7472 fun update (callback : UpdateCallback ) {
75- synchronized(this ) {
76- callback.notifyUpdateInProgress(true )
77- try {
78- callback.notifyUpdateMessage(" Загрузка данных с сервера" )
79- val newEntries = Connection .download(entries.size)
80- entries.addAll(newEntries)
81- callback.notifyUpdateMessage(" Загружено ${newEntries.size} новых записи с сервера" )
82- if (newEntries.isNotEmpty()) {
83- save()
84- }
85- } finally {
86- callback.notifyUpdateInProgress(false )
73+ callback.notifyUpdateInProgress(true )
74+ try {
75+ callback.notifyUpdateMessage(" Загрузка данных с сервера" )
76+ val newEntries = Connection .download(entries.size)
77+ entries.addAll(newEntries)
78+ callback.notifyUpdateMessage(" Загружено ${newEntries.size} новых записи с сервера" )
79+ if (newEntries.isNotEmpty()) {
80+ save()
8781 }
82+ } finally {
83+ callback.notifyUpdateInProgress(false )
8884 }
8985 }
9086
@@ -120,7 +116,7 @@ object SurveyData {
120116
121117 private fun save () {
122118 synchronized(this ) {
123- if (! entries.isEmpty ()) {
119+ if (entries.isNotEmpty ()) {
124120 val file = File (STORE_FILE_NAME )
125121 try {
126122 ObjectOutputStream (file.outputStream()).use {
@@ -155,7 +151,7 @@ private object Connection {
155151 private val DATA_STORE_FACTORY : FileDataStoreFactory = FileDataStoreFactory (DATA_STORE_DIR )
156152
157153 /* * Global instance of the JSON factory. */
158- private val JSON_FACTORY = JacksonFactory .getDefaultInstance()
154+ private val JSON_FACTORY = GsonFactory .getDefaultInstance()
159155
160156 /* * Global instance of the HTTP transport. */
161157 private val HTTP_TRANSPORT : HttpTransport = GoogleNetHttpTransport .newTrustedTransport()
@@ -165,7 +161,7 @@ private object Connection {
165161 * If modifying these scopes, delete your previously saved credentials
166162 * at ~/.credentials/sheets.googleapis.com-java-quickstart
167163 */
168- private val SCOPES = Arrays .asList (SheetsScopes .SPREADSHEETS_READONLY )
164+ private val SCOPES = listOf (SheetsScopes .SPREADSHEETS_READONLY )
169165
170166 /* *
171167 * Build and return an authorized Sheets API client service.
@@ -187,7 +183,7 @@ private object Connection {
187183 @Throws(IOException ::class )
188184 private fun authorize (): Credential {
189185 // Load client secrets.
190- val input = Connection ::class .java.getResourceAsStream(" /client_secret.json" )
186+ val input = Connection ::class .java.getResourceAsStream(" /client_secret.json" )!!
191187 val clientSecrets = GoogleClientSecrets .load(JSON_FACTORY , InputStreamReader (input))
192188
193189 // Build flow and trigger user authorization request.
@@ -196,6 +192,7 @@ private object Connection {
196192 ).setDataStoreFactory(DATA_STORE_FACTORY )
197193 .setAccessType(" offline" )
198194 .build()
195+
199196 val credential = AuthorizationCodeInstalledApp (
200197 flow, LocalServerReceiver ()
201198 ).authorize(" user" )
@@ -252,7 +249,7 @@ private object Connection {
252249 }
253250 }
254251
255- fun reset (){
252+ fun reset () {
256253 DATA_STORE_DIR .takeIf { it.exists() }?.delete()
257254 }
258255}
0 commit comments