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

Create link navigation Activity #44

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Shaarlier.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
Expand Down
72 changes: 66 additions & 6 deletions app/app.iml

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
Expand All @@ -20,6 +22,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
Expand All @@ -29,8 +42,17 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'io.jsonwebtoken:jjwt:0.9.1'

implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
implementation 'android.arch.navigation:navigation-ui:1.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.13'
implementation 'junit:junit:4.13'
androidTestImplementation 'com.android.support:support-annotations:24.0.0'
androidTestImplementation 'com.android.support.test:runner:0.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
14 changes: 9 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".activities.LinkListActivity"
android:label="@string/title_activity_link_list"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.ShareActivity"
android:autoRemoveFromRecents="true"
Expand All @@ -29,11 +38,6 @@
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.HttpSchemeHandlerActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.dimtion.shaarlier.activities

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.NavigationView
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Menu
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.dimtion.shaarlier.R
import com.dimtion.shaarlier.activities.ui.link.LinkFragment
import com.dimtion.shaarlier.utils.Link

class LinkListActivity : AppCompatActivity(), LinkFragment.OnListFragmentInteractionListener {

private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_link_list)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
val intent = Intent(view.context, ShareActivity::class.java)
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "")
startActivity(intent)
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(R.id.nav_home), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.link_list, menu)
return true
}

override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

override fun onListFragmentInteraction(item: Link?) {
Log.d("LinkListActivity", "Clicked on ${item}")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(item?.url))
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.dimtion.shaarlier.activities.ui.home

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.dimtion.shaarlier.R

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dimtion.shaarlier.activities.ui.home

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel

class HomeViewModel : ViewModel() {

private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
}
val text: LiveData<String> = _text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.dimtion.shaarlier.activities.ui.link

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.os.Messenger
import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.dimtion.shaarlier.R
import com.dimtion.shaarlier.activities.MainActivity
import com.dimtion.shaarlier.helpers.AccountsSource

import com.dimtion.shaarlier.helpers.LinksSource
import com.dimtion.shaarlier.services.NetworkService
import com.dimtion.shaarlier.utils.Link
import com.dimtion.shaarlier.utils.ShaarliAccount

/**
* A fragment representing a list of Links.
* Activities containing this fragment MUST implement the
* [LinkFragment.OnListFragmentInteractionListener] interface.
*/
class LinkFragment : Fragment() {

// TODO: Customize parameters
private var columnCount = 1

private var listener: OnListFragmentInteractionListener? = null

private lateinit var mShaarliAccount: ShaarliAccount
private lateinit var mLinksSource: LinksSource
private lateinit var mRefreshLayout: SwipeRefreshLayout
private lateinit var mRecyclerView: RecyclerView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

arguments?.let {
columnCount = it.getInt(ARG_COLUMN_COUNT)
}

val accountsSource = AccountsSource(this.context)
val defaultAccount = accountsSource.defaultAccount
if (defaultAccount != null) {
mShaarliAccount = accountsSource.defaultAccount
} else {
val intent = Intent(this.context, MainActivity::class.java)
startActivity(intent)
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
mRefreshLayout = inflater.inflate(R.layout.fragment_link_list, container, false) as SwipeRefreshLayout
mRecyclerView = mRefreshLayout.findViewById(R.id.list)

mLinksSource = LinksSource()
// Set the adapter
with(mRecyclerView) {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
adapter = MyLinkRecyclerViewAdapter(mLinksSource.LINKS, listener)
}

// Set the refresh callback
mRefreshLayout.setOnRefreshListener {
Log.i("LinkFragment", "onRefresh called from SwipeRefreshLayout")
updateLinks()
}

updateLinks()
return mRefreshLayout
}

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnListFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnListFragmentInteractionListener")
}
}

override fun onDetach() {
super.onDetach()
listener = null
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson
* [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html)
* for more information.
*/
interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
fun onListFragmentInteraction(item: Link?)
}

companion object {
// TODO: Customize parameter argument names
const val ARG_COLUMN_COUNT = "column-count"

// TODO: Customize parameter initialization
@JvmStatic
fun newInstance(columnCount: Int) =
LinkFragment().apply {
arguments = Bundle().apply {
putInt(ARG_COLUMN_COUNT, columnCount)
}
}
}

private fun updateLinks() {
if (!::mShaarliAccount.isInitialized) {
// If the account is not init, we go to the settings
// TODO: probably print a toast
val intent = Intent(this.context, MainActivity::class.java)
startActivity(intent)
}
mRefreshLayout.isRefreshing = true
val accountsSource = AccountsSource(this.context)
val intent = Intent(this.context, NetworkService::class.java)
intent.putExtra("action", NetworkService.INTENT_GET_LINKS)
intent.putExtra("account", mShaarliAccount)
intent.putExtra(NetworkService.EXTRA_MESSENGER, Messenger(NetworkHandler(this)))
this.context?.startService(intent)
}

private class NetworkHandler(parent: LinkFragment) : Handler() {
val mParent = parent
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)

Log.d("LinkFragment", msg.toString())
when (msg?.arg1) {
NetworkService.GET_LINKS -> {
Log.d("LinkFragment", msg.toString())
mParent.mLinksSource.setLinks(msg.obj as List<Link>)
}
else -> Log.e("LinkFrament", "NetworkServiceError: ${msg?.arg1}")
}

mParent.mRefreshLayout.isRefreshing = false
mParent.mRecyclerView.adapter?.notifyDataSetChanged()
Log.d("LinkFragment", "LINKS")
Log.d("LinkFragment", mParent.mLinksSource.LINKS.toString())
}
}
}
Loading