Skip to content
Merged
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: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.exifinterface:exifinterface:1.0.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0"
implementation 'com.github.albfernandez:juniversalchardet:2.0.3' // need this version for Android <7
implementation 'com.google.code.findbugs:annotations:2.0.1'
implementation 'commons-io:commons-io:2.6'
Expand Down Expand Up @@ -305,6 +306,7 @@ dependencies {
testImplementation 'org.powermock:powermock-api-mockito2:2.0.2'
testImplementation 'org.json:json:20180813'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
testImplementation "androidx.arch.core:core-testing:2.0.1"

// dependencies for instrumented tests
// JUnit4 Rules
Expand Down
4 changes: 4 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@
android:name=".ui.activity.SsoGrantPermissionActivity"
android:exported="true"
android:theme="@style/Theme.ownCloud.Dialog.NoTitle" />

<activity
android:name="com.nextcloud.client.etm.EtmActivity"
android:theme="@style/Theme.ownCloud.Toolbar"/>
</application>

</manifest>
4 changes: 3 additions & 1 deletion src/main/java/com/nextcloud/client/di/AppComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
AppInfoModule.class,
NetworkModule.class,
DeviceModule.class,
OnboardingModule.class
OnboardingModule.class,
ViewModelModule.class
})
@Singleton
public interface AppComponent {

void inject(MainApp app);

@Component.Builder
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/nextcloud/client/di/ComponentsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.nextcloud.client.di;

import com.nextcloud.client.onboarding.FirstRunActivity;
import com.nextcloud.client.etm.EtmActivity;
import com.nextcloud.client.onboarding.WhatsNewActivity;
import com.owncloud.android.authentication.AuthenticatorActivity;
import com.owncloud.android.authentication.DeepLinkLoginActivity;
Expand Down Expand Up @@ -121,6 +122,7 @@ abstract class ComponentsModule {
@ContributesAndroidInjector abstract UploadPathActivity uploadPathActivity();
@ContributesAndroidInjector abstract UserInfoActivity userInfoActivity();
@ContributesAndroidInjector abstract WhatsNewActivity whatsNewActivity();
@ContributesAndroidInjector abstract EtmActivity etmActivity();

@ContributesAndroidInjector abstract ExtendedListFragment extendedListFragment();
@ContributesAndroidInjector abstract FileDetailFragment fileDetailFragment();
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/com/nextcloud/client/di/ViewModelFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider

/**
* This factory provide [ViewModel] instances initialized by Dagger 2 dependency injection system.
*
* Each [javax.inject.Provider] instance accesses Dagger machinery, which provide
* fully-initialized [ViewModel] instance.
*
* @see ViewModelModule
* @see ViewModelKey
*/
class ViewModelFactory @Inject constructor(
private val viewModelProviders: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
var vmProvider: Provider<ViewModel>? = viewModelProviders.get(modelClass)

if (vmProvider == null) {
for (entry in viewModelProviders.entries) {
if (modelClass.isAssignableFrom(entry.key)) {
vmProvider = entry.value
break
}
}
}

if (vmProvider == null) {
throw IllegalArgumentException("${modelClass.simpleName} view model class is not supported")
}

@Suppress("TooGenericExceptionCaught", "TooGenericExceptionThrown", "UNCHECKED_CAST")
try {
val vm = vmProvider.get() as T
return vm
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/nextcloud/client/di/ViewModelKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.di

import androidx.lifecycle.ViewModel
import dagger.MapKey
import kotlin.reflect.KClass

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
38 changes: 38 additions & 0 deletions src/main/java/com/nextcloud/client/di/ViewModelModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.client.etm.EtmViewModel
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap

@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(EtmViewModel::class)
abstract fun etmViewModel(vm: EtmViewModel): ViewModel

@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
91 changes: 91 additions & 0 deletions src/main/java/com/nextcloud/client/etm/EtmActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.etm

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.client.di.Injectable
import com.nextcloud.client.di.ViewModelFactory
import com.owncloud.android.R
import com.owncloud.android.ui.activity.ToolbarActivity
import javax.inject.Inject

class EtmActivity : ToolbarActivity(), Injectable {

companion object {
@JvmStatic
fun launch(context: Context) {
val etmIntent = Intent(context, EtmActivity::class.java)
context.startActivity(etmIntent)
}
}

@Inject
lateinit var viewModelFactory: ViewModelFactory
internal lateinit var vm: EtmViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_etm)
setupToolbar()
updateActionBarTitleAndHomeButtonByString(getString(R.string.etm_title))
vm = ViewModelProvider(this, viewModelFactory).get(EtmViewModel::class.java)
vm.currentPage.observe(this, Observer {
onPageChanged(it)
})
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
android.R.id.home -> {
if (!vm.onBackPressed()) {
finish()
}
true
}
else -> super.onOptionsItemSelected(item)
}
}

override fun onBackPressed() {
if (!vm.onBackPressed()) {
super.onBackPressed()
}
}

private fun onPageChanged(page: EtmMenuEntry?) {
if (page != null) {
val fragment = page.pageClass.java.getConstructor().newInstance()
supportFragmentManager.beginTransaction()
.replace(R.id.etm_page_container, fragment)
.commit()
updateActionBarTitleAndHomeButtonByString("ETM - ${getString(page.titleRes)}")
} else {
supportFragmentManager.beginTransaction()
.replace(R.id.etm_page_container, EtmMenuFragment())
.commitNow()
updateActionBarTitleAndHomeButtonByString(getString(R.string.etm_title))
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/nextcloud/client/etm/EtmBaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.etm

import androidx.fragment.app.Fragment

abstract class EtmBaseFragment : Fragment() {
protected val vm: EtmViewModel get() {
return (activity as EtmActivity).vm
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/nextcloud/client/etm/EtmMenuAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.etm

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.owncloud.android.R

class EtmMenuAdapter(
context: Context,
val onItemClicked: (Int) -> Unit
) : RecyclerView.Adapter<EtmMenuAdapter.PageViewHolder>() {

private val layoutInflater = LayoutInflater.from(context)
var pages: List<EtmMenuEntry> = listOf()
set(value) {
field = value
notifyDataSetChanged()
}

class PageViewHolder(view: View, onClick: (Int) -> Unit) : RecyclerView.ViewHolder(view) {
val primaryAction: ImageView = view.findViewById(R.id.primary_action)
val text: TextView = view.findViewById(R.id.text)
val secondaryAction: ImageView = view.findViewById(R.id.secondary_action)

init {
itemView.setOnClickListener { onClick(adapterPosition) }
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
val view = layoutInflater.inflate(R.layout.material_list_item_single_line, parent, false)
return PageViewHolder(view, onItemClicked)
}

override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
val page = pages[position]
holder.primaryAction.setImageResource(page.iconRes)
holder.text.setText(page.titleRes)
holder.secondaryAction.setImageResource(0)
}

override fun getItemCount(): Int {
return pages.size
}
}
Loading