Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4c727a7c
authored
May 20, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初步实现module及component
parent
e4d10df3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
1 deletions
+105
-1
app/src/main/java/com/bgycc/smartcanteen/RootApp.java
+9
-1
app/src/main/java/com/bgycc/smartcanteen/dagger/ApplicationComponent.java
+21
-0
app/src/main/java/com/bgycc/smartcanteen/dagger/ApplicationModule.java
+75
-0
No files found.
app/src/main/java/com/bgycc/smartcanteen/RootApp.java
View file @
4c727a7c
...
...
@@ -13,7 +13,10 @@ import com.blankj.utilcode.util.Utils;
import
java.io.File
;
public
class
RootApp
extends
Application
implements
Configuration
.
Provider
{
import
dagger.android.AndroidInjector
;
import
dagger.android.support.DaggerApplication
;
public
class
RootApp
extends
DaggerApplication
implements
Configuration
.
Provider
{
private
static
final
String
LOG_PREFIX
=
"app"
;
private
static
final
String
LOG_DIR
=
"log"
;
// 日志文件保留周期
...
...
@@ -40,4 +43,9 @@ public class RootApp extends Application implements Configuration.Provider {
.
setExecutor
(
SCTaskExecutor
.
getIOThreadExecutor
())
.
build
();
}
@Override
protected
AndroidInjector
<?
extends
DaggerApplication
>
applicationInjector
()
{
return
null
;
}
}
app/src/main/java/com/bgycc/smartcanteen/dagger/ApplicationComponent.java
0 → 100644
View file @
4c727a7c
package
com
.
bgycc
.
smartcanteen
.
dagger
;
import
android.content.Context
;
import
com.bgycc.smartcanteen.RootApp
;
import
javax.inject.Singleton
;
import
dagger.BindsInstance
;
import
dagger.Component
;
import
dagger.android.AndroidInjector
;
@Singleton
@Component
(
modules
=
ApplicationModule
.
class
)
interface
ApplicationComponent
extends
AndroidInjector
<
RootApp
>
{
@Component
.
Factory
interface
Factory
{
ApplicationComponent
create
(
@BindsInstance
Context
applicationContext
);
}
}
\ No newline at end of file
app/src/main/java/com/bgycc/smartcanteen/dagger/ApplicationModule.java
0 → 100644
View file @
4c727a7c
package
com
.
bgycc
.
smartcanteen
.
dagger
;
import
android.content.Context
;
import
androidx.room.Room
;
import
com.bgycc.smartcanteen.data.DatabaseManager
;
import
com.bgycc.smartcanteen.data.ForceToPayOffline
;
import
com.bgycc.smartcanteen.data.OldDataCompatible
;
import
com.bgycc.smartcanteen.data._1To_2Migration
;
import
com.bgycc.smartcanteen.entity.PayRequest
;
import
com.bgycc.smartcanteen.entity.typeadapter.PayRequestTypeAdapter
;
import
com.bgycc.smartcanteen.repository.CommandRepository
;
import
com.bgycc.smartcanteen.repository.PayDataRepository
;
import
com.bgycc.smartcanteen.repository.PayResponseRepository
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
import
javax.inject.Singleton
;
import
dagger.Module
;
import
dagger.Provides
;
import
kotlin.jvm.JvmStatic
;
@Module
public
class
ApplicationModule
{
private
static
final
String
DB_NAME
=
"smart_canteen.db"
;
@JvmStatic
@Singleton
@Provides
static
CommandRepository
provideCommandRepository
(
DatabaseManager
database
)
{
return
new
CommandRepository
(
database
.
getCommandDao
());
}
@JvmStatic
@Singleton
@Provides
static
PayDataRepository
providePayDataRepository
(
DatabaseManager
database
)
{
return
new
PayDataRepository
(
database
.
getPayDataDao
());
}
@JvmStatic
@Singleton
@Provides
static
PayResponseRepository
providePayResponseRepository
(
DatabaseManager
database
)
{
return
new
PayResponseRepository
(
database
.
getPayResponseDao
());
}
@JvmStatic
@Singleton
@Provides
static
DatabaseManager
provideDatabase
(
Context
context
)
{
return
Room
.
databaseBuilder
(
context
.
getApplicationContext
(),
DatabaseManager
.
class
,
DB_NAME
)
.
addCallback
(
new
OldDataCompatible
())
.
addCallback
(
new
ForceToPayOffline
())
.
addMigrations
(
new
_1To_2Migration
())
// 一旦版本不兼容则清空数据库
.
fallbackToDestructiveMigration
()
.
allowMainThreadQueries
()
.
build
();
}
@JvmStatic
@Singleton
@Provides
static
Gson
provideGson
()
{
return
new
GsonBuilder
()
.
registerTypeAdapter
(
PayRequest
.
class
,
new
PayRequestTypeAdapter
())
.
create
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment