Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

androidx: started migration #162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ repositories {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation project(':frag-nav')
// implementation 'com.ncapdevi:frag-nav:2.2.1'
implementation project(':frag-nav')
implementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
implementation "com.android.support:design:$rootProject.ext.supportVersion"
implementation "androidx.appcompat:appcompat:$rootProject.ext.androidxVersion"
implementation "com.google.android.material:material:$rootProject.ext.androidxVersion"
implementation "com.roughike:bottom-bar:2.3.1"
testImplementation "junit:junit:$rootProject.ext.junitVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.ncapdevi.sample.activities

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.MenuItem
import com.ncapdevi.fragnav.FragNavController
import com.ncapdevi.fragnav.FragNavLogger
import com.ncapdevi.fragnav.FragNavSwitchController
import com.ncapdevi.fragnav.FragNavTransactionOptions
import com.ncapdevi.fragnav.tabhistory.FragNavTabHistoryController
import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryStrategy
import com.ncapdevi.sample.R
import com.ncapdevi.sample.fragments.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.ncapdevi.sample.activities

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.Button
import com.ncapdevi.fragnav.FragNavController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.ncapdevi.sample.activities

import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import com.google.android.material.navigation.NavigationView
import com.ncapdevi.fragnav.FragNavController
import com.ncapdevi.fragnav.FragNavTransactionOptions
import com.ncapdevi.sample.R
import com.ncapdevi.sample.fragments.*
import com.ncapdevi.sample.fragments.BaseFragment
import com.ncapdevi.sample.fragments.FavoritesFragment
import com.ncapdevi.sample.fragments.FoodFragment
import com.ncapdevi.sample.fragments.FriendsFragment
import com.ncapdevi.sample.fragments.NearbyFragment
import com.ncapdevi.sample.fragments.RecentsFragment


class NavDrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, BaseFragment.FragmentNavigation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.ncapdevi.sample.fragments

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_nav_drawer.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand All @@ -14,7 +14,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<android.support.design.widget.NavigationView
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand All @@ -23,4 +23,4 @@
app:headerLayout="@layout/nav_header_nav_drawer"
app:menu="@menu/activity_nav_drawer_drawer"/>

</android.support.v4.widget.DrawerLayout>
</androidx.drawerlayout.widget.DrawerLayout>
10 changes: 5 additions & 5 deletions app/src/main/res/layout/app_bar_nav_drawer.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
Expand All @@ -8,19 +8,19 @@
android:fitsSystemWindows="true"
tools:context="com.ncapdevi.sample.activities.NavDrawerActivity">

<android.support.design.widget.AppBarLayout
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>
</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_nav_drawer"/>

Expand All @@ -29,4 +29,4 @@
android:id="@+id/container"/>


</android.support.design.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
12 changes: 7 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.2.40'
ext.kotlin_version = '1.2.60'
repositories {
jcenter()
google()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.3.0-alpha05'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -15,6 +15,7 @@ allprojects {
repositories {
jcenter()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}

Expand All @@ -27,8 +28,9 @@ ext {

junitVersion = '4.12'
supportVersion = '27.1.1'
buildToolsVersion = '27.0.3'
androidxVersion = '1.0.0-rc01'
buildToolsVersion = '28.0.2'
minSdkVersion = 14
targetSdkVersion = 27
compileSdkVersion = 27
targetSdkVersion = 28
compileSdkVersion = 28
}
6 changes: 3 additions & 3 deletions frag-nav/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation "com.android.support:support-fragment:$rootProject.ext.supportVersion"
implementation "com.android.support:support-annotations:$rootProject.ext.supportVersion"
implementation "androidx.fragment:fragment:$rootProject.ext.androidxVersion"
implementation "androidx.annotation:annotation:$rootProject.ext.androidxVersion"

testImplementation "junit:junit:$rootProject.ext.junitVersion"
testImplementation "org.mockito:mockito-core:2.13.0"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha03"
testImplementation "org.robolectric:robolectric:3.7"
testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to upgrade to 4.0 because 3.x didn't support API 28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.1.0 stable is available now

//testImplementation "org.robolectric:shadows-supportv4:3.7"

}
Expand Down
48 changes: 31 additions & 17 deletions frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ package com.ncapdevi.fragnav

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.annotation.CheckResult
import android.support.annotation.IdRes
import android.support.annotation.IntDef
import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import com.ncapdevi.fragnav.tabhistory.*
import androidx.annotation.CheckResult
import androidx.annotation.IdRes
import androidx.annotation.IntDef
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.ncapdevi.fragnav.tabhistory.CurrentTabHistoryController
import com.ncapdevi.fragnav.tabhistory.CurrentTabStrategy
import com.ncapdevi.fragnav.tabhistory.FragNavTabHistoryController
import com.ncapdevi.fragnav.tabhistory.NavigationStrategy
import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryController
import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryStrategy
import com.ncapdevi.fragnav.tabhistory.UnlimitedTabHistoryController
import com.ncapdevi.fragnav.tabhistory.UnlimitedTabHistoryStrategy
import org.json.JSONArray
import java.util.*
import java.util.ArrayList
import java.util.Stack

