Skip to content

Commit

Permalink
Merge pull request #58 from WTFart/patch/refactor-codes
Browse files Browse the repository at this point in the history
Refactor/Optimize Codes
  • Loading branch information
mickeycj authored Nov 11, 2017
2 parents c9b7003 + f13d85b commit ee2dc04
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 73 deletions.
10 changes: 6 additions & 4 deletions app/src/main/java/com/wtfart/ipaddressmanager/DetailFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ class DetailFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

textview_notation.text = mCidr.notation
textview_netmask.text = IpConverter.toIpAddress(mCidr.netmask)
textview_wildcard_mask.text = IpConverter.toIpAddress(mCidr.wildcardMask)
val (initialIpAddress, lastIpAddress) = mCidr.ipAddressRange
val (notation, netmask, wildcardMask, ipAddressRange) = mCidr
val (initialIpAddress, lastIpAddress) = ipAddressRange

textview_notation.text = notation
textview_netmask.text = IpConverter.toIpAddress(netmask)
textview_wildcard_mask.text = IpConverter.toIpAddress(wildcardMask)
textview_ip_address_range.text = if (initialIpAddress == lastIpAddress) {
IpConverter.toIpAddress(initialIpAddress)
} else {
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/wtfart/ipaddressmanager/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ class MainActivity : AppCompatActivity() {
override fun onBackPressed() {
super.onBackPressed()

if (supportFragmentManager.backStackEntryCount == 0) {
supportActionBar?.setHomeButtonEnabled(false)
supportActionBar?.setDisplayHomeAsUpEnabled(false)
}
setupHomeButton(false)
}

fun requestFocus() {
layout_main_activity.requestFocus()
}

fun switchFragment(fragment: Fragment) {
if (supportFragmentManager.backStackEntryCount == 0) {
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
setupHomeButton(true)
supportFragmentManager
.beginTransaction()
.setCustomAnimations(
Expand All @@ -60,4 +54,11 @@ class MainActivity : AppCompatActivity() {
.addToBackStack(null)
.commit()
}

private fun setupHomeButton(status: Boolean) {
if (supportFragmentManager.backStackEntryCount == 0) {
supportActionBar?.setHomeButtonEnabled(status)
supportActionBar?.setDisplayHomeAsUpEnabled(status)
}
}
}
14 changes: 6 additions & 8 deletions app/src/main/java/com/wtfart/ipaddressmanager/model/Cidr.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.wtfart.ipaddressmanager.model

import com.wtfart.ipaddressmanager.util.IpConverter

import java.io.Serializable
import java.util.regex.Pattern

import com.wtfart.ipaddressmanager.util.IpConverter

/**
* Created by mickeycj on 11/2/2017 AD.
*/
Expand Down Expand Up @@ -137,12 +137,10 @@ data class Cidr(

other as Cidr

if (notation != other.notation) return false
if (netmask != other.netmask) return false
if (wildcardMask != other.wildcardMask) return false
if (ipAddressRange != other.ipAddressRange) return false

return true
return notation == other.notation
|| netmask == other.netmask
|| wildcardMask == other.wildcardMask
|| ipAddressRange == other.ipAddressRange
}

override fun hashCode(): Int {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/anim/slide_in_left.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/anim/slide_in_right.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/anim/slide_out_left.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/anim/slide_out_right.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/drawable-v24/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/drawable/bg_grey_line.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-2dp" android:right="-1dp" android:bottom="-1dp" android:top="1dp">
<item
android:left="-2dp"
android:right="-1dp"
android:bottom="-1dp"
android:top="1dp">
<shape android:shape="rectangle">
<stroke android:color="@color/grey"
<stroke
android:color="@color/grey"
android:width="1dp" />
</shape>
</item>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/drawable/ic_launcher_background.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/res/layout/fragment_calculator.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical"
android:weightSum="1.0">
android:paddingTop="8dp">

<LinearLayout
android:id="@+id/layout_calculator_input"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.30"
android:layout_height="wrap_content"
android:orientation="vertical">

<EditText
Expand All @@ -29,7 +27,7 @@

<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
Expand All @@ -50,9 +48,9 @@
<LinearLayout
android:id="@+id/layout_calculator_output"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.70"
android:paddingTop="8dp"
android:layout_height="wrap_content"
android:layout_below="@id/layout_calculator_input"
android:paddingTop="16dp"
android:orientation="vertical"
android:visibility="invisible">

Expand Down Expand Up @@ -92,4 +90,4 @@
android:layout_marginStart="-8dp"
android:layout_marginEnd="-8dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
17 changes: 7 additions & 10 deletions app/src/main/res/layout/fragment_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
android:orientation="vertical">

<LinearLayout
android:id="@+id/layout_notation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -21,7 +20,7 @@
android:textStyle="bold"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" />

<TextView
Expand All @@ -30,12 +29,11 @@
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6" />
</LinearLayout>

<LinearLayout
android:id="@+id/layout_netmask"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -47,7 +45,7 @@
android:textStyle="bold"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" />

<TextView
Expand All @@ -56,12 +54,11 @@
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6" />
</LinearLayout>

<LinearLayout
android:id="@+id/layout_wildcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -73,7 +70,7 @@
android:textStyle="bold"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" />

<TextView
Expand All @@ -82,7 +79,7 @@
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6" />
</LinearLayout>

Expand Down Expand Up @@ -119,4 +116,4 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--Primary Colors-->
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

<!--Additional Colors-->
<color name="black">#000000</color>
<color name="grey">#888888</color>
<color name="light_grey">#AAAAAA</color>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<!--Application Name-->
<!--Application Names-->
<string name="app_name">IP Address Manager</string>
<string name="calculator_name">CIDR Calculator</string>

Expand All @@ -9,7 +9,7 @@
<string name="calculator_button_calculate">Calculate</string>
<string name="calculator_button_clear">Clear</string>
<string name="calculator_info_required_cidr_notations">Required CIDR Notations:</string>
<string name="calculator_error_input">Invalid Inputs!</string>
<string name="calculator_error_input">Invalid Input!</string>
<string name="calculator_placeholder_required_cidr_notaitons">999</string>

<!--Detail Fragment-->
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/com/wtfart/ipaddressmanager/ExampleUnitTest.kt

This file was deleted.

11 changes: 5 additions & 6 deletions app/src/test/java/com/wtfart/ipaddressmanager/model/CidrTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.wtfart.ipaddressmanager.model

import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.assertNotEquals
import org.junit.Test

/**
Expand Down Expand Up @@ -32,7 +31,7 @@ class CidrTest {
)
)

assertTrue(cidr1 == cidr2)
assertEquals(cidr2, cidr1)
}

@Test
Expand All @@ -56,7 +55,7 @@ class CidrTest {
)
)

assertTrue(cidr1.hashCode() == cidr2.hashCode())
assertEquals(cidr2.hashCode(), cidr1.hashCode())
}

@Test
Expand All @@ -80,7 +79,7 @@ class CidrTest {
)
)

assertFalse(cidr1 == cidr2)
assertNotEquals(cidr2, cidr1)
}

@Test
Expand All @@ -104,7 +103,7 @@ class CidrTest {
)
)

assertFalse(cidr1.hashCode() == cidr2.hashCode())
assertNotEquals(cidr2.hashCode(), cidr1.hashCode())
}

@Test
Expand Down

0 comments on commit ee2dc04

Please sign in to comment.