-
Notifications
You must be signed in to change notification settings - Fork 2
Use Android Architecture Components #1
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
Merged
lorensr
merged 7 commits into
GraphQLGuide:master
from
daniel-stoneuk:add-arch-components
Jul 14, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f854ad4
Add LiveData and ViewModel to fragments
daniel-stoneuk 14746ff
Move toolbar to activity
daniel-stoneuk de4acbb
Use labels to change toolbar instead of programatically
daniel-stoneuk de19686
Improve adapter constructors
daniel-stoneuk 42cde97
Use shared axis transition along X axis
daniel-stoneuk 7264827
Improve LiveData syntax
daniel-stoneuk 2094ef9
Display chapter numbers as integers instead of decimal
daniel-stoneuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package guide.graphql.toc | ||
|
||
//https://github.com/android/architecture-components-samples/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/Resource.kt | ||
data class Resource<out T>(val status: Status, val data: T?, val message: String?) { | ||
companion object { | ||
fun <T> success(data: T?): Resource<T> { | ||
return Resource(Status.SUCCESS, data, null) | ||
} | ||
|
||
fun <T> error(msg: String, data: T?): Resource<T> { | ||
return Resource(Status.ERROR, data, msg) | ||
} | ||
|
||
fun <T> loading(data: T?): Resource<T> { | ||
return Resource(Status.LOADING, data, null) | ||
} | ||
} | ||
} | ||
enum class Status { | ||
SUCCESS, | ||
ERROR, | ||
LOADING | ||
} |
103 changes: 0 additions & 103 deletions
103
app/src/main/java/guide/graphql/toc/SectionsFragment.kt
This file was deleted.
Oops, something went wrong.
11 changes: 9 additions & 2 deletions
11
...java/guide/graphql/toc/ChaptersAdapter.kt → ...raphql/toc/ui/chapters/ChaptersAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package guide.graphql.toc.ui.chapters | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Observer | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.recyclerview.widget.DividerItemDecoration | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.google.android.material.transition.MaterialFade | ||
import com.google.android.material.transition.MaterialSharedAxis | ||
import guide.graphql.toc.R | ||
import guide.graphql.toc.Status | ||
import guide.graphql.toc.databinding.ChaptersFragmentBinding | ||
|
||
class ChaptersFragment : Fragment() { | ||
|
||
private val viewModel: ChaptersViewModel by viewModels() | ||
|
||
private lateinit var binding: ChaptersFragmentBinding | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
binding = ChaptersFragmentBinding.inflate(inflater) | ||
return binding.root | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val backward = MaterialSharedAxis(MaterialSharedAxis.X, false) | ||
reenterTransition = backward | ||
|
||
val forward = MaterialSharedAxis(MaterialSharedAxis.X, true) | ||
exitTransition = forward | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
requireActivity() | ||
val adapter = | ||
ChaptersAdapter( | ||
requireContext() | ||
) { chapter -> | ||
findNavController().navigate( | ||
ChaptersFragmentDirections.viewSections( | ||
chapterId = chapter.id, | ||
chapterNumber = chapter.number?.toInt() ?: -1, | ||
chapterTitle = if (chapter.number == null) chapter.title else getString( | ||
R.string.chapter_title, | ||
chapter.number.toInt().toString(), | ||
chapter.title | ||
) | ||
) | ||
) | ||
} | ||
val layoutManager = LinearLayoutManager(requireContext()) | ||
binding.chapters.layoutManager = layoutManager | ||
val itemDivider = DividerItemDecoration(requireContext(), layoutManager.orientation) | ||
binding.chapters.addItemDecoration(itemDivider) | ||
binding.chapters.adapter = adapter | ||
|
||
viewModel.chapterList.observe(viewLifecycleOwner, Observer { chapterListResponse -> | ||
when (chapterListResponse.status) { | ||
Status.SUCCESS -> { | ||
chapterListResponse.data?.let { | ||
adapter.updateChapters(it) | ||
} | ||
} | ||
Status.ERROR -> Toast.makeText( | ||
requireContext(), | ||
"Error: ${chapterListResponse.message}", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
}) | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing in the chapter title, we generate it here before passing it to the second fragment. This means we can use this argument in the nav editor.