Commit 21925e7a authored by Matthieu's avatar Matthieu

Dependency injection

Configure dependency injection for API and the database. Uses Dagger (dagger.dev)

The API is held in a holder, so that it can be invalidated or recreated as needed.

Big refactor of the feeds, this introduces a bit more duplication of code (some very similar methods that differ only in the types or minor details), but now the inheritance makes more sense and there is less "forcing" to make the inheritance work (for example: no more casting Int to String just to have the right type for the key)
It should be easier to work with the feeds in the future, maybe we'll find better abstractions later on.
parent 15d80da6
......@@ -6,7 +6,7 @@ apply plugin: 'jacoco'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
buildToolsVersion '30.0.1'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
......@@ -56,12 +56,12 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'com.squareup.okhttp3:okhttp:4.7.2'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'androidx.navigation:navigation-ui:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:4.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
......@@ -78,9 +78,9 @@ dependencies {
testImplementation "androidx.room:room-testing:$room_version"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
implementation 'info.androidhive:imagefilters:1.0.7'
implementation 'com.github.yalantis:ucrop:2.2.5-native'
......@@ -97,11 +97,11 @@ dependencies {
kapt 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation "com.github.tomakehurst:wiremock-jre8:2.26.3"
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.27.1'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation 'junit:junit:4.13'
androidTestImplementation('com.squareup.okhttp3:mockwebserver:4.7.2')
androidTestImplementation('com.squareup.okhttp3:mockwebserver:4.8.0')
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
......@@ -126,24 +126,30 @@ dependencies {
implementation "com.mikepenz:iconics-views:5.0.2"
implementation 'com.mikepenz:google-material-typeface:3.0.1.4.original-kotlin@aar'
//Dagger (dependency injection)
implementation 'com.google.dagger:dagger-android:2.28.3'
implementation 'com.google.dagger:dagger-android-support:2.28.3'
// if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.28.3'
kapt 'com.google.dagger:dagger-compiler:2.28.3'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
def fragment_version = '1.2.4'
def fragment_version = '1.2.5'
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
// Use the most recent version of CameraX
def camerax_version = '1.0.0-beta04'
def camerax_version = '1.0.0-beta07'
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation 'androidx.camera:camera-view:1.0.0-alpha11'
implementation 'androidx.camera:camera-view:1.0.0-alpha14'
implementation 'com.karumi:dexter:6.1.2'
implementation 'com.karumi:dexter:6.2.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
......
......@@ -16,6 +16,7 @@ import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import org.junit.Before
import org.junit.Rule
......@@ -40,7 +41,7 @@ class DrawerMenuTest {
val baseUrl = mockServer.getUrl()
context = ApplicationProvider.getApplicationContext()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -6,7 +6,6 @@ import android.widget.TextView
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.swipeDown
import androidx.test.espresso.action.ViewActions.swipeUp
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
......@@ -17,7 +16,7 @@ import com.google.android.material.tabs.TabLayout
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.fragments.feeds.PostViewHolder
import com.h.pixeldroid.fragments.feeds.postFeeds.PostViewHolder
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.clickChildViewWithId
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.first
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.getText
......@@ -25,6 +24,7 @@ import com.h.pixeldroid.testUtility.CustomMatchers.Companion.second
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.slowSwipeUp
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.typeTextInViewWithId
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import org.junit.Before
import org.junit.Rule
......@@ -49,7 +49,7 @@ class HomeFeedTest {
mockServer.start()
val baseUrl = mockServer.getUrl()
context = ApplicationProvider.getApplicationContext()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -26,10 +26,11 @@ import androidx.test.rule.ActivityTestRule
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.fragments.feeds.PostViewHolder
import com.h.pixeldroid.fragments.feeds.postFeeds.PostViewHolder
import com.h.pixeldroid.objects.Account
import com.h.pixeldroid.objects.Account.Companion.ACCOUNT_TAG
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
......@@ -64,7 +65,7 @@ class IntentTest {
val baseUrl = mockServer.getUrl()
context = ApplicationProvider.getApplicationContext()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -13,7 +13,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.utils.DBUtils
import com.h.pixeldroid.testUtility.initDB
import org.junit.After
import org.junit.Before
import org.junit.Rule
......@@ -43,7 +43,7 @@ class LoginActivityOfflineTest {
fun before() {
switchAirplaneMode()
val context = ApplicationProvider.getApplicationContext<Context>()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
ActivityScenario.launch(LoginActivity::class.java)
}
......
......@@ -20,6 +20,7 @@ import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import org.junit.Before
import org.junit.Rule
......@@ -45,7 +46,7 @@ class LoginActivityOnlineTest {
context = ApplicationProvider.getApplicationContext()
pref = context.getSharedPreferences("com.h.pixeldroid.pref", Context.MODE_PRIVATE)
pref.edit().clear().apply()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.close()
}
......@@ -97,7 +98,7 @@ class LoginActivityOnlineTest {
@Test
fun correctIntentReturnLoadsMainActivity() {
context = ApplicationProvider.getApplicationContext()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
......
......@@ -16,10 +16,11 @@ import com.google.android.material.tabs.TabLayout
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.fragments.feeds.PostViewHolder
import com.h.pixeldroid.fragments.feeds.postFeeds.PostViewHolder
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.clickChildViewWithId
import com.h.pixeldroid.testUtility.CustomMatchers.Companion.first
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import com.h.pixeldroid.utils.PostUtils.Companion.censorColorMatrix
import com.h.pixeldroid.utils.PostUtils.Companion.uncensorColorMatrix
......@@ -46,7 +47,7 @@ class MockedServerTest {
mockServer.start()
val baseUrl = mockServer.getUrl()
context = ApplicationProvider.getApplicationContext()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -24,6 +24,7 @@ import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.testUtility.CustomMatchers
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import kotlinx.android.synthetic.main.activity_post_creation.*
import org.hamcrest.Matchers.not
......@@ -60,7 +61,7 @@ class PostCreationActivityTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext
mockServer.start()
val baseUrl = mockServer.getUrl()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -19,6 +19,7 @@ import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.InstanceDatabaseEntity
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import kotlinx.android.synthetic.main.activity_main.*
import org.hamcrest.Matcher
......@@ -73,7 +74,7 @@ class PostFragmentUITests {
val context = InstrumentationRegistry.getInstrumentation().targetContext
mockServer.start()
val baseUrl = mockServer.getUrl()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
......@@ -27,6 +27,7 @@ import com.h.pixeldroid.objects.Status
import com.h.pixeldroid.objects.Tag
import com.h.pixeldroid.testUtility.MockServer
import com.h.pixeldroid.testUtility.initDB
import com.h.pixeldroid.utils.DBUtils
import org.hamcrest.Matcher
import org.junit.*
......@@ -49,7 +50,7 @@ class PostTest {
val mockServer = MockServer()
mockServer.start()
val baseUrl = mockServer.getUrl()
db = DBUtils.initDB(context)
db = initDB(context)
db.clearAllTables()
db.instanceDao().insertInstance(
InstanceDatabaseEntity(
......
package com.h.pixeldroid.testUtility
import android.content.Context
import androidx.room.Room
import com.h.pixeldroid.db.AppDatabase
fun initDB(context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java, "pixeldroid"
).allowMainThreadQueries().build()
}
......@@ -16,13 +16,14 @@
<application
android:name=".Pixeldroid"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
<activity
android:name=".PhotoEditActivity"
......
......@@ -4,6 +4,8 @@ import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.di.PixelfedAPIHolder
import com.h.pixeldroid.fragments.feeds.AccountListFragment
import com.h.pixeldroid.objects.Account
import com.h.pixeldroid.objects.Account.Companion.ACCOUNT_ID_TAG
......@@ -12,28 +14,32 @@ import com.h.pixeldroid.utils.DBUtils
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class FollowsActivity : AppCompatActivity() {
private var followsFragment = AccountListFragment()
@Inject
lateinit var db: AppDatabase
@Inject
lateinit var apiHolder: PixelfedAPIHolder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_followers)
(this.application as Pixeldroid).getAppComponent().inject(this)
// Get account id
val id = intent.getSerializableExtra(ACCOUNT_ID_TAG) as String?
val following = intent.getSerializableExtra(FOLLOWING_TAG) as Boolean
if(id == null) {
val db = DBUtils.initDB(applicationContext)
val user = db.userDao().getActiveUser()
val domain = user?.instance_uri.orEmpty()
val accessToken = user?.accessToken.orEmpty()
db.close()
val pixelfedAPI = PixelfedAPI.create(domain)
val pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
pixelfedAPI.verifyCredentials("Bearer $accessToken").enqueue(object :
Callback<Account> {
......
......@@ -4,7 +4,6 @@ import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Bundle
import android.view.View
......@@ -13,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.di.PixelfedAPIHolder
import com.h.pixeldroid.objects.Account
import com.h.pixeldroid.objects.Application
import com.h.pixeldroid.objects.Instance
......@@ -26,6 +26,7 @@ import okhttp3.HttpUrl
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class LoginActivity : AppCompatActivity() {
......@@ -38,7 +39,12 @@ class LoginActivity : AppCompatActivity() {
private lateinit var oauthScheme: String
private lateinit var appName: String
private lateinit var preferences: SharedPreferences
private lateinit var db: AppDatabase
@Inject
lateinit var db: AppDatabase
@Inject
lateinit var apiHolder: PixelfedAPIHolder
private lateinit var pixelfedAPI: PixelfedAPI
private var inputVisibility: Int = View.GONE
......@@ -46,11 +52,11 @@ class LoginActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
(application as Pixeldroid).getAppComponent().inject(this)
loadingAnimation(true)
appName = getString(R.string.app_name)
oauthScheme = getString(R.string.auth_scheme)
preferences = getSharedPreferences("$PACKAGE_ID.pref", Context.MODE_PRIVATE)
db = DBUtils.initDB(applicationContext)
if (Utils.hasInternet(applicationContext)) {
connect_instance_button.setOnClickListener {
......@@ -61,8 +67,8 @@ class LoginActivity : AppCompatActivity() {
} else {
login_activity_connection_required.visibility = View.VISIBLE
login_activity_connection_required_button.setOnClickListener {
finish();
startActivity(intent);
finish()
startActivity(intent)
}
}
loadingAnimation(false)
......@@ -113,7 +119,7 @@ class LoginActivity : AppCompatActivity() {
hideKeyboard()
loadingAnimation(true)
PixelfedAPI.create(normalizedDomain).registerApplication(
apiHolder.setDomain(normalizedDomain).registerApplication(
appName,"$oauthScheme://$PACKAGE_ID", SCOPE
).enqueue(object : Callback<Application> {
override fun onResponse(call: Call<Application>, response: Response<Application>) {
......@@ -185,7 +191,7 @@ class LoginActivity : AppCompatActivity() {
}
}
pixelfedAPI = PixelfedAPI.create(domain)
pixelfedAPI = apiHolder.setDomain(domain)
pixelfedAPI.obtainToken(
clientId, clientSecret, "$oauthScheme://$PACKAGE_ID", SCOPE, code,
"authorization_code"
......@@ -242,7 +248,7 @@ class LoginActivity : AppCompatActivity() {
.enqueue(object : Callback<Account> {
override fun onResponse(call: Call<Account>, response: Response<Account>) {
if (response.body() != null && response.isSuccessful) {
db.userDao().deActivateActiveUser()
db.userDao().deActivateActiveUsers()
val user = response.body() as Account
DBUtils.addUser(
db,
......@@ -251,7 +257,7 @@ class LoginActivity : AppCompatActivity() {
activeUser = true,
accessToken = accessToken
)
db.close()
apiHolder.setDomainToCurrentUser(db)
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
......
......@@ -14,15 +14,15 @@ import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayoutMediator
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.db.UserDatabaseEntity
import com.h.pixeldroid.di.PixelfedAPIHolder
import com.h.pixeldroid.fragments.CameraFragment
import com.h.pixeldroid.fragments.SearchDiscoverFragment
import com.h.pixeldroid.fragments.feeds.NotificationsFragment
import com.h.pixeldroid.fragments.feeds.OfflineFeedFragment
import com.h.pixeldroid.fragments.feeds.PostsFeedFragment
import com.h.pixeldroid.fragments.feeds.PublicTimelineFragment
import com.h.pixeldroid.fragments.feeds.postFeeds.HomeTimelineFragment
import com.h.pixeldroid.fragments.feeds.postFeeds.PublicTimelineFragment
import com.h.pixeldroid.objects.Account
import com.h.pixeldroid.utils.DBUtils
import com.h.pixeldroid.utils.Utils.Companion.hasInternet
......@@ -39,11 +39,16 @@ import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class MainActivity : AppCompatActivity() {
private val searchDiscoverFragment: SearchDiscoverFragment = SearchDiscoverFragment()
private lateinit var db: AppDatabase
@Inject
lateinit var db: AppDatabase
@Inject
lateinit var apiHolder: PixelfedAPIHolder
private lateinit var header: AccountHeaderView
private var user: UserDatabaseEntity? = null
......@@ -56,7 +61,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = DBUtils.initDB(applicationContext)
(this.application as Pixeldroid).getAppComponent().inject(this)
//get the currently active user
user = db.userDao().getActiveUser()
......@@ -67,7 +72,7 @@ class MainActivity : AppCompatActivity() {
} else {
setupDrawer()
val tabs = arrayOf(
if (hasInternet(applicationContext)) PostsFeedFragment()
if (hasInternet(applicationContext)) HomeTimelineFragment()
else OfflineFeedFragment(),
searchDiscoverFragment,
CameraFragment(),
......@@ -163,14 +168,17 @@ class MainActivity : AppCompatActivity() {
}
private fun getUpdatedAccount(){
private fun getUpdatedAccount() {
if (hasInternet(applicationContext)) {
val domain = user?.instance_uri.orEmpty()
val accessToken = user?.accessToken.orEmpty()
val pixelfedAPI = PixelfedAPI.create(domain)
pixelfedAPI.verifyCredentials("Bearer $accessToken")
val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
api.verifyCredentials("Bearer $accessToken")
.enqueue(object : Callback<Account> {
override fun onResponse(call: Call<Account>, response: Response<Account>) {
override fun onResponse(
call: Call<Account>,
response: Response<Account>
) {
if (response.body() != null && response.isSuccessful) {
val account = response.body() as Account
DBUtils.addUser(db, account, domain, accessToken = accessToken)
......@@ -197,9 +205,9 @@ class MainActivity : AppCompatActivity() {
return false
}
db.userDao().deActivateActiveUser()
db.userDao().deActivateActiveUsers()
db.userDao().activateUser(profile.identifier.toString())
apiHolder.setDomainToCurrentUser(db)
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
......@@ -271,9 +279,9 @@ class MainActivity : AppCompatActivity() {
}
/**
Launches the given activity and put it as the current one
Setting argument firstTime to true means the task history will be reset (as if the app were launched anew into
this activity)
* Launches the given activity and put it as the current one
* @param firstTime to true means the task history will be reset (as if the app were
* launched anew into this activity)
*/
private fun launchActivity(activity: AppCompatActivity, firstTime: Boolean = false) {
val intent = Intent(this, activity::class.java)
......@@ -285,7 +293,7 @@ class MainActivity : AppCompatActivity() {
}
/**
Closes the drawer if it is open, when we press the back button
* Closes the drawer if it is open, when we press the back button
*/
override fun onBackPressed() {
if(drawer_layout.isDrawerOpen(GravityCompat.START)){
......
......@@ -2,13 +2,29 @@ package com.h.pixeldroid
import android.app.Application
import androidx.preference.PreferenceManager
import com.h.pixeldroid.di.*
import com.h.pixeldroid.utils.ThemeUtils
class Pixeldroid: Application() {
private lateinit var mApplicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this)
ThemeUtils.setThemeFromPreferences(sharedPreferences, resources)
mApplicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(ApplicationModule(this))
.databaseModule(DatabaseModule(applicationContext))
.aPIModule(APIModule())
.build()
mApplicationComponent.inject(this);
}
fun getAppComponent(): ApplicationComponent {
return mApplicationComponent
}
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.db.AppDatabase
import com.h.pixeldroid.di.PixelfedAPIHolder
import com.h.pixeldroid.fragments.PostFragment
import com.h.pixeldroid.objects.DiscoverPost
import com.h.pixeldroid.objects.Status
......@@ -16,25 +18,32 @@ import kotlinx.android.synthetic.main.activity_post.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class PostActivity : AppCompatActivity() {
private lateinit var postFragment : PostFragment
lateinit var domain : String
private lateinit var accessToken : String
@Inject
lateinit var db: AppDatabase
@Inject
lateinit var apiHolder: PixelfedAPIHolder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_post)
(this.application as Pixeldroid).getAppComponent().inject(this)
val status = intent.getSerializableExtra(POST_TAG) as Status?
val discoverPost: DiscoverPost? = intent.getSerializableExtra(DISCOVER_TAG) as DiscoverPost?
val db = DBUtils.initDB(applicationContext)