Procházet zdrojové kódy

Skip writing zero progress to notes

sirekanyan před 8 roky
rodič
revize
eb4b3ceb4a

+ 1 - 0
app/build.gradle

@@ -35,6 +35,7 @@ dependencies {
     implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
     implementation 'com.github.bumptech.glide:glide:4.5.0'
     implementation(name: 'ulogin-sdk-v1.1', ext: 'aar')
+    testImplementation 'junit:junit:4.12'
 }
 
 clean {

+ 5 - 1
app/src/main/java/me/vadik/knigopis/api/gson/NotesTypeAdapter.kt

@@ -10,7 +10,11 @@ class NotesTypeAdapter : TypeAdapter<Notes>() {
     private val regex = Regex("(.*) // (\\d+)%")
 
     override fun write(output: JsonWriter, notes: Notes) {
-        output.value("${notes.text} // ${notes.progress}%")
+        if (notes.progress == 0) {
+            output.value(notes.text)
+        } else {
+            output.value("${notes.text} // ${notes.progress}%")
+        }
     }
 
     override fun read(input: JsonReader): Notes {

+ 13 - 0
app/src/test/java/me/vadik/knigopis/api/gson/NotesTypeAdapterTest.kt

@@ -17,10 +17,23 @@ class NotesTypeAdapterTest {
         assertEquals("\"text // 25%\"", gson.toJson(notes))
     }
 
+    @Test
+    fun writeZeroProgress() {
+        val notes = Notes("text", 0)
+        assertEquals("\"text\"", gson.toJson(notes))
+    }
+
     @Test
     fun read() {
         val notes = gson.fromJson("\"text // text // 25%\"", Notes::class.java)
         assertEquals("text // text", notes.text)
         assertEquals(25, notes.progress)
     }
+
+    @Test
+    fun readZeroProgress() {
+        val notes = gson.fromJson("\"text // text\"", Notes::class.java)
+        assertEquals("text // text", notes.text)
+        assertEquals(0, notes.progress)
+    }
 }