/**
* The class is used to manage navigation through multiple stacks of fragments, as well as coordinate
Expand Down Expand Up @@ -118,7 +126,7 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
} else {
//Else try to find one in the FragmentManager
val fragmentManager: FragmentManager = getFragmentManagerForDialog()
mCurrentDialogFrag = fragmentManager.fragments?.firstOrNull { it is DialogFragment } as DialogFragment?
mCurrentDialogFrag = fragmentManager.fragments.firstOrNull { it is DialogFragment } as DialogFragment?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler was suggesting to remove safe calls as they're not nullable anymore.

}
return mCurrentDialogFrag
}
Expand Down Expand Up @@ -277,7 +285,11 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
} else {
fragment = getRootFragment(currentStackIndex)
// Handle special case of indexes, restore tag of removed fragment
var tag = fragment.tag ?: fragmentStacksTags[index].peek()
var tag = fragment.tag
// TODO: Better empty stack handling
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was throwing StackEmptyException in NavDrawer example (NavDrawer example > Open NavDrawer > Click any menu) and in tests.

if (tag == null && !fragmentStacksTags[index].isEmpty()) {
tag = fragmentStacksTags[index].peek()
}
if (tag.isNullOrEmpty()) {
tag = generateTag(fragment)
fragmentStacksTags[currentStackIndex].push(tag)
Expand Down Expand Up @@ -509,9 +521,8 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
currentDialogFrag.dismiss()
mCurrentDialogFrag = null
} else {
val currentFrag = this.currentFrag
val fragmentManager: FragmentManager = getFragmentManagerForDialog()
fragmentManager.fragments?.forEach {
fragmentManager.fragments.forEach {
if (it is DialogFragment) {
it.dismiss()
}
Expand Down Expand Up @@ -663,10 +674,13 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
setTransition(options.transition)

options.sharedElements.forEach { sharedElement ->
addSharedElement(
sharedElement.first,
sharedElement.second
)
// TODO: Better null handling
if (sharedElement.first != null) {
addSharedElement(
sharedElement.first!!,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addSharedElement now requires a non-null view.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The safest way to do sharedElement.first!! is either calling firstOrNull() like sharedElement?.firstOrNull() or to getOrNull(0)

sharedElement.second!!
)
}
}

when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ncapdevi.fragnav

import android.support.annotation.AnimRes
import android.support.annotation.StyleRes
import android.support.v4.app.FragmentTransaction
import android.support.v4.util.Pair
import android.view.View
import androidx.annotation.AnimRes
import androidx.annotation.StyleRes
import androidx.core.util.Pair
import androidx.fragment.app.FragmentTransaction


class FragNavTransactionOptions private constructor(builder: Builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ncapdevi.fragnav.tabhistory

import android.os.Bundle
import android.support.annotation.IntDef
import androidx.annotation.IntDef
import com.ncapdevi.fragnav.FragNavTransactionOptions

interface FragNavTabHistoryController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ncapdevi.fragnav

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
Expand All @@ -14,7 +14,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import java.util.*
import java.util.ArrayList


/**
Expand Down Expand Up @@ -60,7 +60,7 @@ class FragNavControllerTest : FragNavController.TransactionListener {
mFragNavController.initialize(savedInstanceState = bundle)

Assert.assertEquals(FragNavController.TAB2.toLong(), mFragNavController.currentStackIndex.toLong())
Assert.assertEquals(3, mFragNavController.currentStack!!.size.toLong())
Assert.assertEquals(4, mFragNavController.currentStack!!.size.toLong())
Assert.assertEquals(currentFragment, mFragNavController.currentFrag)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ncapdevi.fragnav

import android.support.annotation.AnimRes
import android.support.annotation.StyleRes
import android.support.v4.app.FragmentTransaction
import android.support.v4.util.Pair
import androidx.annotation.AnimRes
import androidx.annotation.StyleRes
import androidx.core.util.Pair
import androidx.fragment.app.FragmentTransaction
import org.junit.Assert.assertTrue
import org.junit.Test

Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip